C# DateTime to “YYYYMMDDHHMMSS” format

后端 未结 18 1568
故里飘歌
故里飘歌 2020-11-22 01:53

I want to convert a C# DateTime to \"YYYYMMDDHHMMSS\" format. But I don\'t find a built in method to get this format? Any comments?

相关标签:
18条回答
  • 2020-11-22 02:39

    Specify formatted DateTime as Utc:

    Step 1 - Initial date

    var initialDtm = DateTime.Now;
    

    Step 2 - Format date as willing ("yyyyMMddHHmmss")

    var formattedDtm = DateTime.ParseExact(initialDtm.ToString("yyyyMMddHHmmss"), "yyyyMMddHHmmss", CultureInfo.InvariantCulture);    
    

    Step 3 - Specify kind of date (Utc)

    var specifiedDtm = DateTime.SpecifyKind( formattedDtm, DateTimeKind.Utc);
    
    0 讨论(0)
  • 2020-11-22 02:40
    DateTime.Now.ToString("MM/dd/yyyy") 05/29/2015
    DateTime.Now.ToString("ffffdd, dd MMMM yyyy") Friday, 29 May 2015
    DateTime.Now.ToString("ffffdd, dd MMMM yyyy") Friday, 29 May 2015 05:50
    DateTime.Now.ToString("ffffdd, dd MMMM yyyy") Friday, 29 May 2015 05:50 AM
    DateTime.Now.ToString("ffffdd, dd MMMM yyyy") Friday, 29 May 2015 5:50
    DateTime.Now.ToString("ffffdd, dd MMMM yyyy") Friday, 29 May 2015 5:50 AM
    DateTime.Now.ToString("ffffdd, dd MMMM yyyy HH:mm:ss")    Friday, 29 May 2015 05:50:06
    DateTime.Now.ToString("MM/dd/yyyy HH:mm")   05/29/2015 05:50
    DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")    05/29/2015 05:50 AM
    DateTime.Now.ToString("MM/dd/yyyy H:mm")    05/29/2015 5:50
    DateTime.Now.ToString("MM/dd/yyyy h:mm tt") 05/29/2015 5:50 AM
    DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")    05/29/2015 05:50:06
    DateTime.Now.ToString("MMMM dd")    May 29
    DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK") 2015-05-16T05:50:06.7199222-04:00
    DateTime.Now.ToString("ffffd, dd MMM yyy HH’:’mm’:’ss ‘GMT’") Fri, 16 May 2015 05:50:06 GMT
    DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")  2015-05-16T05:50:06
    DateTime.Now.ToString("HH:mm")  05:50
    DateTime.Now.ToString("hh:mm tt")   05:50 AM
    DateTime.Now.ToString("H:mm")   5:50
    DateTime.Now.ToString("h:mm tt")    5:50 AM
    DateTime.Now.ToString("HH:mm:ss")   05:50:06
    DateTime.Now.ToString("yyyy MMMM")  2015 May
    
    0 讨论(0)
  • 2020-11-22 02:41
    DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive
    
    0 讨论(0)
  • 2020-11-22 02:41

    You can use a custom format string:

    DateTime d = DateTime.Now;
    string dateString = d.ToString("yyyyMMddHHmmss");
    

    Substitute "hh" for "HH" if you do not want 24-hour clock time.

    0 讨论(0)
  • 2020-11-22 02:42

    You've practically written the format yourself.

    yourdate.ToString("yyyyMMddHHmmss")

    • MM = two digit month
    • mm = two digit minutes
    • HH = two digit hour, 24 hour clock
    • hh = two digit hour, 12 hour clock

    Everything else should be self-explanatory.

    0 讨论(0)
  • 2020-11-22 02:43

    After spent a lot of hours on Google search, I found the below solution as when I locally give date time, no exception while from other server, there was Error......... Date is not in proper format.. Before saving/ searching Text box date time in C#, just checking either the outer Serer Culture is same like database server culture.. Ex both should be "en-US" or must be both "en-GB" asp below snap shot.

    Even with different date format like (dd/mm/yyyy) or (yyyy/mm/dd), it will save or search accurately.

    0 讨论(0)
提交回复
热议问题