Ad

Saturday, July 27, 2013

Some Examples on DateTime using C#.Net?

Overview:
                In this article i would like to publish an article on "Some Examples on DateTime using C#.Net".In Previous article 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 Class1
    {
        static void Main(string[] args)
        {
           
            DateTime d1 = DateTime.Now;
            DateTime d2 = new DateTime(2012, 1, 2);
            Console.WriteLine("Displaying String Values:"+d1.ToString());
            Console.WriteLine("Displaying Time String:"+d1.ToShortTimeString());
            Console.WriteLine("Displaying ToShortDate String:"+d1.ToShortDateString());
            Console.WriteLine("Displaying ToLong Time String:" + d1.ToLongTimeString());
            Console.WriteLine("Displaying ToLong Date String:" + d1.ToLongDateString());
            Console.WriteLine("Displaying To Local Time:" + d1.ToLocalTime());           
            Console.WriteLine("Days:"+d1.AddDays(2));
            Console.WriteLine("Hours:"+d1.AddHours(2));
            Console.WriteLine("MilliSeconds:"+d1.AddMilliseconds(2));
            Console.WriteLine("Minutes:"+d1.AddMinutes(10));
            Console.WriteLine("Adding Months:"+d1.AddMonths(2));
            Console.WriteLine("Seconds:"+d1.AddSeconds(10));
            Console.WriteLine("Ticks:"+d1.AddTicks(10));
            Console.WriteLine("After Adding:"+d1.AddYears(2));
            Console.WriteLine("Compare:"+d1.CompareTo(d2));
            Console.WriteLine("Substract:"+d1.Subtract(d2));
            Console.ReadLine();

        }
    }
}

 
OutPut:

No comments: