Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Formatting a DateTime for display - format string description

Rate me:
Please Sign up or sign in to vote.
4.86/5 (12 votes)
26 Jan 2010CPOL1 min read 55.1K   7   7
Normally I keep information like this in a text file, in a folder called "How To Do It", as it is handy to have around, and too much work to search for each time I need it, but too much detail for me to remember if I don't use it often. Formatting a DateTime for display:DateTime.ToString()...

Normally I keep information like this in a text file, in a folder called "How To Do It", as it is handy to have around, and too much work to search for each time I need it, but too much detail for me to remember if I don't use it often.

Formatting a DateTime for display:
DateTime.ToString() returns a displayable date, in the form of "26/01/2010 11:36:17", but this is rarely what you want.
However, ToString takes a string parameter which specifies exactly what information to convert. This document details the string contents:
Short form strings. Note these ARE culture dependant, and will take acount of the current settings. So if the user time display setting is 24 hours:

Str      Result
d	26/01/2010
D	26 January 2010
f	26 January 2010 11:47
F	26 January 2010 11:47:10
g	26/01/2010 11:47
G	26/01/2010 11:47:10
m	26 January
M	26 January
r	Tue, 26 Jan 2010 11:47:10 GMT
R	Tue, 26 Jan 2010 11:47:10 GMT
s	2010-01-26T11:47:10
u	2010-01-26 11:47:10Z
U	26 January 2010 11:47:10
y	January 2010
Y	January 2010

But if it is 12 hours, with AM/PM:

Str      Result
d	26/01/2010
D	26 January 2010
f	26 January 2010 11:57
F	26 January 2010 11:57:12 AM
g	26/01/2010 11:57
G	26/01/2010 11:57:12 AM
m	26 January
M	26 January
r	Tue, 26 Jan 2010 11:57:12 GMT
R	Tue, 26 Jan 2010 11:57:12 GMT
s	2010-01-26T11:57:12
u	2010-01-26 11:57:12Z
U	26 January 2010 11:57:12 AM
y	January 2010
Y	January 2010

Notes:
1) It is generally better to customise your own string if you need to fit the date / time into a specific space.
2) The "s" format is to ISO 8601, and is compatible with SQL / MySQL. You should not need to convert from a DateTime to string to access databases however. Construct a parameterized query and use the DateTime object directly.

The following details the meaning of each pattern character you can use in a custom format string:
Time:

Str     Result
h       12-hour clock hour (e.g. 11, or 3) 
hh      12-hour clock, with a leading 0 if required (e.g. 11, or 03) 
H       24-hour clock hour (e.g. 14, or 3) 
HH      24-hour clock hour, with a leading 0 if required (e.g. 14, or 03) 
m       Minutes 
mm      Minutes with a leading zero if required 
s       Seconds 
ss      Seconds with leading zero if required
f       Represents the most significant digit of the seconds. I.e. tenths of a second in a date and time value.  
F       Represents the most significant digit of the seconds. I.e. tenths of a second in a date and time value. Nothing is displayed if the digit is zero.  
ff      Represents the two most significant digits of the seconds. I.e. hundredths of a second in a date and time value. 
fff     Represents the three most significant digits of the seconds. I.e. milliseconds in a date and time value. 
ffff    Represents the four most significant digits of the seconds.
fffff   Represents the five most significant digits of the seconds.
ffffff  Represents the six most significant digits of the seconds.
fffffff Represents the seven most significant digits of the seconds. 
t       Abbreviated AM / PM (e.g. A or P) 
tt      AM / PM (e.g. AM or PM) 

Notes: The time is dependant on the resolution of the system clock, so extreme decimal places of a second are to be treated with considerable suspicion! Windows clock resolution is 64Hz...
Date:

Str      Result
d       Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero 
dd      Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero 
ddd     Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc) 
dddd    Represents the full name of the day of the week (Monday, Tuesday etc) 
M       Month number 
MM      Month number with leading zero 
MMM     Abbreviated Month Name (e.g. Dec) 
MMMM    Full month name (e.g. December) 
y       Year, no leading zero (e.g. 2001 would be 1) 
yy      Year, leading zero (e.g. 2001 would be 01) 
yyy     Year, (e.g. 2001 would be 2001) 
yyyy    Year, (e.g. 2001 would be 2001) 

Time Zone information:

Str	Result
K       Represents the time zone information of a date and time value (e.g. +07:00) 
z       With DateTime values, represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +8) 
zz      As z but with leading zero (e.g. +08) 
zzz     With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +08:00) 

Separators and specials:

Str	Result
:       Represents the time separator defined in the current DateTimeFormatInfo.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.  
/       Represents the date separator defined in the current DateTimeFormatInfo.DateSeparator property. This separator is used to differentiate years, months, and days.  
"       Represents a quoted string. Displays the literal value of any string between two double quote marks.  
'       Represents a quoted string. Displays the literal value of any string between two single quote characters.  
%       Represents the result associated with a short form string as listed above. E.g. "%d" would give "26/01/2010"
\       Escape character.

Any other character is transfered over "as-is".

History

2016-07-20 Typos. Thanks Brisingr Aerowing!

2010-01-26 Original version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
BugTypos. Pin
Brisingr Aerowing19-Jul-16 13:48
professionalBrisingr Aerowing19-Jul-16 13:48 
GeneralRe: Typos. Pin
OriginalGriff19-Jul-16 22:46
mveOriginalGriff19-Jul-16 22:46 
GeneralMy vote of 5 Pin
Renju Vinod2-Jul-13 23:05
professionalRenju Vinod2-Jul-13 23:05 
GeneralMy vote of 5 Pin
shaikh-adil10-Dec-12 17:02
shaikh-adil10-Dec-12 17:02 
GeneralMy 5+ i m working on the project of one legal firm....work w... Pin
NikulDarji20-Dec-11 3:13
NikulDarji20-Dec-11 3:13 
GeneralRe: Glad to be of service! Pin
OriginalGriff20-Dec-11 3:41
mveOriginalGriff20-Dec-11 3:41 
GeneralReason for my vote of 5 VERY GOOD Pin
BELHAJ Yosri30-Sep-10 1:16
BELHAJ Yosri30-Sep-10 1:16 
Reason for my vote of 5
VERY GOOD

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.