Standard Date and Time Format String In C#

By using the standard date and time format strings we can represent the date and time in various ways. Their are multiple format specifiers to format the date and time in various ways, some of them are described here.

Date Format Specifier

The date format specifier can be used to display the date in either short or long format, In other words if I use the short date format specifier "d" it will give the date in a short format and if I use the long date specifier "D" then it will give the date in a long format. For more details go to my article Date Format Specifier In C# .

Time Format Specifier

The time specifier can also be used to represent the time in a short or long format.

The Short Time "t" Format specifier

The short time will only display the hour and minutes information but not seconds information, and we can display the time information in many cultures, as in:

namespace DateTimeFormatString

{

classProgram

{

staticvoid Main(string[] args)

{

//Get the system current date

DateTime dt = DateTime.Now;

Console.WriteLine("The default time (en-US) is as :" + dt.ToString("t"));

Console.WriteLine("The time in es-ES culture is as :"+ dt.ToString("t",CultureInfo.CreateSpecificCulture("es-ES")));

Console.WriteLine("The time in hr-HR culture is as :" + dt.ToString("t", CultureInfo.CreateSpecificCulture("hr-HR")));

Console.WriteLine("The time in ar-EG culture is as :" + dt.ToString("t", CultureInfo.CreateSpecificCulture("ar-EG")));

}

}

}

Output

The Long Time "T" Format specifier

The long time will display the hours, minutes and seconds information, and we can display the time information in many cultures, as in:

namespace DateTimeFormatString

{

classProgram

{

staticvoid Main(string[] args)

{

//Get the system current date

DateTime dt = DateTime.Now;

Console.WriteLine("The default time (en-US) is as :" + dt.ToString("T"));

Console.WriteLine("The time in es-ES culture is as :"+ dt.ToString("T",CultureInfo.CreateSpecificCulture("es-ES")));

Console.WriteLine("The time in hr-HR culture is as :" + dt.ToString("T", CultureInfo.CreateSpecificCulture("hr-HR")));

Console.WriteLine("The time in ar-EG culture is as :" + dt.ToString("T", CultureInfo.CreateSpecificCulture("ar-EG")));

}

}

}

Output

Date and Time Format Specifier

In this format specifier we can reformat both the date and time; the various format specifiers in this are described below.