如何在C#中将DayOfYear转换为日期 [英] How to Convert DayOfYear into Date in C#
问题描述
我想将DayOfValue(例如:257(9月13日))转换为日期(13/09/1990)。只是我的输入是257.我的预期输出是9月13日。任何VB.NET或C#。 NET不重要。
这是食品行业常见的问题,其中过期日期按照格式日期(NNN)的5位数字串进行报告+年(NN)。
请帮助我。
在新的DateTime(1990,9,13).DayOfYear)
中快速输出,显示日期值为 256
。在这种情况下,结合 AddOrdinal 方法-way-to-create-ordinals-in-c>这个答案:
DateTime d = new DateTime (1990,1,1).AddDays(256 - 1);
string answer = string.Format({0} {1:MMMM yyyy},AddOrdinal(d.Day),d);如b所指出的那样,每年的闰年之间的日期将会有所不同,和非闰年值,所以要小心。
I want to convert a DayOfValue(ex:257(Sept 13))into a date(13/09/1990).So simply my input is 257.My expected output is 13th September.Either VB.NET or C#.NET is doesn't matter.
This is a common problem in the food industry where expiration dates are reported in a 5 digit string following the format day of year (NNN) + year (NN).
Please help me.
解决方案 Doing a quick output on new DateTime(1990, 9, 13).DayOfYear)
reveals that day-of-year value to be 256
. That being the case, and in conjunction with the AddOrdinal
method from this answer:
DateTime d = new DateTime(1990, 1, 1).AddDays(256 - 1);
string answer = string.Format("{0} {1:MMMM yyyy}", AddOrdinal(d.Day), d);
As has been pointed out, day-of-year will vary for dates after 28th February between leap year and non-leap year values, so be careful.
这篇关于如何在C#中将DayOfYear转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!