datetime.parse 并使其以特定格式工作 [英] datetime.parse and making it work with a specific format

查看:60
本文介绍了datetime.parse 并使其以特定格式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 XML 文件返回的日期时间,格式如下:

I have a datetime coming back from an XML file in the format:

20080916 11:02

20080916 11:02

yyyymm hh:ss

yyyymm hh:ss

我怎样才能得到 datetime.parse 函数来解决这个问题?即解析它没有错误?干杯

How can i get the datetime.parse function to pick up on this? Ie parse it without erroring? Cheers

推荐答案

DateTime.ParseExact(input,"yyyyMMdd HH:mm",null);

假设您想说小时之后是分钟,而不是秒 - 您的示例有点令人困惑.

assuming you meant to say that minutes followed the hours, not seconds - your example is a little confusing.

ParseExact 文档详细说明其他重载,以防您希望解析自动转换为世界时或类似的时间.

The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that.

正如@Joel Coehoorn 所提到的,还可以选择使用 TryParseExact,它将返回一个布尔值,指示操作成功或失败- 我还在使用 .Net 1.1,所以我经常忘记这个.

As @Joel Coehoorn mentions, there's also the option of using TryParseExact, which will return a Boolean value indicating success or failure of the operation - I'm still on .Net 1.1, so I often forget this one.

如果需要解析其他格式,可以查看标准日期时间格式字符串.

If you need to parse other formats, you can check out the Standard DateTime Format Strings.

这篇关于datetime.parse 并使其以特定格式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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