giammin

DateTime Format Strings

Feb 26th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3.  
  4. public class MainClass {
  5.    public static void Main(string[] args)  {
  6.        DateTime dt = DateTime.Now;
  7.        String[] format = {
  8.            "d", "D",
  9.            "f", "F",
  10.            "g", "G",
  11.            "m",
  12.            "r",
  13.            "s",
  14.            "t", "T",
  15.            "u", "U",
  16.            "y",
  17.            "dddd, MMMM dd yyyy",
  18.            "ddd, MMM d \"'\"yy",
  19.            "dddd, MMMM dd",
  20.            "M/yy",
  21.            "dd-MM-yy",
  22.        };
  23.        String date;
  24.        for (int i = 0; i < format.Length; i++) {
  25.            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
  26.            Console.WriteLine(String.Concat(format[i], " :" , date));
  27.        }
  28.  
  29.   /** Output.
  30.    *
  31.    * d :08/17/2000
  32.    * D :Thursday, August 17, 2000
  33.    * f :Thursday, August 17, 2000 16:32
  34.    * F :Thursday, August 17, 2000 16:32:32
  35.    * g :08/17/2000 16:32
  36.    * G :08/17/2000 16:32:32
  37.    * m :August 17
  38.    * r :Thu, 17 Aug 2000 23:32:32 GMT
  39.    * s :2000-08-17T16:32:32
  40.    * t :16:32
  41.    * T :16:32:32
  42.    * u :2000-08-17 23:32:32Z
  43.    * U :Thursday, August 17, 2000 23:32:32
  44.    * y :August, 2000
  45.    * dddd, MMMM dd yyyy :Thursday, August 17 2000
  46.    * ddd, MMM d "'"yy :Thu, Aug 17 '00
  47.    * dddd, MMMM dd :Thursday, August 17
  48.    * M/yy :8/00
  49.    * dd-MM-yy :17-08-00
  50.    */
  51.    }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment