且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在 uwp 中将字符串转换为 DateTime

更新时间:2023-02-03 08:06:39

在 UWP 应用中,同时使用 格式模板,例如 shortdatelongtime,默认情况下,它将使用用户的默认全局上下文,这些上下文显示在 Time & 下的 Settings 中.语言.在我的电脑中,它们设置如下:
所以当我在我身边使用你的 GetSystem 方法时,dateString 就像

In UWP apps, while using Format Template like shortdate or longtime, by default it will use user's default global context which are shown in Settings under Time & Language. And in my computer, they are set as following:
So when I use your GetSystem method in my side, the dateString is like

6‎/‎1‎/‎2016 ‎1:‎44‎:‎43‎ ‎PM

显然这不能用 format = "dd-MM-yyyy HH:mm:ss"; 解析.所以我认为在这里使用固定的自定义日期和时间格式字符串不是一个好习惯.

and obviously this can't be parsed with format = "dd-MM-yyyy HH:mm:ss";. So I think using a fixed custom date and time format string here is not a good practice.

那么即使您的 dateString 的格式与您在 DateTime.ParseExact 方法中使用的 format 匹配,您也将获得错误:String 未被识别为有效的 DateTime.

Then even when the format of your dateString matches the format you've used in DateTime.ParseExact method, you will also get the error: String was not recognized as a valid DateTime.

这是因为,当我们使用DateTimeFormatter.Format方法时,其返回值中有一些不可见的8206个字符.所以你的 dateString 看起来像 30 - 05 - 2016 14 : 54 : 18,但实际上它不是 30-05-2016 年 14:54:18.为了清楚地看到这一点,我们可以将 dateString 转换为字符数组.例如,这里使用shortdate"模板:

This is because, when we use DateTimeFormatter.Format method, there are some invisible 8206 characters in its return value. So your dateString looks like 30‎-‎05‎-‎2016 ‎14‎:‎54‎:‎18, but actually it's not 30-05-2016 14:54:18. To see this clearly, we can convert the dateString to char array. Here using "shortdate" template for example:

var dateString = new DateTimeFormatter("shortdate").Format(DateTime.Now);
var array = dateString.ToCharArray();
foreach (var item in arry)
{
    Debug.WriteLine(item);
}

字符数组会像:

And the char array will like:

所以为了解决你的问题,我建议你使用 通用日期长时间 ("G") 格式说明符.

So to solve your problem, I'd suggest you use The General Date Long Time ("G") Format Specifier.

G"标准格式说明符代表短日期(d")和长时间(T")模式的组合,用空格分隔.

The "G" standard format specifier represents a combination of the short date ("d") and long time ("T") patterns, separated by a space.

您可以使用此格式说明符,如下所示:

You can use this format specifier like following:

var dateString = DateTime.Now.ToString("G");

然后将字符串转换为 DateTime,例如:

And then convert string to DateTime like:

var result = DateTime.Parse(dateString);

var result = DateTime.ParseExact(dateString, "G", null);

这里的 providernull ,它表示使用当前文化对应的 CultureInfo 对象.如果我们在这里使用错误的文化,我们也会得到 String was notknowledge as a valid DateTime 异常.

the provider here is null which represents the CultureInfo object that corresponds to the current culture is used. If we use wrong culture here, we will also get String was not recognized as a valid DateTime exception.

如果您确实想使用 dd-MM-yyyy HH:mm:ss 格式,您可以使用如下代码:

If you do want to use dd-MM-yyyy HH:mm:ss format, you can use some code like:

var dateString = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");

转换和上面一样,只是在这种情况下provider参数不重要.

And the convert is as same as above, only in this scenario the provider parameter is not important.