Introduction
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
{
class Program
{
static void 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
{
class Program
{
static void 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.
Full date Short Time/Full date Long Time
For this go to my article Time Format Specifier In C#.
General Date Short Time "g" Format specifier
This format specifier "g" can be used to represent the combination of short date ("d") and short time ("t") patterns, separated by a space as in:
namespace DateTimeFormatString
{
class Program
{
static void Main(string[] args)
{
//Get the system current date
DateTime dt = DateTime.Now;





Join the conversation! Your thoughts help the community grow.