【问题标题】:C# DateTime.Parse(longdate) throws exceptionC# DateTime.Parse(longdate) 抛出异常
【发布时间】:2017-10-12 07:01:41
【问题描述】:

在 UWP 中,像这样将日期格式化为 longdate 字符串

string myDateString = new DateTimeFormatter("longdate").Format(DateTime.Today);

myDateString = "‎Thursday‎, ‎12‎ ‎October‎ ‎2017"

尝试像这样将其转换回来

DateTime myDate = DateTime.Parse(myDateString, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal);

抛出System.FormatException

尝试像这样将其转换回来

DateTime myDate = DateTime.ParseExact(myDateString, "longdate", CultureInfo.CurrentCulture);

也抛出System.FormatException

然后我将我的机器设置为 US。 myDateString = "‎Thursday‎, ‎October‎ 12‎ ‎‎2017"的值

但是当我尝试将它转换回日期时间时,这也会引发System.FormatException

在 C# 中如何使用当前区域性将长日期字符串转换为日期时间?

【问题讨论】:

  • 你的长日期格式是什么?毫米/日/年?获取日期时间,DateTime d = DateTime.Now;然后将其转换为您想要的任何格式的字符串, string s = d.ToString("dd/MM/yyyy-HH:mm:ss.fff");
  • DateTime.ParseExact 不使用DateTimeFormatter 所以我认为它不知道DateTimeFormatter("longdate") 的格式
  • 我认为这是 UWP?如果是这样,我建议以这种方式标记它。此外,请提供您为myDateString 获得的实际价值。
  • 如果你需要一个格式化的字符串,这就足够了:DateTime.Today.ToLongDateString()
  • 如我的示例所示,将日期转换为字符串没有问题。问题是从长日期字符串转换回日期时间

标签: c# datetime uwp


【解决方案1】:

@Jay Zuo 解释在Cannot convert string to DateTime in uwp

当我们使用DateTimeFormatter.Format方法时,它的返回值中有一些不可见的8206字符

所以正如@Corak 建议的那样,不要使用DateTimeFormatter,使用ToString("D")

【讨论】:

    【解决方案2】:
    string sd = "‎Thursday‎,‎ ‎October‎ 12, ‎2017";
    sd = DateTime.Now.ToString("dddd, MMMM dd, yyyy", new CultureInfo("en-US"));
    DateTime myDate;
    if (DateTime.TryParseExact(sd,"dddd, MMMM dd, yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out myDate))
    {
       Console.WriteLine(myDate);    //if format accepted.
    }
    

    【讨论】:

    • 您的解决方案与文化无关。参考上面的cmets。看来这是一个 UWP 问题。
    • 是的,如果你使用 CultureInfo 像 new CultureInfo((int)CultureTypes.AllCultures) 那么我认为哪种文化并不重要。
    【解决方案3】:

    阅读一些日期时间格式...DateTime Formats

    相关的堆栈帖子:here

    日期时间基础知识:basics

    DateTime with culture使用示例

    一个基本的例子:

    DateTime d = DateTime.Now;
    DateTime ut = d.ToUniversalTime();
    // Defines a custom string format to display the DateTime value.
    // zzzz specifies the full time zone offset.
      String format = "MM/dd/yyyy hh:mm:sszzz";
    
    String utcstr = utcdt.ToString(format);
          Console.WriteLine(utcstr);
    

    编辑:小型控制台应用示例

    static void Main(string[] args)
            {
                string myDateString = "Thursday, 12 October 2017";
                //Why use the above just get a new one for today in the correct format
                //Or create your own converter
                DateTime date = DateTime.Now;
    
                CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
                //or
                var culture = System.Globalization.CultureInfo.CurrentCulture;
    
                string test = currentCulture.ToString();
    
                 Console.WriteLine(date.ToString(CultureInfo.GetCultureInfo(test)));
                Console.ReadLine();
            }
    

    【讨论】:

    • 感谢@JohnChris 的建议,但我们不能指定这样的日期格式,因为我们必须使用用户机器的文化。
    • 再次感谢@JohnChris,但我们没有使用 UTC。我们使用本地机器的文化......无论在哪里。
    • @Vague 好的.. 我为当前的文化提出了建议......但我不明白为什么你要使用 myDateString,如果它的格式无效。解决此问题的方法是构建您自己的转换器来获取日期、月份和年份并输入正确的格式,您希望我给您写一个转换器吗?
    • mydateString 不是无效格式。它是由 'Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longdate")' 创建的标准长日期格式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2015-08-04
    相关资源
    最近更新 更多