Home »

DateTime Format in C# [Convert String to DateTime and backwards]

Converting string to datetime and backwards is sometime a problem. I used to face this problem. Everytime when I work with DateTime format, I google it, and it takes a lot of time to find out the solution. After several times, I think that I found the suitable solution that works perfectly (for me xD).

Convert String to DateTime


When you want to convert a string to a DateTime object, all you have to do is to use DateTimeFormatInfo.
string dtStr = "2010-12-01 15:23:42.123";
DateTime dt;
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
try{
dtfi.ShortDatePattern = "yyyy-MM-dd HH:mm:ss.fff";
//Or you can use dtfi.LongDatePattern
dtfi.DateSeparator = "-"; //date separator character
dtfi.TimeSeparator = ":"; //time sepatator character
dt = Convert.ToDateTime(strDate, dtfi);
}catch (Exception ex){
//Do something here
}

The convenient when you use DateTimeFormatInfo is that you can define the DateTime format inside the string. In the code above, the result will be 2010 Dec 01st Time. But if I change the pattern into yyyy-dd-MM HH:mm:ss.fff then the result will be 2010 Jan 12th Time
The only thing is to make sure your string match the format xD.


Here are the custom format specifiers:


y: year
M: month
d: day
h: hour (12)
H: hour (24)
m: minute
s: second
f: second fraction
F: second fraction, trailing zeroes are trimmed
t: AM/PM
z: time zone

Convert DateTime to String


When you want to convert a DateTime object to a string, the flexible way is defining your own format as below
DateTime dt = new DateTime(2010, 12, 31, 09, 32, 41, 251);

String.Format("{0:MM/dd/yy}", dt);
// Result is 12/31/2010

Here are the custom format specifiers with the output
DateTime dt = new DateTime(2010, 01, 02, 13, 04, 05, 678);

//Year
String.Format("{0:y yy yyy yyyy}", dt); // "0 10 010 2010"
//Month
String.Format("{0:M MM MMM MMMM}", dt); // "1 01 Jan January"
//Day
String.Format("{0:d dd ddd dddd}", dt); // "2 02 Sat Saturday"
//Hour 12/24
String.Format("{0:h hh H HH}", dt); // "1 01 13 13"
//Minute
String.Format("{0:m mm}", dt); // "4 04"
//Second
String.Format("{0:s ss}", dt); // "5 05"
//Second Fraction
String.Format("{0:f ff fff ffff}", dt); // "6 67 678 6780"
//Second Fraction Without Zeroes
String.Format("{0:F FF FFF FFFF}", dt); // "6 67 678 678"
//Time AM/PM
String.Format("{0:t tt}", dt); // "P PM"
//Time Zone
String.Format("{0:z zz zzz}", dt); // "+7 +07 +07:00"

Combine those to make a suitable format for you. For example something like this:
DateTime dt = new DateTime(2010, 01, 02, 13, 04, 05, 678);

String.Format("{0:MMMM dd, yyyy HH:mm}", dt); // "January 02, 2010 13:04"


Hope this entry help you!
If you found any mistake or error in this entry, please let me know. I'll try to fix that a.s.a.p.
Any solution is highly appreciated!

-Share2Learn-

2 comments:

  1. YOu can achive the same thing with a very simple way like: DateTime.ParseExact(str, "yyyy-MM-dd HH:mm:ss", null);

    ReplyDelete
  2. And datetime to string like dt.ToString("dd/MM/yyyy");

    ReplyDelete