[英]Unable to parse a date Time
var date = "05/18/2012";
DateTime.ParseExact(date, "yyyy/MM/dd", CultureInfo.InvariantCulture)
DateTime.Parse(date);
I think the problem here is that the date should be day/month/year. 我认为这里的问题是日期应该是天/月/年。 The problem is that the client wants it in this format.
问题在于客户端需要这种格式。 The system must be able to operate in English and french.
该系统必须能够以英语和法语操作。 I want all dates to be parsed in the same way so they are all in the format - yyyy/MM/dd.
我希望所有日期都以相同的方式进行解析,因此它们都采用yyyy / MM / dd格式。
Neither of the previous examples seem to work. 前面的示例似乎都不起作用。
EDIT: I can set the date in the format 2012/05/18 but how do I do that for a DateTime.Now? 编辑:我可以将日期设置为2012/05/18格式,但如何为DateTime.Now设置日期?
It should be 它应该是
string date = "05/18/2012";
DateTime dateA = DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
string newDate = dateA.ToString("yyyy-MM-dd");
ParseExact allows you to tell the parser what format the date is in. So in your example you are saying it is yyyy/MM/dd so you should have the date setup like "2012/05/18" for it to parse correctly. ParseExact允许您告诉解析器日期的格式。因此,在您的示例中,您说的是yyyy / MM / dd,因此您应具有“ 2012/05/18”之类的日期设置才能正确解析。
Here is example. 这是例子。
var date = "05/18/2012";
var date2 = "2012/05/18";
var pDate = DateTime.Parse(date);
var pDate2 = DateTime.ParseExact(date2, "yyyy/MM/dd", null);
Console.WriteLine("Normal Date Formated: " + pDate.ToString("yyyy/MM/dd"));
Console.WriteLine("Your Format, Parsed: " + pDate2.ToShortDateString());
Console.WriteLine("DateTime.Now Formatted: " + DateTime.Now.ToString("yyyy/MM/dd"));
Output 产量
Normal Date Formated: 2012/05/18
Your Format, Parsed: 5/18/2012
DateTime.Now Formatted: 2012/09/18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.