DateTime Conversion and Parsing DateTime.Now.ToString(“MM/dd/yyyy hh:mm:ss.fff”)

前端 未结 3 886
半阙折子戏
半阙折子戏 2020-12-21 07:41

I store some DateTime in a CSV log with:

DateTime.Now.ToString(\"MM/dd/yyyy hh:mm:ss.fff\")

When I try to read it I found something like:

相关标签:
3条回答
  • 2020-12-21 07:58

    You should use this method to parse your string. You would have to make a class, imlementing IFormatProvider, but if you want to use a custom DateTime format, it's the best method I can think of.

    0 讨论(0)
  • 2020-12-21 08:16

    You can use DateTime.ParseExact:

    string format = "MM/dd/yyyy hh:mm:ss.fff";
    DateTime d = DateTime.ParseExact("05/15/2012 10:09:28.650",
                                    format,
                                    System.Globalization.CultureInfo.InvariantCulture);
    

    Standard Date and Time Format Strings

    0 讨论(0)
  • 2020-12-21 08:21

    use DateTime.ParseExact with specifying the format

    String dateStr=DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff");
    DateTime date = DateTime.ParseExact(dateStr,"MM/dd/yyyy hh:mm:ss.fff",System.Globalization.CultureInfo.InvariantCulture);
    
    0 讨论(0)
提交回复
热议问题