
.NET 1.1+DateTime Information Extraction (2)
The twenty-sixth part of the C# Fundamentals tutorial continues the examination of the DateTime data type provided by C# and the .NET Framework. In this instalment we will consider how information can be compared and extracted from values of this type.
Extraction of Date or Time
It is often necessary to extract just the date or time part from a DateTime value. The date can be obtained from the Date property, which returns a DateTime value with the time part set to midnight.
DateTime theDate = DateTime.Parse("31 Dec 2006 13:14:15");
DateTime dateOnly = theDate.Date; // dateOnly = 31 Dec 2006 00:00:00
The TimeOfDay property returns a TimeSpan value. The TimeSpan structure is designed to hold a period of time, rather than an instant in time signified by a DateTime. It has similar properties to the DateTime structure but holds only days, hours, minutes, seconds and milliseconds. An example of a TimeSpan value is 1.12:05:01 indicating a duration of one day, twelve hours, five minutes and one second.
When using the TimeOfDay property of a DateTime, the returned TimeSpan is populated with the time element only. The Days property is set to zero.
DateTime theDate = DateTime.Parse("31 Dec 2006 13:14:15");
TimeSpan timeOnly = theDate.TimeOfDay; // timeOnly = 0.13:14:15
DayOfWeek Property
The DayOfWeek property allows you to determine the weekday that a particular date falls upon. The property returns an enumerated type value. An enumerated type provides a list of named numbers, in this case the numbers zero to six represent the day names Sunday to Saturday. The DayOfWeek value may be converted to either a number or a string depending upon the desired use.
DateTime theDate = DateTime.Parse("31 Dec 2006");
DayOfWeek day = theDate.DayOfWeek; // day = 0 (Sunday)
int dayNumber = (int)day; // dayNumber = 0
string dayString = day.ToString(); // dayString = "Sunday"
DayOfYear Property
Sometimes a date must be expressed as a simple integer, being the number of days passed since the start of the current year. The DateTime structure allows this via the DayOfYear property. This property returns a day number within the current year. For 1 January the return value is one. For 31 December, the returned value is 365, or 366 for leap years.
DateTime standard = DateTime.Parse("25 March 2006");
DateTime leapyear = DateTime.Parse("25 March 2008");
int day;
day = standard.DayOfYear; // day = 84
day = leapyear.DayOfYear; // day = 85
Determining Calendar Information
Finally, you can use the DateTime class to obtain information about a specific month or year, which is useful when displaying or processing calendar information. The first method to review is DaysInMonth. This accepts year and month numbers as parameters and returns the number of days in that month. The second, IsLeapYear, returns a Boolean value indicating if a given year is a leap year. Both methods are static members.
int days = DateTime.DaysInMonth(2008, 2); // days = 29
bool leap = DateTime.IsLeapYear(2008); // leap = true
1 January 2007