DateTime.Parse date format

前端 未结 3 751
孤城傲影
孤城傲影 2021-01-11 12:30

I have a date represented as a string thus

20130116154407

I called DateTime.Parse on this but it failed. How can I convert this to a DateT

相关标签:
3条回答
  • 2021-01-11 13:08

    Use this code

    string DATE_FORMAT= "yyyyMMddhhmmss";
    
    DateTime date;
    if(DateTime.TryParseExact(str, DATE_FORMAT, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out date))
    {
    //success
    //you can use date
    }else
    {
    //fail
    }
    
    0 讨论(0)
  • 2021-01-11 13:13

    Try DateTime.ParseExact

    var dt = DateTime.ParseExact("20130116154407", "yyyyMMddhhmmss", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2021-01-11 13:15

    You need to specify a format:

    DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.InvariantCulture)
    
    0 讨论(0)
提交回复
热议问题