C#.Web gives DateTime construction that provides many helpful strategies and properties for date and time calculations. The vary of DateTime is between 01/01/0001 12:00:00 AM to 31/12/9999 11:59:59 PM.
DateTime return numerous date/time components:
[csharp]
DateTime d1 = DateTime.Now;
listBox1.Objects.Add(“Yeartt: ” + d1.12 months);
listBox1.Objects.Add(“Monthtt: ” + d1.Month);
listBox1.Objects.Add(“Daytt: ” + d1.Day);
listBox1.Objects.Add(“DayOfWeekt: ” + d1.DayOfWeek);
listBox1.Objects.Add(“DayOfYeart: ” + d1.DayOfYear);
listBox1.Objects.Add(“Hourtt: ” + d1.Hour);
listBox1.Objects.Add(“TimeOfDayt: ” + d1.TimeOfDay);
listBox1.Objects.Add(“Minutett: ” + d1.Minute);
[/csharp]
Output:
Formatting and parsing date and time
Following are other ways for Formatting and parsing date and time.
[csharp]
listBox1.Objects.Add(DateTime.Now.ToString());
listBox1.Objects.Add(DateTime.Now.ToShortDateString());
listBox1.Objects.Add(DateTime.Now.ToLongDateString());
listBox1.Objects.Add(DateTime.Now.ToShortTimeString());
listBox1.Objects.Add(DateTime.Now.ToLongTimeString());
[/csharp]
Output:
Including or Subtracting from Dates and Occasions
The C# DateTime gives a variety of strategies for including or subtracting date and occasions from a DateTime occasion.
Substracting days from present date.
[csharp]
DateTime dt = DateTime.As we speak.AddDays(-2);
string strDate = dt.ToString(“dd / MM / yy”);
listBox1.Objects.Add(strDate);
[/csharp]
Add days to the present date.
[csharp]
DateTime tomorrow = DateTime.As we speak.AddDays(1);
string strDate = tomorrow.ToString(“dd / MM / yy”);
listBox1.Objects.Add(strDate);
[/csharp]
Calculating the time after 30 minute (including minutes to the present time.)
[csharp]
DateTime later = DateTime.Now.AddMinutes(30);
string strDate = later.ToString(“dd / MM / yy, hh : mm “);
listBox1.Objects.Add(strDate);
[/csharp]
output:
To calculate complete variety of days between two dates
To get the times in between two dates in DateTime format, we have to Use TimeSpan object which is the results of date subtraction.
[csharp]
DateTime d1 = DateTime.Now.AddDays(6);
DateTime d2 = DateTime.Now.AddDays(-2);
TimeSpan t = d1 – d2;
double diff = t.TotalDays;
[/csharp]
To get the times in between two dates in DateTime format, we have to Use TimeSpan object which is the results of date subtraction.
Supply techzoo.org