Ad

Saturday, July 27, 2013

Timespan and DateTime Classes In C#.Net

Overview:
               In this article i would like to publish an article on  "Timespan and DateTime Classes In C#.Net".In prevoius article i publish on Some Examples on DateTime using C#.Net.


Description:
         In this article i would like to tends to make an difference of an "TimeSpan" and "DateTime".

TimeSpan:It is used to represent an time interval in terms of days in number of formats.

DateTime:It is used to represent an instant in time,typically expressed as a date and time of a day.

Program:


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

namespace dt
{
    class Class2
    {
        static void Main(string[] args)
        {
            DateTime date1 = new DateTime(1980, 6, 3, 22, 15, 0);//years,months,day,time,minutes,sec
            DateTime date2 = new DateTime(1983, 12, 6, 13, 2, 0);//years,months,day,time,minutes,sec
            DateTime date3 = new DateTime(1986, 10, 12, 8, 42, 0);//years,months,day,time,minutes,sec
            Console.WriteLine(date1);
            Console.WriteLine(date2);
            Console.WriteLine(date3);
            // Difference between date2 and date1 stores in timespan diff1
            TimeSpan diff1 = date2.Subtract(date1);
            Console.WriteLine("Difference is:"+diff1);
            // Difference is Stored in terms of Date Time Format
            DateTime date4 = date3.Subtract(diff1);
            Console.WriteLine("Difference is Stored in terms of Date Time Format:" + date4);
            // Difference between date2 and date3 stores in timespan diff2
            TimeSpan diff2 = date2 - date3;
            Console.WriteLine("Difference is:" + diff2);
            // Difference is Stored in terms of Date Time Format
            DateTime date5 = date1 - diff2;
            Console.WriteLine("Difference is Stored in terms of Date Time Format:" + date5);
            Console.ReadLine();
        }
    }
}

OutPut: 

No comments: