[edit] Using the DateTime.AddDays Method
To use the Datetime AddDays method you will not specify a timespan. You must specify the amount of days you want to add. It accepts a double value so it can calculate partial days. Remember, a timespan must NOT be used with DateTime.AddDays.
[edit] Syntax
DateTime.AddDays(<double>)
[edit] Example 1:
using System;
class Program
{
static void Main()
{
DateTime today = DateTime.Now;
Console.WriteLine("today: " + today.ToString());
today = today.AddDays(3);
Console.WriteLine("New Date: " + today.ToString());
Console.Read();
}
}
Output:
today: 4/4/2007 9:46:40 PM
New Date: 4/7/2007 9:46:40 PM
As you can see the AddDays method simply added one Day to the current date.
[edit] Example 2:
This example will add partial days.
using System;
class Program
{
static void Main()
{
DateTime today = DateTime.Now;
Console.WriteLine("today: " + today.ToString());
today = today.AddDays(3.7);
Console.WriteLine("New Date: " + today.ToString());
Console.Read();
}
}
Output:
today: 4/5/2007 7:57:10 PM
New Date: 4/7/2007 12:10:45 PM
As you can see the program added 3 days and is also added the appropriate hours, minutes and seconds to the time part.
<-- DateTime Tutorial
|