Ad

Saturday, July 27, 2013

Caluclate The Difference between Two Dates Using C#.Net

Overview:
                In this article i would like to publish an article on Caluclate The Difference between Two Dates Using C#.Net.


Description:
                DateTime is one of the class available in "Using System.DataTime".It is used to accommodate datetime values into that class.It consists of year,month,day,sec,millsec and so on..........

 The DateTime and TimeSpan value types differ in that a DateTime represents an instant in time, whereas a TimeSpan represents a time interval. This means, for example, that you can subtract one instance of DateTime from another to obtain the time interval between them. Or you could add a positive TimeSpan to the current DateTime to calculate a future date.

Program:



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

namespace dt
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2012,9,24);
            DateTime dt1 = DateTime.Now;
            TimeSpan t = dt1 - dt;
            Console.WriteLine("Date:"+dt.Date);
            Console.WriteLine("Day:"+dt.Day);
            Console.WriteLine("Week:"+dt.DayOfWeek);
            Console.WriteLine("Year:"+dt.DayOfYear);
            Console.WriteLine("Hour:"+dt.Hour);
            Console.WriteLine("Kind:"+dt.Kind);
            Console.WriteLine("MilliSecond:"+dt.Millisecond);
            Console.WriteLine("Minute:"+dt.Minute);
            Console.WriteLine("Month:"+dt.Month);
            Console.WriteLine("Seconds:"+dt.Second);
            Console.WriteLine("Ticks:"+dt.Ticks);
            Console.WriteLine("Time Of Day:"+dt.TimeOfDay);
            Console.WriteLine("Year:"+dt.Year);
            Console.WriteLine("Total Number Of Days:"+t.TotalDays);
            Console.ReadLine();
        }
    }
}

  Output:


No comments: