简体   繁体   English

如何将某些字符串解析为日期时间格式

[英]How to parse certain string to datetime format

I receive data containing a string of a datetime in the following format: 我收到包含以下格式的日期时间字符串的数据:

Tue Nov 08 11:20:00 GMT 2016 2016年11月8日星期二11:20:00
Fri Nov 11 08:05:00 GMT 2016 2016年11月11日星期五,格林尼治标准时间08:05:00

I'm finding it very difficult to parse this into a DateTime. 我发现很难将其解析为DateTime。 How can this be parsed into a date time? 如何将其解析为日期时间?

I think this should work: 我认为这应该工作:

public DateTime ConvertToDateTime(string dateString)
{
    return DateTime.ParseExact(dateString, "ddd MMM dd HH:mm:ss Z yyyy", CultureInfo.InvariantCulture)
}

You can use DateTime.ParseExact with a custom format string : 您可以将DateTime.ParseExact自定义格式字符串一起使用

var dt = DateTime.ParseExact("Tue Nov 08 11:20:00 GMT 2016", 
    "ddd MMM dd HH:mm:ss Z yyyy", CultureInfo.InvariantCulture);

See this fiddle . 看到这个小提琴

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

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