Loading ...

DateTime in C#: Different Formats

 /5
0 (0votes)

Here are a few handy string formats to convert a given datetime to custom formats:

//save today's date in a string
string date = DateTime.Now.Date;

1. Show Date as " Friday 13 August 2003"

 date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.Year.ToString()

2. Show date as "Friday 13 August 13:00:13"  //time in 24H format

 date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.ToString("HH:mm:ss")

3. Show date in 12H format with AM/PM as: "Friday 13 August 01:00:13 PM"

  date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.ToString("hh:PM:mm:ss:tt")

Comments (1)

   
nyjoshi
hi,i recently tried to insert a date in a windows application with datetime picker, but it did not allow me to edit time.secondly when i tried to use custom formats, it threw an exception, saying cannot convert string to datetimecan u please help?
9/25/2010
 · 
by

Top Posts