简体   繁体   English

如何使用一种方法将字符串的多种不同类型的日期值转换为datetime?

[英]How can I convert date values from the multiple different types that are string to datetime with one method?

I have multiple datetime values. 我有多个日期时间值。 I would like to convert them to datetime with one method. 我想用一种方法将它们转换为日期时间。

The date values may change by system regional settings like below. 日期值可能会受到如下所示的系统区域设置的影响。

'7/26/2013 12:00:00 AM'
'26.7.2013 12:00:00'
'07-26-2013 12:00 AM'

Is there a way to do this without changing system regional settings with one method ? 有没有一种方法可以用一种方法来更改系统区域设置?

I believe the question DateTime.TryParse all possible type of dates may be of some use to you. 我相信问题DateTime.TryParse所有可能的日期类型可能对您有用。

Here is the snippet: 这是代码段:

CultureInfo ci = CultureInfo.GetCultureInfo("sl-SI");
string[] fmts = ci.DateTimeFormat.GetAllDateTimePatterns();

if (DateTime.TryParseExact(date, fmts, ci, DateTimeStyles.AssumeLocal, out dt))
{
    DateTime = Convert.ToDateTime(date);
    Check = true;
}

Update: 更新:

This codeproject article may also be of use to you: 此代码项目文章也可能对您有用:

http://www.codeproject.com/Articles/33298/C-Date-Time-Parser http://www.codeproject.com/Articles/33298/C-Date-Time-Parser

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

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