DateTime转换,无论文化 [英] DateTime conversion regardless of culture

查看:327
本文介绍了DateTime转换,无论文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字符串需要成为一个 DateTime 对象:

I have a text string that needs to become a DateTime object:

Feb 10, 2012 at 16:33.29

此文本不更改,但软件将在许多不同的设备上运行,具有不同的 DateTime 格式。

This text does not change, but the software will run on many different devices with different DateTime formats.

如何设置自定义 DateTime 解析器,以便不管文化如何,我将获得一个完全填充的 DateTime 对象?

How can I set a custom DateTime parser so that regardless of culture I will get a fully populated DateTimeobject?

推荐答案

使用自定义格式字符串和不变文化使用 ParseExact

Use ParseExact with a custom format string and the invariant culture:

DateTime date = DateTime.ParseExact(theString, "MMM d', 'yyyy' at 'HH':'mm'.'ss", CultureInfo.InvariantCulture);

这篇关于DateTime转换,无论文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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