While working with dates two of the most common requirements are:
- Converting date to strings This is usually for displaying the dates to a user in easier to understand
representation. - Converting strings to date This could be for different reasons such as performing calculations and extracting specific parts of the date such as month and year.
Date is represented in .NET with the DateTime data type.For performing the above two operations we use date and time format strings.
To convert a datetime variable to a string we use the ToString() method.If we don’t specify any options in ToString() method then the default conversion happens.If we require more control over how the date is formatted then we need to use the date and time format strings.
Its quite simple to format datetime values in C# by calling the ToString() method and passing the required format specifier.You need to pass the format specifier as a string to the ToString method.
dateTimeObject.Now.ToString("format specifier");
In the following example we are creating a datetime variable.We initialize the datetime variable for the year 2015,8th month and 15th day.For displaying the datetime value we use the ToString() method as:
DateTime date = new DateTime(2015, 8, 15,12,1,1); Console.WriteLine(date.ToString());
Output of the above conversion is:
18-05-2015 12:01:01 PM
We are able to easily convert the datetime value to a string and display it.We can get this far only without using any format strings.For formatting datetime values format strings are used.
Displaying the year ,month and date.
If we look at overloads of the ToString() method we can see that there is an overload which expects a format string.
It is this overload which we can use to pass our format string.To display the datetime value in the format Year-Month-Date we can use the format string as:
Console.WriteLine(date.ToString("y-M-d"));
Unlike the default ToString() method we get the output as:
15-5-18
Format Specifiers
The characters “y”, “M” and “d” in the above format string are called format specifiers.For Date formatting in C# different format specifiers can be used.There are two types of date and time format strings
- Standard date and time format string Consists of a single format specifier.
- Custom date and time format string Consists of a single or multiple format specifiers.
Some of the commonly used format specifiers are:
Standard date time format specifiers
Short date pattern Represented with the character “d” this displays only date,not the time.
Console.WriteLine(date.ToString("d"));
Output:18-05-2015
Long date pattern Represented with the character “D” this displays only date,not the time.Month name is displayed
Console.WriteLine(date.ToString("D"));
Output:18 May 2015
Full date ,short time Represented with the character “f ” this displays date and time.Seconds are not displayed
Console.WriteLine(date.ToString("f"));
Output:18 May 2015 12:01 PM
Full date,full time specifier Represented with the character “F” this displays date and time with seconds
Console.WriteLine(date.ToString("F"));
Output:18 May 2015 12:01:01 PM
Month format specifier Represented with the character “m” this displays only date and month
Console.WriteLine(date.ToString("m"));
Custom format specifiers
Multiple custom format specifiers are used together to format the datetime value.
Following are some of the useful custom format specifiers.We are considering the date 4 feb 2015 in the examples.
Format specifier | Description | Example |
“d” | Day of the month | 4 |
“dd” | Day of the month in two digits | 04 |
“ddd” | Short Week day name | Wed |
“dddd” | Full Week day name | Wednesday |
“M” | Month in digits | 2 |
“MM” | Month in two digits | 02 |
“MMM” | Short name of the month | Feb |
“y” | Year in digits | 15 |
“yy” | Year in minimum 2 digits e.g 01 and 11. | 15 |
“yyy” | Year in minimum 3 digits e.g 001 and 011 | 2015 |
“yyyy” | Year in minimum 4 digits e.g 0001 and 0011 | 2015 |
In the following example we are displaying the day of the week followed by date and year:
dateTimeObject.Now.ToString("dddd, dd MMMM yyyy") Wednesday, 4 Feb 2015 dateTimeObject.Now.ToString("dddd, dd MMMM yyyy") Wednesday, 4 Feb 2015 10:20 AM
If the format string consists of more than one specifiers then then they are interpreted as a custom format specifiers.To specify only a single character for customer format specifier we can either
- Include a space before or after the single format specifier
- Include the percent format specifier before the single format specifier
For example if we execute the following
Console.WriteLine(date.ToString("H"));
then we get the format exception as:
To format the date using single format specifier we can include a space character as:
Console.WriteLine(date.ToString(" H"));