如何从一年中的某一天获取日期

时间:2014-02-22 21:01:21

标签: c# datetime

如何在C#中从date获取day of year

我有这段代码:

int a = 53;   // This is the day of year value, that I got previously
string b = Convert.ToDateTime(a).ToString();   // Trying to get the date

我需要获得值22.2.2014。但这不起作用,我该怎么办?提前谢谢。

4 个答案:

答案 0 :(得分:15)

int a = 53;
int year = DateTime.Now.Year; //Or any year you want
DateTime theDate = new DateTime(year, 1, 1).AddDays(a - 1);
string b = theDate.ToString("d.M.yyyy");   // The date in requested format

答案 1 :(得分:4)

假设你想要当年?

int a = 53;
DateTime date = new DateTime(DateTime.Now.Year, 1,1).AddDays(a -1);

答案 2 :(得分:1)

int a = 53;
var dt = new DateTime(DateTime.Now.Year, 1, 1).AddDays(a - 1);
string b = dt.ToString();

答案 3 :(得分:1)

使用

DateTime.AddDays()

将日期初始化为年初,然后使用此功能将a添加到该日期

http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx