Saturday, 22 March 2014

Datetime format in c#

How to format DateTime using datetime.tostring( format) method;
Formatting is defined into different character and it represents different formats like
 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 (P.M or A.M) and z (time zone).
You can change the date separator / (slash) or – (Dash) and time sepatator : (colon) or . (dot)
@Format datetime by single character
Formatting dattime by single charater values.
DateTime dt = DateTime.Now; // get the current date
Console.WriteLine(dt.ToString("d") ); // 15/12/2013
Console.WriteLine(dt.ToString("D") ); // Sunday, December 15, 2013
Console.WriteLine(dt.ToString("m") ); //  December 15
Console.WriteLine(dt.ToString("M") ); //  December 15
Console.WriteLine(dt.ToString("t") ); //   04:10 PM
Console.WriteLine(dt.ToString("T")); //   04:10:45 PM
Console.WriteLine(dt.ToString("y")); //   December, 2013
Console.WriteLine(dt.ToString("Y")); //   December, 2013
@Out Put
Format datetime
Format DatTime in normal way by using Datetime object and string value;
DateTime dt = DateTime.Now; // get the current date
          
Console.WriteLine(dt.ToString("MM/dd/yyyy"));   // 12/15/2013
Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy"));   //Sunday, 15 December 2013
Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm")); // Sunday, 15 December 2013 14:58
Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy H:mm")); // Sunday, 15 December 2013 5:01
Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy hh:mm tt")); // Sunday, 15 December 2013 03:00 PM
Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy h:mm tt")); // Sunday, 15 December 2013 3:03 PM
Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss")); // Sunday, 15 December 2013 15:04:10
Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm")); // 12/15/2013 15:05
           Console.WriteLine(dt.ToString("MM/dd/yyyy H:mm")); // 12/15/2013 15:07
           Console.WriteLine(dt.ToString("MM/dd/yyyy h:mm tt")); // 12/15/2013 3:07 PM
           Console.WriteLine(dt.ToString("MM/dd/yyyy hh:mm tt")); // 12/15/2013 03:06 PM
          
           Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm:ss")); // 12/15/2013 15:09:29
           Console.WriteLine(dt.ToString("MMMM dd")); // December 15
           Console.WriteLine(dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK")); // 2013-12-15T15:11:19.1250000+05:30   
           Console.WriteLine(dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'")); // Sat, 15 Jul 2013 15:12:16 GMT
           Console.WriteLine(dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss")); // 2013-12-15T15:12:57
           Console.WriteLine(dt.ToString("HH:mm")); // 15:14
           Console.WriteLine(dt.ToString("hh:mm tt")); // 03:14 PM
           Console.WriteLine(dt.ToString("H:mm")); // 5:15
           Console.WriteLine(dt.ToString("h:mm tt")); // 3:16 PM
           Console.WriteLine(dt.ToString("HH:mm:ss")); // 15:16:29
           Console.WriteLine(dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'")); // 2013-12-15 15:17:20Z
           Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss")); // Sunday, 15 December 2013 15:17:58
           Console.WriteLine(dt.ToString("yyyy MMMM")); // 2013 December
  



how to do the formating of datetime which is passed as string format?
string dateString = "6/28/2016"; // Modified from MSDN
           // string format = "ddd dd MMM h:mm tt yyyy";

            DateTime dateTime = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
            MessageBox.Show("date " + dateTime);

No comments:

Post a Comment

Others

You will like to read it