Date & Time Manipulations

 To store date and time values, you require creating an object for “System.DateTime” class.
 The object for “System.DateTime” class, can hold date and time value in US format:
DateFormat: mm/dd/yyyy hh:mi:ss AM/PM

Object Implementation

 Create an instance for “System.DateTime” class:
DateTime dtobj;
 Assign any date&time value:
dtobj = Convert.ToDateTime(“mm/dd/yyyy hh:mi:ss AM/PM”);
 Assign System date&tme:
dtobj = DateTime.Now;

Property Description Syntax
Day Gets the day (dd) dtobj.Day
Month Gets the month (mm) dtobj.Month
Year Gets the year (yy) dtobj.Year
Hour Gets the hour (hh) (in 24-hours format) dtobj.Hour
Minute Gets the minute (mi) Dtobj.Minute
Second Gets the second (ss) dtobj.Second
MilliSecond Gets the milli second dtobj.MilliSecond
DayOfYear Gets the index of the current day, in the year (0 to 366) dtobj.DayOfYear
DayOfWeek Gets the current week name (Sunday, Monday,…,Saturday) dtobj.DayOfWeek

Methods of “DateTime” Class

Method Description Syntax
ToString() Returns the date and time value as string in following format.
mm/dd/yyy hh:mi:ss AM/PM
dtobj.ToString()
ToShortDateString() Returns the date value as string in the following format.
mm/dd/yyyy
dtobj.ToShortDateString()
ToLongDateString() Returns the date value as string in the following format. Day, Month dd, yyyy dtobj.ToLongDateString()
ToShortTimeString() Returns the time value as string in the following format. hh:mi AM/PM dtobj.ToShortTimeString()
ToLongTimeString() Returns the date value as string in the following format. hh:mi:ss AM/PM dtobj.ToLongTimeString()
AddDays() Adds the given no. of days to the date object and returns it. dtobj.AddDays(n)
AddMonths() Adds the given no. of months to the date object and returns it. dtobj.AddMonths(n)
AddYears() Adds the given no. of years to the date object and returns it. dtobj.AddYears(n)
AddHours() Adds the given no. of hours to the date object and returns it. dtobj.AddHours(n)
AddMinutes() Adds the given no. of minutes to the date object and returns it. dtobj.AddMinutes(n)
AddSeconds() Adds the given no. of seconds to the date object and returns it. dtobj.AddSeconds(n)
AddMilliSeconds() Adds the given no. of milli seconds to the date object and returns it. dtobj.AddMilliSeconds(n)
Static Properties of “DateTime” Class

Property Description DateTime.Now
Now Gets the current system date and time DateTime.Now


Static Methods of “DateTime” Class

Property Description Syntax
IsLeapYear() Returns a bool value, that indicates whether the given year is a leap year or not. DateTime.IsLeapYear(year)
DaysInMonth() Returns the no. of days in the given month, based on the given year. DateTime.DaysInMonth(year, month);


Date Comparisons  Date comparisons are very simple in C#, because “DateTime” overloads the necessary relational operators such as “==”, “!=”, “<”, “>”, “<=”, “>>=”.
 You can directly use these operators between two dates and get the appropriate result.

Date Difference  To find out the difference between two dates, simply use “-“ operator, which is already overloaded.
 When you use this, you can get the result as an “System.TimeSpan” object.

“DateTime” Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DateTimeDemo
{
class Program
{
static void Main(string[] args)
{
//create DateTime obj
DateTime dt1;
dt1 = Convert.ToDateTime("7/16/2009 6:23 PM");
DateTime dt2 = DateTime.Now;
//display dates
Console.WriteLine("dt1 is: " + dt1.ToString());
Console.WriteLine("dt2 is: " + dt2.ToString());

//display individual values
Console.WriteLine("\nIndividual values of dt2:");
Console.WriteLine("dd: " + dt2.Day);
Console.WriteLine("mm: " + dt2.Month);
Console.WriteLine("yyyy: " + dt2.Year);
Console.WriteLine("hh: " + dt2.Hour);
Console.WriteLine("mi: " + dt2.Minute);
Console.WriteLine("ss: " + dt2.Second);
Console.WriteLine("ms: " + dt2.Millisecond);

//implement user-defined date formats
string IndianFormattedDate = dt2.Day + "-" + dt2.Month + "-" + dt2.Year;
Console.WriteLine("\nDate in indian format: " + IndianFormattedDate);

//display the day index
Console.WriteLine("\nDay of year: " + dt2.DayOfYear);
Console.WriteLine("\nDay of week: " + dt2.DayOfWeek);

//long and short formatted date and time
Console.WriteLine("\nShort date format: " + dt2.ToShortDateString());
Console.WriteLine("\nLong date format: " + dt2.ToLongDateString());
Console.WriteLine("\nShort time format: " + dt2.ToShortTimeString());
Console.WriteLine("\nLong time format: " + dt2.ToLongTimeString());

//adding values
DateTime dt3 = dt2.AddDays(5);
Console.WriteLine("\ndt2 after adding 5 days: " + dt3.ToShortDateString());
DateTime dt4 = dt2.AddMonths(5);
Console.WriteLine("\ndt2 after adding 5 months: " + dt4.ToShortDateString());
DateTime dt5 = dt2.AddYears(5);
Console.WriteLine("\ndt2 after adding 5 years: " + dt5.ToShortDateString());
DateTime dt6 = dt2.AddHours(5);
Console.WriteLine("\ndt2 after adding 5 hours: " + dt6.ToLongTimeString());
DateTime dt7 = dt2.AddMinutes(5);
Console.WriteLine("\ndt2 after adding 5 minutes: " + dt7.ToLongTimeString());
DateTime dt8 = dt2.AddSeconds(5);
Console.WriteLine("\ndt2 after adding 5 seconds: " + dt8.ToLongTimeString());

//check whether this is leap year or no
if (DateTime.IsLeapYear(dt2.Year))
Console.WriteLine("\nThis is a leap year.");
else
Console.WriteLine("\nThis is not a leap year.");

//get the total no. of days in this month
Console.WriteLine("\nNo. of days in this month: " + DateTime.DaysInMonth(dt2.Year, dt2.Month));

//date comparision
if (dt1 == dt2)
Console.WriteLine("\ndt1 and dt2 are equal.");
else if (dt1 > dt2)
Console.WriteLine("\ndt1 is greater than dt2.");
else if (dt1 < dt2)
Console.WriteLine("\ndt1 is less than dt2.");

//date difference
TimeSpan ts = dt2 - dt1;
Console.WriteLine("\nThe difference between dt2 and dt1 is: " + ts.Days + " days.");

Console.Read();
}
}
}
DAte Time

The “System.Random” class

 This class is used to generate a random number, at any time.
 In future, while performing difference programming logics, this random number generation concept may be required.

Implementation:
 Create an instance:
Random r = new Random();  Generate the random number based on the given boundary values:
r.Next(min value, max value);


“Random” Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RandomDemo
{
class Program
{
static void Main(string[] args)
{
Random r = new Random();
for (int i = 0; i < 300; i++)
{
Console.Write(r.Next(1, 300) + ", ");
}
Console.Read();
}
}
}
Ramdom Classes