Format DateTime value using the invariant DateTimeFormatInfo in CSharp

Description

The following code shows how to format DateTime value using the invariant DateTimeFormatInfo.

Example


using System;/* ww  w . ja  va 2s  .com*/
using System.Globalization; 

public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var