.NET解析字符串转换成日期时间 [英] .NET Parsing string into DateTime

查看:119
本文介绍了.NET解析字符串转换成日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它是日期时间的重新presentation:

I have a string which is a representation of DateTime:

字符串日期时间=20110801;

string dateTime = "20110801";

现在我想用DateTime.Parse(日期时间);

Now I want to use DateTime.Parse(dateTime);

它抛出一个异常,说明日期时间是不是有效的格式。

It throws an Exception stating that dateTime is not in a valid format.

谁能告诉我怎么可以做到?

Can anyone tell me how it can be done??

推荐答案

您可以使用的 DateTime.ParseExact ,该取入的格式作为一个参数。

You can use DateTime.ParseExact, which takes in the format as a parameter.

例如:

DateTime.ParseExact("20110801", "yyyyMMdd", CultureInfo.CurrentCulture)

这篇关于.NET解析字符串转换成日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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