代码之家  ›  专栏  ›  技术社区  ›  Nitin S

指定格式时,日期时间分析不起作用

c#
  •  0
  • Nitin S  · 技术社区  · 8 年前

    指定格式时,日期时间分析不起作用

    void Main()
    {
        Console.WriteLine(DateTime.Now.ToString(@"dd\/MM\/yyyy hh:mm:ss tt"));
        DateTime ValidUptoFO;
        string date = "10/25/2018 10:38:26 AM";
        var op = DateTime.ParseExact(date,@"dd\/MM\/yyyy hh:mm:ss tt",null);
        Console.WriteLine(op);
    }
    

    这是个错误 The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

    10/25/2018 10:38:26 AM DateTime

    3 回复  |  直到 8 年前
        1
  •  3
  •   ProgrammingLlama Raveena Sarda    8 年前

    如果你使用 CultureInfo.InvariantCulture 它将工作:

        private static void Main(string[] args)
        {
            const string DATE_FORMAT = "MM/dd/yyyy hh:mm:ss tt";
    
            Console.WriteLine(DateTime.Now.ToString(DATE_FORMAT, CultureInfo.InvariantCulture));
    
            string dateText = "10/25/2018 10:38:26 AM";
            var op = DateTime.ParseExact(dateText, DATE_FORMAT, CultureInfo.InvariantCulture);
            Console.WriteLine(op.ToString(DATE_FORMAT,CultureInfo.InvariantCulture));
            Console.ReadKey();
        }
    
        2
  •  2
  •   Tetsuya Yamamoto    8 年前

    日期格式为 MM/dd/yyyy

    var op = DateTime.ParseExact(date, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
    

        3
  •  1
  •   Access Denied    8 年前

    虽然不变区域性解决了OP问题,但最好理解指定日期时间格式的错误。

    如果您有用指定格式保存日期时间的代码和用指定格式读取相同日期的代码。问题是 / : 在不同的文化中也有不同的解释 tt . 和

        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fi");
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fi");
        var format = @"dd/MM/yyyy hh:mm:ss";
        var dateString = DateTime.Now.ToString(format);
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
        var parsedDate = DateTime.ParseExact(dateString,format,null);
    

    让我们解释一下它为什么会崩溃。让我们切换到有趣的区域性,它演示了问题中dateformat的所有问题:

    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fi");
    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fi");  
    

    问题是,在芬兰地区,您将得到以下结果:

    Console.WriteLine(DateTime.Now.ToString(@"dd/MM/yyyy hh:mm:ss tt"));
    

    2018年10月25日05.33.42 ip。

    Console.WriteLine(DateTime.Now.ToString(@"dd\/MM\/yyyy hh:mm:ss tt"));
    

    2018年10月25日05.36.40 ip。

    Console.WriteLine(DateTime.Now.ToString(@"dd\/MM\/yyyy hh\:mm\:ss tt"));
    

    以及解决 ip.

    Console.WriteLine(DateTime.Now.ToString(@"dd\/MM\/yyyy HH\:mm\:ss"))
    

    25/10/2018 17:45:03

        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fi");
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fi");
        var format = @"dd\/MM\/yyyy HH\:mm\:ss";
        var dateString = DateTime.Now.ToString(format);
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
        var parsedDate = DateTime.ParseExact(dateString, format, null);