Custom Date/Time Format Strings
Created by Brad Overlund (Unlicensed)
Last updated: Nov 13, 2013 by Jessica Rivers (Unlicensed)
You can exercise greater control over how a Date/Time is formatted by using custom Date/Time format specifiers to create your own custom Date/Time format string. Combine one or more custom format specifiers to construct a Date/Time formatting pattern that yields the output you prefer.
If a format specifier is used alone as a single character, it is interpreted as a standard format specifier. Only format specifiers consisting of two or more characters are interpreted as custom format specifiers. In order to display the custom format for a specifier defined as both a standard and a custom format specifier, precede the specifier with a % symbol (e.g.: '%d').
Character | Description |
---|---|
d | Displays the current day of the month, measured as a number between 1 and 31, inclusive. If the day is a single digit only (1-9), then it is displayed as a single digit. |
dd | Displays the current day of the month, measured as a number between 1 and 31, inclusive. If the day is a single digit only (1-9), it is formatted with a preceding 0 (01-09). |
ddd | Displays the abbreviated name of the day for the specified Date/Time. |
dddd | Displays the full name of the day for the specified Date/Time. |
f | Displays the most significant digit of the seconds fraction. |
ff | Displays the two most significant digits of the seconds fraction. |
f f | Displays the x most significant digits of the seconds fraction where x is the number of f characters. |
F | Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero. |
FF | Displays the two most significant digits of the seconds fraction. However, trailing zeros, or two zero digits, are not displayed. |
F F | Displays the x most significant digits of the seconds fraction. However, trailing zeros, or x zero digits, are not displayed where x is the number of F characters. |
g | Displays the era (A.D. for example) for the specified Date/Time. |
h | Displays the hour for the specified Date Time in the range 1-12. The hour represents whole hours passed since either midnight (displayed as 12) or noon (also displayed as 12). If this format is used alone, then the same hour before or after noon is indistinguishable. If the hour is a single digit (1-9), it is displayed as a single digit. No rounding occurs when displaying the hour. |
hh | Displays the hour for the specified Date Time in the range 1-12. The hour represents whole hours passed since either midnight (displayed as 12) or noon (also displayed as 12). If this format is used alone, then the same hour before or after noon is indistinguishable. If the hour is a single digit (1-9), it is formatted with a preceding 0 (01-09). |
H | Displays the hour for the specified Date Time in the range 0-23. The hour represents whole hours passed since midnight (displayed as 0). If the hour is a single digit (0-9), it is displayed as a single digit. |
HH | Displays the hour for the specified Date Time in the range 0-23. The hour represents whole hours passed since midnight (displayed as 0). If the hour is a single digit (0-9), it is formatted with a preceding 0 (01-09). |
m | Displays the minute for the specified Date Time in the range 0-59. The minute represents whole minutes passed since the last hour. If the minute is a single digit (0-9), it is displayed as a single digit. |
mm | Displays the minute for the specified Date Time in the range 0-59. The minute represents whole minutes passed since the last hour. If the minute is a single digit (0-9), it is formatted with a preceding 0 (01-09). |
M | Displays the month, measured as a number between 1 and 12, inclusive. If the month is a single digit (1-9), it is displayed as a single digit. |
MM | Displays the month, measured as a number between 1 and 12, inclusive. If the month is a single digit (1-9), it is formatted with a preceding 0 (01-09). |
MMM | Displays the abbreviated name of the month for the specified Date/Time. |
MMMM | Displays the full name of the month for the specified Date/Time. |
o | Displays the current day of the year, measured as a number between 1 and 365 (366 for leap years), inclusive. If the day is a single digit only (1-9), then it is displayed as a single digit. |
oo | Displays the current day of the year, measured as a number between 1 and 365 (366 for leap years), inclusive. If the day is a single digit only (1-9), it is formatted with a preceding 0 (01-09). |
ooo | Displays the current day of the year, measured as a number between 1 and 365 (366 for leap years), inclusive. If the day is 1 or 2 digits only (1-99), it is formatted with 1 or 2 preceding 0's (001-099). |
s | Displays the seconds for the specified Date Time in the range 0-59. The second represents whole seconds passed since the last minute. If the second is a single digit (0-9), it is displayed as a single digit only. |
ss | Displays the seconds for the specified Date Time in the range 0-59. The second represents whole seconds passed since the last minute. If the second is a single digit (0-9), it is formatted with a preceding 0 (01-09). |
t | Displays the first character of the A.M./P.M. designator for the specified Date/Time |
tt | Displays the A.M./P.M. designator for the specified Date/Time. |
y | Displays the year for the specified Date/Time as a maximum two-digit number. The first two digits of the year are omitted. If the year is a single digit (1-9), it is displayed as a single digit. |
yy | Displays the year for the specified Date/Time as a maximum two-digit number. The first two digits of the year are omitted. If the year is a single digit (1-9), it is formatted with a preceding 0 (01-09). |
yyyy | Displays the year for the specified Date/Time, including the century. If the year is less than four digits in length, then preceding zeros are appended as necessary to make the displayed year four digits long. |
z | Displays the time zone offset for the system's current time zone in whole hours only. If the offset is a single digit (0-9), it is displayed as a single digit with the appropriate leading sign. |
zz | Displays the time zone offset for the system's current time zone in whole hours only. If the offset is a single digit (0-9), it is formatted with a preceding 0 (01-09) with the appropriate leading sign. |
zzz | Displays the time zone offset for the system's current time zone in hours and minutes. If the offset is a single digit (0-9), it is formatted with a preceding 0 (01-09) with the appropriate leading sign. |
: | Time Separator |
/ | Date Separator |
Date / Time Format Samples:
Note: All samples assume en-US culture is in use.
Format | Output |
---|---|
d | 4/10/2001 |
D | Tuesday, April 10, 2001 |
t | 3:51 PM |
T | 3:51:24 PM |
f | Tuesday, April 10, 2001 3:51 PM |
F | Tuesday, April 10, 2001 3:51:24 PM |
g | 4/10/2001 3:51 PM |
G | 4/10/2001 3:51:24 PM |
s | 2001-04-10T15:51:24 |
m | April 10 |
y | April, 2001 |
d, M | 10, 4 |
d MMMM | 10 April |
ddd MMM yy | Thur Apr 01 |
ddd MMMM yyyy | Thur April 2001 |
dddd MMMM yy gg | Thursday April 01 A.D. |
h:m:s | 3:51:24 |
hh:mm:ss | 03:51:24 |
HH-mm-ss-t | 15-51-24-P |
HH-mm-ss-tt | 15-51-24-PM |