无法在 uwp 中将字符串转换为 DateTime [英] Cannot convert string to DateTime in uwp

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

问题描述

private string GetSystem()
{
    StringBuilder results = new StringBuilder();

    DateTimeFormatter[] basicFormatters = new[]
    {
        // Default date formatters
        new DateTimeFormatter("shortdate"),

        // Default time formatters
        new DateTimeFormatter("longtime"),
    };

    DateTime dateandTime = DateTime.Now;

    foreach (DateTimeFormatter formatter in basicFormatters)
    {
        // Format and display date/time.
        results.Append(formatter.Format(dateandTime));
        results.Append(" ");
    }           
    return results.ToString();
}

dateString = GetSystem();
format = "dd-MM-yyyy HH:mm:ss";
provider = new CultureInfo("en-IN");
try
{           
    result = DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture);
    Debug.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
    Debug.WriteLine("{0} is not in the correct format.", dateString);
}

我收到错误

字符串未被识别为有效的日期时间"

"String was not recognized as a valid DateTime"

在运行此代码时,任何人都可以提出一些想法来解决我的问题.

while running this code can anyone suggest some idea to resolve my problem.

推荐答案

在 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.

这篇关于无法在 uwp 中将字符串转换为 DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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