C#中:从字符串不同的格式转换的日期时间 [英] C# : Convert datetime from string with different format

查看:269
本文介绍了C#中:从字符串不同的格式转换的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的字符串是26/01/2011零时十四分00秒
,但我的电脑设置联合国家格式(AM:PM)
如何我字符串转换为日期时间
我尝试 Convert.ToDateTime(),但它造成的错误。

If my string is 26/01/2011 00:14:00 but my computer set United state format (AM:PM) How to convert my string into Datetime? I try Convert.ToDateTime() but it cause error.

推荐答案

作为其他人所说,你可以使用DateTime.TryParseExact,但你似乎也有在你的日期是欧洲文化的格式。它可能不会伤害作出尝试用它来执行转换:

As the others have said, you can use DateTime.TryParseExact, but you also seem to have a European culture format in your date. It might not hurt to make an attempt to use that to perform the conversion:

CultureInfo enGB = new CultureInfo("en-GB"); 
string dateString;
DateTime dateValue;

// Parse date with no style flags.
dateString = "26/01/2011 00:14:00";
DateTime.TryParseExact(dateString, "g", enGB, DateTimeStyles.None, out dateValue);

这篇关于C#中:从字符串不同的格式转换的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆