解析日期字符串以获取年份 [英] Parsing a date string to get the year

查看:517
本文介绍了解析日期字符串以获取年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储,如05/11/2010日期的字符串变量。

I have a string variable that stores a date like "05/11/2010".

如何解析字符串只有一年获得?

How can I parse the string to get only the year?

因此,我将有一个像一年变量年= 2010

So I will have another year variable like year = 2010.

推荐答案

您可以使用 DateTime.Parse方法到字符串解析到具有的日期时间值=HTTP ://msdn.microsoft.com/en-us/library/system.datetime.year.aspx>年产权的:

You can use the DateTime.Parse Method to parse the string to a DateTime value which has a Year Property:

var result = DateTime.Parse("05/11/2010").Year;
// result == 2010



根据操作系统的文化设置,可能需要提供一个的CultureInfo

var result = DateTime.Parse("05/11/2010", new CultureInfo("en-US")).Year;
// result == 2010

这篇关于解析日期字符串以获取年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆