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?
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);
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
DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive
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.
You've practically written the format yourself.
yourdate.ToString("yyyyMMddHHmmss")
Everything else should be self-explanatory.
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.