DateTime Formatting in C# with Examples

No.of Views3278
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  03 Jul 2010 03:07:52
Tag : CSharp , DateTime Controls
Format the DateTime in C# different ways.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This code snippet, I have described different ways of DateTime formatting in C#. Because every application users, they would like to have date and time different way or format. Here is list of supporting DateTime format strings http://msdn.microsoft.com/en-us/library/az4se3k1%28VS.71%29.aspx

Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DateTimeDemo
{class Program
    {static void Main(string[] args)
        {
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "Char In use ", "Description ", "Out put"));
            Console.WriteLine(string.Format("{0}", "---------------------------------------------------"));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "d", "Short Format", DateTime.Now.ToString("d")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "D", "Long Format", DateTime.Now.ToString("D")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "T", "Long Time", DateTime.Now.ToString("T")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "t", "short Time", DateTime.Now.ToString("t")));

            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "f", "Full Date and time ", DateTime.Now.ToString("f")));

            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "F", "Full date and Time-Long", DateTime.Now.ToString("F")));

            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "G", "Default date and time -Long", DateTime.Now.ToString("G")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "g", "Default date and time", DateTime.Now.ToString("g")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "M", "Day / Month", DateTime.Now.ToString("M")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "r", "RFC1123 date", DateTime.Now.ToString("r")));
           
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "s     ", "Sortable date/time", DateTime.Now.ToString("s")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "u     ", "Universal time, local timezone ", DateTime.Now.ToString("u")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "Y ", "    Month / Year ", DateTime.Now.ToString("Y")));
            
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "ss     ", "seconds ", DateTime.Now.ToString("ss")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "tt     ", "AM/PM ", DateTime.Now.ToString("tt")));


            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "hh     ", "2 digit hour ", DateTime.Now.ToString("hh")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "HH     ", "2 digit hour (24 hour)", DateTime.Now.ToString("HH")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "mm     ", "2 digit minute ", DateTime.Now.ToString("mm")));

            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "dd     ", "Day     08", DateTime.Now.ToString("dd")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "ddd     ", "Short Day Name ", DateTime.Now.ToString("ddd")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "dddd     ", "Full Day Name ", DateTime.Now.ToString("dddd")));

            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "MM     ", "Month     ", DateTime.Now.ToString("MM")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "MMM     ", "Short Month name ", DateTime.Now.ToString("MMM")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "MMMM     ", "Month name ", DateTime.Now.ToString("MMMM")));
           
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "yy     ", "2 digit year ", DateTime.Now.ToString("yy")));
            Console.WriteLine(string.Format("{0}\t{1}\t{2}", "yyyy", "     4 digit year ", DateTime.Now.ToString("yyyy")));
            
           

            Console.ReadLine();
        }
    }
}

 Output

T       Long Time       4:40:26 PM
t       short Time      4:40 PM
f       Full Date and time      Saturday, July 03, 2010 4:40 PM
F       Full date and Time-Long Saturday, July 03, 2010 4:40:26 PM
G       Default date and time -Long     7/3/2010 4:40:26 PM
g       Default date and time   7/3/2010 4:40 PM
M       Day / Month     July 03
r       RFC1123 date    Sat, 03 Jul 2010 16:40:26 GMT
s               Sortable date/time      2010-07-03T16:40:26
u               Universal time, local timezone  2010-07-03 16:40:26Z
Y               Month / Year    July, 2010
ss              seconds         26
tt              AM/PM   PM
hh              2 digit hour    04
HH              2 digit hour (24 hour)  16
mm              2 digit minute  40
dd              Day     08      03
ddd             Short Day Name  Sat
dddd            Full Day Name   Saturday
MM              Month           07
MMM             Short Month name        Jul
MMMM            Month name      July
yy              2 digit year    10
yyyy            4 digit year    2010 

That's all, i hope this is save your time.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top