BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

DateTime Manipulation

The twenty-seventh part of the C# Fundamentals tutorial completes the examination of the DateTime data type provided by C# and the .NET Framework. This article considers manipulation of DateTime data and formatting date and time information as a string.

Using ToString with Format Specifiers

The basic formatting provided by the above conversion methods is useful in many cases. For greater control, using the ToString method allows you to specify a format specifier. This permits the use of a wider range of date and time styles. It does, however, limit the user's control over how they view an application's output.

DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30");
string result;

result = theDate.ToString("d");       // result = "03/01/2007"
result = theDate.ToString("f");       // result = "03 January 2007 21:25"
result = theDate.ToString("y");       // result = "January 2007"

Available Format Specifiers

The full list of format specifiers for DateTime conversion is as follows:

SpecifierDescriptionExample
dShort date format. This is equivalent to using ToShortDateString."03/01/2007"
DLong date format. This is equivalent to using ToLongDateString."03 January 2007"
fDate and time using long date and short time format."03 January 2007 21:25"
FDate and time using long date and time format."03 January 2007 21:25:30"
gDate and time using short date and time format."03/01/2007 21:25"
GDate and time using short date and long time format."03/01/2007 21:25:30"
mDay and month only."03 January"
rDate and time in standard Greenwich Mean Time (GMT) format."Wed, 03 Jan 2007 21:25:30 GMT"
sSortable date and time format. The date elements start at the highest magnitude (year) and reduce along the string to the smallest magnitude (seconds)."2007-01-03T21:25:30"
tShort time format. This is equivalent to using ToShortTimeString."21:25"
TLong time format. This is equivalent to using ToLongTimeString."21:25:30"
uShort format, sortable co-ordinated universal time."2007-01-03 21:25:30Z"
ULong format date and time."03 January 2007 17:25:30"
yMonth and year only."January 2007"

Using Picture Formats for Custom Formatting

The standard format specifiers are useful in most cases. On some occasions however, it is necessary to have complete control over the positioning and styling of each element of date and time information. For this, you can use picture formats, allowing an exact format to be created using any combination of elements from a DateTime.

DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30");
string result;

result = theDate.ToString("d-MM-yy");       // result = "3-01-07"
result = theDate.ToString("HH:mm");         // result = "21:25"
result = theDate.ToString("h:mm tt");       // result = "9:25 PM"

Available Picture Formatting Codes

The above example shows several formats and the results. The full list of formatting codes is as follows:

SpecifierDescriptionExamples
yOne-digit year. If the year cannot be specified in one digit then two digits are used automatically."7"
"95"
yyTwo-digit year with leading zeroes if required."07"
yyyyFull four-digit year."2007"
g or ggIndicator of Anno Domini (AD)."A.D."
MOne-digit month number. If the month cannot be specified in one digit then two digits are used automatically."1"
"12"
MMTwo-digit month number with leading zeroes if required."01"
MMMThree letter month abbreviation."Jan"
MMMMMonth name."January"
dOne-digit day number. If the day cannot be specified in one digit then two digits are used automatically."3"
"31"
ddTwo-digit day number with leading zeroes if required."03"
dddThree letter day name abbreviation."Wed"
ddddDay name."Wednesday"
hOne-digit hour using the twelve hour clock. If the hour cannot be specified in one digit then two digits are used automatically."9"
"12"
hhTwo-digit hour using the twelve hour clock with leading zeroes if required."09"
HOne-digit hour using the twenty four hour clock. If the hour cannot be specified in one digit then two digits are used automatically."1"
"21"
HHTwo-digit hour using the twenty four hour clock with leading zeroes if required."09"
tSingle letter indicator of AM or PM, generally for use with twelve hour clock values."A"
"P"
ttTwo letter indicator of AM or PM, generally for use with twelve hour clock values."AM"
"PM"
mOne-digit minute. If the minute cannot be specified in one digit then two digits are used automatically."1"
"15"
mmTwo-digit minute with leading zeroes if required."01"
sOne-digit second. If the second cannot be specified in one digit then two digits are used automatically."1"
"59"
ssTwo-digit second with leading zeroes if required."01"
fFraction of a second. Up to seven f's can be included to determine the number of decimal places to display."0"
"0000000"
zOne-digit time zone offset indicating the difference in hours between local time and UTC time. If the offset cannot be specified in one digit then two digits are used automatically."+6"
"-1"
zzTwo-digit time zone offset indicating the difference in hours between local time and UTC time with leading zeroes if required."+06"
zzzFull time zone offset indicating the difference in hours and minutes between local time and UTC time with leading zeroes if required."+06:00"

Some of the formatting codes use the same letter as a format specifier. When used within a format string that is greater than one character in length this does not present a problem. If you need to use a single character picture format string, the letter must be preceded by a percentage sign (%) to indicate that the standard format specifier is not to be used.

DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30");
string result;

result = theDate.ToString("d");       // result = "03/01/2007"
result = theDate.ToString("%d");      // result = "3"

Using String.Format

In the article, C# String Generation with String.Format I described the use of the String.Format method for generating strings containing formatted numeric information. This method may also be used for including DateTime values in strings.

3 January 2007