Asp.Net C# VB.Net Date Time Formatting Reference List

Always we struggle to Format Date Time in our Asp.Net C# or VB.Net applications for different customers. Because different customers need different Date Time Format. And it’s not so easy to memorize all possible format’s. That’s why in this article I am giving you a Reference List of Date Time Formats or Cheat Sheet for Dates and Times in C# & VB.Net.

Format Description Example Output
d Short Date 28/09/2014
D Long Date Sunday, September 28, 2014
t Short Time 21:08
T Long Time 19:07:39
f Full date and time 28 September 2014 19:07
F Full date and time (long) 28 September 2014 19:07:39
g Default date and time 09/09/2014 19:07
G Default date and time (long) 09/04/2014 19:07:37
M Day / Month 28 September
r RFC1123 date Sun, 28 Sep 2014 19:07:37 GMT
s Sortable date/time 2014-07-08T19:07:37
u Universal time, local timezone 2014-07-08 19:07:59Z
Y Month / Year September 2014
dd Day 28
ddd Short Day Name Sun
dddd Full Day Name Sunday
hh 2 digit hour 07
HH 2 digit hour (24 hour) 19
mm 2 digit minute 09
MM Month 09
MMM Short Month name Sep
MMMM Month name September
ss seconds 39
fff milliseconds 110
FFF milliseconds without trailing zero 11
tt AM/PM AM
yy 2 digit year 09
yyyy 4 digit year 2014
: Hours, minutes, seconds separator, e.g. {0:hh:mm:ss} 07:37:59
/ Year, month , day separator, e.g. {0:dd/MM/yyyy} 28/09/2014

Now How to Use the Above Date Time Format:
Using Date Time Format from Server Tag:

<%= String.Format("{0:Format}", DateTime.Now)%>
Example1 (Using Server Tag):<br />
<%= String.Format("{0:dd/MM/yyyy}", DateTime.Now)%>
<br /><br />Example2 (Using Server Tag):<br />
<%= String.Format("{0:dd/MM/yyyy hh:mm:ss tt}", DateTime.Now)%>

Using Date Time Format from Code Behind:

DateTime.Now.ToString("Format")

To do the Example Add a Label Like Below:

<asp:Label ID="lbl_Format_Date" runat="server" Text=""></asp:Label>

C# Date Time Format Code Example:

    protected void Page_Load(object sender, EventArgs e)
    {
        lbl_Format_Date.Text = "Example1 (From Code Behind):<br />"+ DateTime.Now.ToString("D");
        lbl_Format_Date.Text += "<br /><br />Example2 (From Code Behind):<br />" + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    }

C# Date Time Format Code Example:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        lbl_Format_Date.Text = "Example1 (From Code Behind):<br />" + DateTime.Now.ToString("D")
        lbl_Format_Date.Text += "<br /><br />Example2 (From Code Behind):<br />" + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")
    End Sub

Sample Output:
C# VB.Net Date Time Formatting

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*