简体   繁体   English

DateTime格式C#

[英]DateTime formats C#

I have a string which needs to be converted and validated to a DateTime. 我有一个字符串,需要将其转换并验证为DateTime。 The string is in the following format 'dd.mm.yy' I am trying to convert it to DateTime using the following 该字符串的格式为“ dd.mm.yy”,我正在尝试使用以下格式将其转换为DateTime

string format = "dd.mm.yy";
date = DateTime.ParseExact(current.Substring(aiRule.AiLength), format, 
                                           CultureInfo.InvariantCulture);

but unfortunately this fails. 但不幸的是,这失败了。 The question is how to convert a string in the format 'dd.mm.yy' to a DateTime ? 问题是如何将格式为“ dd.mm.yy”的字符串转换为DateTime? Thank you 谢谢

mm means "minutes". mm表示“分钟”。 I suspect you want "dd.MM.yy" . 我怀疑您想要"dd.MM.yy" See MSDN for more information about custom date and time format strings . 有关自定义日期和时间格式字符串的更多信息,请参见MSDN。

(In particular, read the part about the "yy" specifier and how it chooses which century to use. If you can possibly change the input to use a four digit year, that could save you some problems...) (特别是,请阅读有关“ yy”说明符以及它如何选择使用哪个世纪的部分。如果您可以将输入更改为使用四位数的年份,则可以节省一些问题...)

I'll tell something "heretical". 我会说一些“异端”。 If dd.MM.yy (with 2 or 4 yy ) is the format of your local culture, then you could let the DateTime.Parse (not ParseExact !) do its work without setting it to CultureInfo.InvariantCulture , or perhaps setting it to your local culture like new CultureInfo("it-IT") . 如果dd.MM.yy (具有2或4 yy )是您本地文化的格式,那么您可以让DateTime.Parse (不是ParseExact !)完成其工作,而无需将其设置为CultureInfo.InvariantCulture或将其设置为您的本地文化,例如new CultureInfo("it-IT")

the string format should be like this.... 字符串格式应该是这样的。

string Format =  "dd.MM.yy"


 mm is for showing minutes 

 MM is for showing months..

I hope it will helps you... 希望对您有帮助...

As earlier posts has already pointed out, mm means minutes and MM means months. 正如之前的帖子所指出的,mm表示分钟,MM表示月。 I ran this test snippet and it works as expected: 我运行了此测试代码段,它按预期运行:

string format = "dd.MM.yy";
string date = "27.10.11";
DateTime result;
result = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM