Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Understanding DateTime and TimeSpan in .Net

Posted by Ravi Varma Thumati on October 20, 2009

//Creating Two Instance of DateTime
DateTime dt1 = new DateTime();
DateTime dt2 = new DateTime();

//Initializing DateTime Object
dt1 = DateTime.Now.Date; //Initializing with Current Date
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 00:00:00

//Examples for DateTime Manipulation in .Net
//Example 1 : Adding Minutes to Current Date.
dt1 = dt1.AddMinutes(5.0);
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 00:05:00

//Example 2 : Adding Seconds to DateTime Object., similarly you can add milliseconds
dt1 = dt1.AddSeconds(30.0);
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 00:05:30

//Example 3 : Adding Hours to DateTime Object in .Net.
dt1 = dt1.AddHours(5.0);
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 05:05:30

//Example 4 : Adding Time to DateTime Object.
//You can replace Example 1,2 and 3 by simply adding TimeSpan with your desired
//TimeSpan value.
//Here we are adding 5 Hours, 5 Minutes and 30 Seconds.
dt1 = dt1.Add(new TimeSpan(5,5,30));
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 10:11:00

//Example 5 : Adding Days to DateTime Object in .Net
dt2 = dt1.AddDays(7.0); //Initializing dt2 object with 7 days ahead from
//dt1 DateTime value.
Response.Write(dt2.ToString() + "<br>");
//Output: 29-06-2007 10:11:00

//Example 6 : Adding Month to DateTime Object
dt2 = dt2.AddMonths(1);
Response.Write(dt2.ToString() + "<br>");
//Output: 29-07-2007 10:11:00

//Example 7 : Adding Year to DateTime Object
dt2 = dt2.AddYears(1);
Response.Write(dt2.ToString() + "<br>");
//Output: 29-07-2008 10:11:00

//Examples of Retrieving DateTime object value.
//Example 1 : Get Day value.
int intDay = dt1.Day;
Response.Write(intDay.ToString() + "<br>");
//Output: 22

//Example 2 : Get Day of Week.
DayOfWeek dow = dt1.DayOfWeek;
Response.Write(dow.ToString() + "<br>");
//Output: Friday
//How to find whether day of week is sunday or saturday?
if(dow == DayOfWeek.Sunday dow == DayOfWeek.Saturday)
{
//Hurray its weekend.
}

//Example 3 : Get the Day of Year
int intYear = dt1.DayOfYear; //Similarly you can get Year value.
Response.Write(intYear.ToString() + "<br>");
//Output: 173

//Similarly you can get Hours, Minutes, Seconds, Milliseconds value.

//Conversion of String to DateTime
//Make use of Parse Method of DateTime to "Convert String to DateTime in .Net"
DateTime dt4 = DateTime.Parse("08/06/2007");
DateTime dt5 = DateTime.Parse("10/06/2007");

//Comparing Date
//How to Find whether both dates are equal or Not?
//How to compare two dates in .Net?
if (DateTime.Compare(dt4, dt5) == 0)
{
Response.Write("Both Dates are Equal" + "<br>");
}
else if (DateTime.Compare(dt4, dt5) < 0)
{
Response.Write(dt4.ToString() + " is Less than "
+ dt5.ToString() + "<br>");
}
else
{
Response.Write(dt4.ToString() + " is Greater than "
+ dt5.ToString() + "<br>");
}
//Output: 08-06-2007 00:00:00 is Less than 10-06-2007 00:00:00
/*Note: You may recieve error: "String was not recognized as a valid
DateTime." This may be occur as in India the DateTime format is followed
as "dd/mm/yyyy" But let say if you are in USA than it follow "mm/dd/yyyy" so here
there is crash situation and so you might recieve above error, so to avoid such
error make sure that user input valid date before you Parse date. */


//Difference between Date
//How to Find difference between two dates in .Net?
//How to Find days difference between two dates in .Net?
//How to subtract one date from another?
TimeSpan tsDiff = dt5.Subtract(dt4); //Note: Always subtract smaller date
//value from greater date
Response.Write("Difference between " + dt4.ToString() + " and " +
dt5.ToString() + " is " + tsDiff.Days.ToString() + " days." + "<br>");
//Output: Difference between 08-06-2007 00:00:00 and 10-06-2007 00:00:00 is 2 days.

//Similarly you can also find:
//How many hour difference between two dates
//How many seconds difference between two dates
//And so on... by changing tsDiff property value to hours, seconds....
//instead of tsDiff.Days.

//Compare Time
//How to Find whether both Time are equal or Not?
//How to compare Time in .Net?
dt4 = DateTime.Parse("4:30 AM");
dt5 = DateTime.Parse("7:30 PM");
if (DateTime.Compare(dt4, dt5) == 0)
{
Response.Write("Both Times are Equal" + "<br>");
}
else if (DateTime.Compare(dt4, dt5) < 0)
{
Response.Write(dt4.ToShortTimeString() + " is Less than "
+ dt5.ToShortTimeString() + "<br>");
}
else
{
Response.Write(dt4.ToShortTimeString() + " is Greater than "
+ dt5.ToShortTimeString() + "<br>");
}
//Output: 04:30:00 is Less than 19:30:00

//Difference between Time
//How to Find difference between two Time value in .Net?
//How to Find Hours difference between two Time in .Net?
//How to subtract one Time from another?
//How to find elapsed Time between Two Time value in .Net?
tsDiff = dt5.Subtract(dt4); //Note: Always subtract smaller date
//value from greater date
Response.Write("Difference between " + dt4.ToString() + " and " + dt5.ToString() + " is " + tsDiff.Hours.ToString() + " Hours." + "<br>");
//Output: Difference between 22-06-2007 04:30:00 and 22-06-2007 19:30:00 is 15 Hours.

//More on DateTime
//How many days in a given month in .Net?
int intDaysInMonth = DateTime.DaysInMonth(2007, 6); //Pass valid year, and month
Response.Write(intDaysInMonth.ToString() + "<br>");
//Output: 30

//How to find whether given year is Leap year or not in .Net?
Response.Write( DateTime.IsLeapYear(2007)); //Pass valid year
//Output: False

//How to find current date and time?
Response.Write("Current Date and Time: " + DateTime.Now + "<br>");
//Output: Current Date and Time: 22-06-2007 11:31:04

//How to find current date?
Response.Write("Current Date: " + DateTime.Today + "<br>");
//Output: Current Date: 22-06-2007 00:00:00
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: