将Sting转换为DateTime“MMYY” [英] Convert Sting to DateTime "MMYY"
本文介绍了将Sting转换为DateTime“MMYY”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的字符串1217,我希望转换为日期时间格式为MMYY,
所以如何为所有人制作
thx ....
解决方案
不是100%肯定会有什么结果。它很可能是2017年12月1日。这取决于你想如何使用它。如果你把它当作一个范围那么你也必须找到月底
DateTime.ParseExact( 1217, MMYY,CultureInfo.InvariantCulture);
月末:
DateTime.ParseExact( 1217, MMYY,CultureInfo.InvariantCulture).AddMonths( 1 ) .AddDays(-1);
尝试MMyy
< pre lang =c#> DateTime.ParseExact( 1217, MMyy,CultureInfo.InvariantCulture);
没有资本Y日期时间格式的格式!
Quote:这里有以下自定义格式说明符y(年),M(月),d(日) ,h(小时12),H(小时24),m(分钟),s(秒),f(第二分数),F(第二分数,尾随零被修整),t(PM或AM)和z(时间) zone)。
DateTime没有格式,所以不存在带格式的日期时间。您能做的最好的事情就是将您的1207字符串转换为任意日期时间,例如2017年12月1日,并了解当天部分无关紧要。
string s = 1217\" 跨度>;
DateTime dt;
DateTime.TryParseExact( 01 + s, ddMMyy,System.Globalization.CultureInfo.CurrentCulture,System.Globalization.DateTimeStyles.None,出 dt);
// dt现在代表2017年12月1日
// 添加3个月
dt = dt.AddMonths( 3 跨度>);
// 转换回MMyy
s = dt.ToString ( MMyy);
i have string like this "1217" and i want convert to datetime with format "MMYY",
so how to make
thx for all....
解决方案
Not 100% sure what this will come up with. It will most likely be 01 dec 2017. It depends on how you want to use it. If you use it as a range then you will have to also find the end of the month
DateTime.ParseExact("1217", "MMYY", CultureInfo.InvariantCulture);
End of Month:
DateTime.ParseExact("1217", "MMYY", CultureInfo.InvariantCulture).AddMonths(1).AddDays(-1);
try withMMyy
DateTime.ParseExact("1217", "MMyy", CultureInfo.InvariantCulture);
There is no capital Y format in datetime formats!
Quote:here are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
DateTime doesn't have a format so there is no such thing as "datetime with format". The best you'll be able to do is to convert your 1207 string into an arbitrary datetime such as 1 Dec 2017 with the understanding that the day part is irrelevant.
string s = "1217"; DateTime dt; DateTime.TryParseExact("01" + s, "ddMMyy", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dt); // dt now represents 1st Dec 2017 // add 3 months dt = dt.AddMonths(3); // convert back to MMyy s = dt.ToString("MMyy");
这篇关于将Sting转换为DateTime“MMYY”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文