Simple Date Time Format in .NET
Posted by triaslama on December 10, 2007
DateTime format it’s a simple but interesting enough to get my attention. The problem begin when I must work with the DateTime type and I encountered several exception that raised because of date time formatting.
For explanatory purpose here I give a brief introduction on date time formatting in .NET platform:
|
Format Specifier |
Description |
|
d |
Displays the specified date of the month (short form). |
|
D |
Displays the specified date of the month (long form). |
|
dd |
Displays the specified date of the month (measured the number between 1 and 31 -inclusive-). If the number is a single digit only, then the formatting preceding with zero number. |
|
ddd |
Displays abbreviated name of the specified date. |
|
dddd (or more) |
Displays full name of the specified date. |
|
MM |
Displays the specified month (two digits). |
|
MMM |
Displays abbreviated name of the specified month. |
|
MMMM (or more) |
Displays full name of the specified month. |
|
t |
Displays specified date and time (short form). |
|
f |
Displays specified date and time (long form, time format excluding second digits). |
|
F |
Displays specified date and time (long form, time format including second digits). |
|
yy |
Displays two digits number of the specified year, the first two digits are omitted. |
|
yyyy |
Displays the specified year. |
|
HH |
Displays hour for the specified DateTime in am/pm format. |
|
hh |
Displays hour for the specified DateTime in 24 hours format. |
|
mm |
Displays minute for the specified DateTime (range: 0-59). |
|
ss |
Displays second for the specified DateTime (range: 0-59). |
Please refers to the .NET documentation for the more complete date time formatting, I exclude several formats here.
The above formatting is done using DateTime.ToString() method. ToString method from DateTime class has four overloads. One of them receives string as a parameter and then we can do date time formatting like the following (written in C#):
using System;
namespace Learning_Materials
{
class SimpleDateTimeFormats
{
static void Main(string[] args)
{
DateTime date = DateTime.Now;
// print out the date time using default format
Console.WriteLine(date.ToString());
// otherwise, specify the other formats
Console.WriteLine(date.ToString(“d”));
Console.WriteLine(date.ToString(“dd/MM/yyyy”)); // Indonesian format (date/month/year).
Console.WriteLine(date.ToString(“dd-MMMM-yyyy hh:mm:ss”)); // Indonesian format (date-month(fullname)- year hour(24 hours):minutes:seconds)
Console.WriteLine(date.ToString(“MM/dd/yyyy”)); // en-us format (month/date/year).
Console.WriteLine(date.ToString(“MMMM dd, yyyy HH:mm:ss”)); // en-us format (month date, year hours(am/pm format):minutes:seconds).
}
}
}
Here we go, sometimes missing the date time format can be so sucks. I ever do this mistake when I must insert user input in string format to the MS SQL Server database. the input was in date/month/year format (standard format for Indonesian date) but the SQL Server format was month/date/year, finally the exception happen.
Hopefully that I will found more interesting things in my coding experience and I can learn more from my mistakes (smile face). Regards and see you later.
Tri Sugiyantowo (triaslama)
December 15, 2007 at 2:19 pm
Nice article. The code could probably use some indenting (have you tried the html “code” tag coupled with spaces in the code?)
May 16, 2008 at 1:25 pm
HH is 24h format!!
October 3, 2008 at 1:53 am
thx for the post. it’s help me.