Operations on Dates |
|
The Leap Year |
One of the operations performed on date values is to find out whether the year value of a date is a leap year. Fortunately, the static IsLeapYear() method of the DateTime structure can be used to perform this operation. The syntax of this function is: public static bool IsLeapYear(int year); This method takes an integer argument and examines it. If the argument, which must be a valid year number, is a leap year, the method returns true; otherwise, it would return false. Here are two examples: |
using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(1988, 10, 6); Console.WriteLine("{0} is a leap year: {1}", date.Year, DateTime.IsLeapYear(date.Year)); date = new DateTime(1990, 8, 12); Console.WriteLine("{0} is a leap year: {1}", date.Year, DateTime.IsLeapYear(date.Year)); return 0; } } } This would produce: 1988 is a leap year: True 1990 is a leap year: False Press any key to continue . . .
The DateTime structure is equipped with a property that can be used to retrieve the day of the week for a given date. The property is named DayOfWeek. To get the day of the week, access the DayOfWeek property a your DateTime value or variable. The values are stored in the DayOfWeek enumeration. Here is an example: using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(1988, 10, 6); Console.WriteLine("Day of the week: {0}", date.DayOfWeek); return 0; } } } This would produce: Day of the week: Thursday Press any key to continue . . .
Dates are some of the most important values of the operating system and many applications need them to perform their routine operations. Common operations include adding days to a date, subtracting months from a date, adding years, comparing dates to find out whether one occurs before another, finding out if a payroll falls on a holiday, etc. To perform some of these operations, a unit called an interval is considered as part of a date. A date interval is the number of days, months, or years that have elapsed, or would elapse, from one starting date to another ending date. Once you have specified this interval, you can then apply it to a date with the operation of your choice. To support the ability to perform operations on dates, the .NET Framework is provides the TimeSpan structure. This structure is equipped with various members that would be applied on the TimeSpan structure. The value produced by this structure is then applied to a DateTime object. To create a date interval, declare a TimeSpan variable and initialize it with the desired value(s). To make this possible, the TimeSpan structure is equipped with various constructors.
With the DateTime structure, you can add some days to an existing date or subtract some days from a date value. To support this, the DateTime structure is equipped with the AddDays() method. Its syntax is: public DateTime AddDays(int days); Here is an example of calling this method: using System; namespace DateAndTime { class Program { static int Main() { DateTime startDate = new DateTime(1988, 10, 6); Console.WriteLine("Starting Date: {0}", startDate); // This will be used to add 8 days to the previous date DateTime endDate = startDate.AddDays(8); // And display it Console.WriteLine("Ending Date: {0}\n", endDate); return 0; } } } This would produce: Starting Date: 10/6/1988 12:00:00 AM Ending Date: 10/14/1988 12:00:00 AM Press any key to continue . . . If you pass a positive value to the DateTime.AddDays() method, a number of days is added to the DateTime variable. If you want to subtract some days from a date, pass the argument with a negative value. This method is able to figure out the year before, the month before, the day before, the day after, the month after, and the year after the designated date. One of the constructors of the TimeSpan structure takes four arguments. Its syntax public TimeSpan(int days, int hours, int minutes, int seconds); The first argument of this constructor represents the number of days. If you are planning to add a number of days to a date, pass a positive integer as the first argument. If you want to subtract a date, pass a negative value. If you want to add only a number of days, pass only this argument. After creating a TimeSpan object, you can call the Add() method of the DateTime structure to a date value. This would produce a new DateTime object that is the resulting value. Here are examples: using System; namespace DateAndTime { class Program { static int Main() { DateTime startDate = new DateTime(1988, 10, 6); Console.WriteLine("Starting Date: {0}\n", startDate); // This will be used to add 172 days to the previous date TimeSpan ts = new TimeSpan(172, 0, 0, 0); // Pass the TimeSpan object to the date DateTime endDate = startDate.Add(ts); // And display it Console.WriteLine("Ending Date: {0}\n", endDate); // Subtract 10 days to the previous date ts = new TimeSpan(-10, 0, 0, 0); // Pass the TimeSpan object to the date endDate = startDate.Add(ts); // And display it Console.WriteLine("Ending Date: {0}\n", endDate); return 0; } } } This would produce: Starting Date: 10/6/1988 12:00:00 AM Ending Date: 3/27/1989 12:00:00 AM Ending Date: 9/26/1988 12:00:00 AM Press any key to continue . . . Notice that, when adding or subtracting days, the complier can figure out the previous day, the previous month, the previous year, the following day, the following month, or the following year.
To add a number of months to a date, the DateTime class is equipped with a method named AddMonths(). Its syntax is: public DateTime AddMonths(int months); This method takes one argument as the number of months to be added or subtracted. If you pass the argument with a positive value, the months are added to the date. If you pass a negative value, the number of months is subtracted from the date.
To get the date a few years before or after a known date, you can add years to, or subtract years from, a known date. To support this operation, the DateTime class provides a method named AddYears(). Its syntax is: public DateTime AddYears(int years); The argument passed to the method is the number of years. A positive value (or a negative value) adds (or subtracts) the number of years to (or from) the date.
Using the logical operators we reviewed for primitive types, you can compare the values of dates for equality, differences, lower or greater values. To support these operations, the DateTime structure have the logical operators configured. To compare two dates, apply the desired Boolean operator the same day you would two variables of primitive types. Here is an example: using System; namespace DateAndTime { class Program { static int Main() { DateTime startDate = new DateTime(1988, 10, 6); Console.WriteLine("Starting Date: {0}", startDate); // This will be used to add 8 days to the previous date DateTime endDate = startDate.AddDays(8); // And display it Console.WriteLine("Ending Date: {0}\n", endDate); if (startDate < endDate) Console.WriteLine("{0} Occurs Before {1}", startDate, endDate); return 0; } } } This would produce: Starting Date: 10/6/1988 12:00:00 AM Ending Date: 10/14/1988 12:00:00 AM 10/6/1988 12:00:00 AM Occurs Before 10/14/1988 12:00:00 AM Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|