如何将诸如2009-05-08 14:40:52,531这样的字符串转换为DateTime?
当前回答
我尝试了各种方法。对我有用的是:
Convert.ToDateTime(data, CultureInfo.InvariantCulture);
我的数据是2017年9月24日上午9:31:34
其他回答
试试下面的方法,strDate是你的日期,格式为'MM/dd/yyyy'
var date = DateTime.Parse(strDate,new CultureInfo("en-US", true))
转换。ToDateTime或DateTime。解析
我尝试了各种方法。对我有用的是:
Convert.ToDateTime(data, CultureInfo.InvariantCulture);
我的数据是2017年9月24日上午9:31:34
试试这个
DateTime myDate = DateTime.Parse(dateString);
一个更好的方法是:
DateTime myDate;
if (!DateTime.TryParse(dateString, out myDate))
{
// handle parse failure
}
如果您不确定输入值,也可以如下所示使用DateTime.TryParseExact()。
DateTime outputDateTimeValue;
if (DateTime.TryParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out outputDateTimeValue))
{
return outputDateTimeValue;
}
else
{
// Handle the fact that parse did not succeed
}
推荐文章
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”
- 使用linq转换列表到字典,不用担心重复
- 单元测试:日期时间。现在
- 什么是回调?
- 返回匿名类型的结果?
- indexOf()和search()的区别是什么?
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 我如何在Swift连接字符串?
- 在c#的控制台应用程序中使用'async
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 在单元测试中设置HttpContext.Current.Session
- 如何开始开发Internet Explorer扩展?
- 更新行,如果它存在,否则插入逻辑实体框架