C# 如何通过格式将字符串转换为日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14103970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:41:03  来源:igfitidea点击:

How convert string to Datetime by a format?

c#stringdatetimeformat

提问by Ehsan

Possible Duplicate:
Convert string to DateTime in c#

可能的重复:
在 C# 中将字符串转换为 DateTime

How can I convert string to DateTime by a format?

如何通过格式将字符串转换为 DateTime?

Convert.ToDateTime("12/11/17 2:52:35 PM")

Result is 12/11/2017 02:52:35 PMand this is incorrect because my expect is 11/17/2012 02:52:35 PM

结果是12/11/2017 02:52:35 PM,这是不正确的,因为我的期望是11/17/2012 02:52:35 PM

采纳答案by SLaks

You're looking for DateTime.ParseExact().

您正在寻找DateTime.ParseExact().

DateTime.ParseExact(myStr, "yy/MM/dd h:mm:ss tt", CultureInfo.InvariantCulture);

回答by Oded

It is better to use one of the DateTime.*Parsemethods.

最好使用其中一种DateTime.*Parse方法。

These take the string representing the DateTime, a format string (or array of them) and some other parameters.

它们采用表示 的字符串DateTime、格式字符串(或它们的数组)和一些其他参数。

The custom format stringwould be yy/MM/dd h:mm:ss tt.

自定义格式字符串yy/MM/dd h:mm:ss tt

So:

所以:

var date = DateTime.ParseExact("12/11/17 2:52:35 PM", 
                               "yy/MM/dd h:mm:ss tt"
                               CultureInfo.InvariantCulture);

回答by Woot4Moo

You need to specify the culture of the string:

您需要指定字符串文化

// Date strings are interpreted according to the current culture. 
// If the culture is en-US, this is interpreted as "January 8, 2008",
// but if the user's computer is fr-FR, this is interpreted as "August 1, 2008" 
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);            
Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", dt.Year, dt.Month, dt.Day);

// Specify exactly how to interpret the string.
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);

// Alternate choice: If the string has been input by an end user, you might  
// want to format it according to the current culture: 
// IFormatProvider culture = System.Threading.Thread.CurrentThread.CurrentCulture;
DateTime dt2 = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
Console.WriteLine("Year: {0}, Month: {1}, Day {2}", dt2.Year, dt2.Month, dt2.Day);

/* Output (assuming first culture is en-US and second is fr-FR):
    Year: 2008, Month: 1, Day: 8
    Year: 2008, Month: 8, Day 1
 */

回答by Soner G?nül

Use DateTime.ParseExact()method.

使用DateTime.ParseExact()方法。

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

使用指定的格式和特定​​于区域性的格式信息将日期和时间的指定字符串表示形式转换为其等效的 DateTime。字符串表示的格式必须与指定的格式完全匹配。

   DateTime result = DateTime.ParseExact(yourdatestring, 
                                        "yy/MM/dd h:mm:ss tt",             
                                         CultureInfo.InvariantCulture);

回答by Olivier Jacot-Descombes

Try this

尝试这个

dateValue = DateTime.ParseExact(dateString,
                                "yy/MM/dd hh:mm:ss tt",
                                new CultureInfo("en-US"),
                                DateTimeStyles.None);

"tt" is the AM/PM designator.

“tt”是 AM/PM 指示符。