Using .NET's DateTime object
By Donny Mack
Published: 12/6/2000
Reader Level: Beginner
Rated: This article has not yet been rated.
Be the first to rate it!
Tell a Friend
Rate this Article
Printable Version
Discuss in the Forums

Using .NET's DateTime object

This article is more informational than how to. I have run into a couple people who where having a hard time with the DateTime object. So I put together a kind of quick reference, here's how you do it, article on the DateTime object.

The topics I cover are:

  1. Constructing the DateTime object
  2. Getting data from the DateTime object
  3. Formatting the DateTime Object

The article itself is going to be fairly short. It is easier to see the code in action rather than explain it.

Creating the DateTime object can be done in a number of different ways. If you are reading this article I am assuming you either have the .NET SDK or VisualStudio.NET. You can read about constructing a DateTime object by putting the address below in your browser or clicking Here

ms-help://MSDNVS/cpref/html_hh2/frlrfsystemdatetimeclassctortopic.htm

This is the point that I think a lot of people get confused (constructing the DateTime object). Well I believe a majority of people are only interested in getting the time at a particular moment, not constructing a time object for a future or later date. So to get the present Date and Time you can simply use the following Code:

DateTime dToday = DateTime.Now;

Additionally, you have the option to get "Today", which get's the Date and Time is set to 12am

DateTime dToday = DateTime.Today;

You now have a particular moment in time in a variable named dToday and can get all sorts of information out of it, and you can format it in tons of different ways. Listing 1.1 illustrates some of the many ways to get information out of the DateTime object and how to format it.

Listing 1.1 Information obtained from DateTime

The Following Link displays the same information seen in Listing 1.1, but additionally gives the code sample of how I obtained that data, including the formatting of the date directly below each.

Sample Here

For more information on formatting characters see Table 1.1 and Table 2.2. See Listing 1.2 for information on how to apply the formatting options

Table 1.1 - Standard Format Characters
Format CharacterFormat Pattern
dMM/dd/yyyy
Ddddd, MMMM dd, yyyy
fdddd, MMMM dd, yyyy HH:mm
F dddd, MMMM dd, yyyy HH:mm:ss
g MM/dd/yyyy HH:mm
G MM/dd/yyyy HH:mm:ss
m, M MMMM dd
r, R ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
s yyyy-MM-dd HH:mm:ss
t HH:mm
T HH:mm:ss
u yyyy-MM-dd HH:mm:ss
U dddd, MMMM dd, yyyy HH:mm:ss
y, Y MMMM, yyyy

Table 1.2 - List of patterns you can use to create custom formatting
Format PatternDescription
d The day of the month. Single-digit days will not have a leading zero.
dd The day of the month. Single-digit days will have a leading zero.
ddd The abbreviated name of the day of the week
dddd The full name of the day of the week
M The numeric month. Single-digit months will not have a leading zero.
MM The numeric month. Single-digit months will have a leading zero.
MMM The abbreviated name of the month
MMMM The full name of the month
y The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero.
yy The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
yyyy The year including the century in four digits.
gg The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
h The hour in a 12-hour clock. Single-digit hours will not have a leading zero.
hh, hh* The hour in a 12-hour clock. Single-digit hours will have a leading zero.
H The hour in a 24-hour clock. Single-digit hours will not have a leading zero.
HH, HH* The hour in a 24-hour clock. Single-digit hours will have a leading zero.a
m The minute. Single-digit minutes will not have a leading zero.
mm, mm* The minute. Single-digit minutes will have a leading zero.
s The second. Single-digit seconds will not have a leading zero.
ss, ss* The second. Single-digit seconds will have a leading zero.
t The first character in the AM/PM designator
tt, tt* The AM/PM designator
z The timezone offset (hour only). Single-digit hours will not have a leading zero.
zz The timezone offset (hour only). Single-digit hours will have a leading zero.
zzz, zzz* The full timezone offset (hour and minutes). Single-digit hours and minutes will have leading zeros.
: The default time separator
/ The default date separator
% c Where c is a standard format character. Displays the standard format pattern associated with the format character.
\ c Where c is any character. Displays the character literally.

Listing 1.2 Using DateTime.Format for most situations.

DateTime.Format("FormatCharacters", null);



Marketplace
(Sponsored Links)
What are the green links?
   



 
Copyright © 2007 CMP Tech LLC |
Privacy Policy (4/10/06) | Your California Privacy Rights (4/10/06) | Terms of Service | Advertising Info | About Us | Help