Sunday, March 9, 2014

DateTime Structure

DateTime 

  • Library: System.DateTime
  • To  store  date  and  time  values,  you  require  creating  an  instance  for  DateTime structure.
  • The instance for System.DateTime class, can hold date and time value in US format: Date and time Format: mm/dd/yyyy hh:mi:ss AM/PM

Object Implementation:

  • Create an instance for “System.DateTime” structure:
              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;

 

Date Comparisons

  • Date  comparisons  are  very  simple  in  C#,  because  DateTime  structure  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.
Prgoram of DateTime Structure                                                        
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();
}
}
}

No comments:

Post a Comment

Flag Counter