[英]How can I convert a string into datetime in .NET?
How can I convert dates like "Jun 17 2009, 03:37 pm ET" into a DateTime
variable using C#?如何使用 C# 将诸如“Jun 17 2009, 03:37 pm ET”之类的日期转换为
DateTime
变量?
I have tried DateTime.ParseExact
but I haven't figured out the correct format to use.我试过
DateTime.ParseExact
但我还没有想出要使用的正确格式。
// String to DateTime
String MyString;
MyString = "1999-09-01 21:34 PM";
//MyString = "1999-09-01 21:34 p.m."; //Depends on your regional settings
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);
Source: http://www.codeproject.com/KB/cs/String2DateTime.aspx来源: http://www.codeproject.com/KB/cs/String2DateTime.aspx
Modified to fit your date format:修改以适合您的日期格式:
// String to DateTime
String MyString;
MyString = "Jun 17 2009, 03:37 pm";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "MMM dd YYYY, HH:mm tt", null);
Have you tried DateTime.Parse()
?你试过
DateTime.Parse()
吗? I ususally find that it is not necessary to specify the format unless there is some abiguity between what number is the month and what number is the day.我通常发现没有必要指定格式,除非月份和日期之间存在一些歧义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.