Using DateTime Object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //1
            //To get current Date And Time
            DateTime DT = new DateTime();
            DT = DateTime.Now;
            Console.WriteLine("Current date is {0} ", DT.Date);
            //2
            //Initalize datetime object using overloads
            DateTime DT = new DateTime(2008, 7, 4, 21, 35, 15, 777);
            Console.WriteLine("Current date is {0} ", DT);
            //3
            //Extracting Parts from datetime object.
            DateTime DT = new DateTime();
            DT=DateTime.Now;
            Console.WriteLine("Day of week is {0} ,day name is {1} ,year is {2} ",  DT.Day,DT.DayOfWeek, DT.Year);
            //4
            //To add days,months,years,hours,minutes,seconds
            DateTime DT = DateTime.Now;
            Console.WriteLine("Before adding date is {0} ", DT.Date);
            DT = DT.AddDays(1);
            Console.WriteLine("After adding date is {0} ", DT.Date);
            //5
            //To calculate difference between two datetime objects.
            DateTime Current_Date = DateTime.Now;
            DateTime DT = new DateTime(2010, 10, 25, 21, 35, 15, 777);
            TimeSpan Difference = Current_Date - DT;
            Console.WriteLine("Current date is {0}", Current_Date);
            Console.WriteLine("Date to be subtracted is {0}", DT);
            Console.WriteLine(" Difference in days is {0},difference in minutes is {1},difference in hours is {2}", Difference.Days,Difference.Minutes,Difference.Hours);
  //6
            //Converting strings to DateTime.
            DateTime DT;
            Console.Write("Please enter a date (mm/dd/yyyy): ");
            string dateStr = Console.ReadLine();
            DT = DateTime.Parse(dateStr);
            Console.WriteLine("You entered {0}",DT);

        }
    }
}

Methods for converting DateTime Objects to String:
ToLongDateString()
ToLongTimeString()
ToShortDateString()
ToShortTimeString()
ToString()

This is how you can convert a DateTime object to string

DateTime DT = DateTime.Now;
Console.WriteLine("Date is {0} ",DT.ToLongDateString());

Methods for Adding DateTime Object:

Add (TimeSpan Value)
AddDays(double value)
AddHours(double value)
AddMilliSeconds(double value)
AddMonths(int value)
AddSeconds(double value)
AddMinutes(double value)
AddYear(int value)
AddTicks(long value)

Properties of DateTime Objects:

Date                       Gets date component of the instance.
Day                        Gets the day of month
DayOfWeek             Gets the name of day
DayOfYear              Gets the day number of year.
Hour                       Gets the hour of the day (1PM=13)
MilliSecond               Gets the millisecond of the date
Second                   Gets the second of the date.
Minute                    Gets the minute component from date.
Month                     Gets the month number(March=3)
Ticks                      Gets number of ticks from date specified.
TimeOfDay              Gets time of the day(14:10:30)
Year                       Gets the year component from date.


Using Format Strings:
DateTime DT = DateTime.Now;
          Console.WriteLine("Date is {0:D} ",DT);
The output of this will be “Date is Sunday,December 04,2011”
You can use any of format string in Console statement listed in the table.
Format String Purpose:

D                  Day (1–31)
Dd               Two-digit day (01–31)
Ddd              Abbreviated name of day (for example, Fri)
dddd             Full name of day (for example, Friday)
M                Month (1–12)
MM               Two-digit month (01–12)
MMM             Abbreviated name of month (for example, Jul)
MMMM          Full month name (for example, July)
Yy                Two-digit year
yyyy             Four-digit year
H                  Hour (1–12)
Hh                Two-digit hour (01–12)
H                  24-hour clock (0–23)
HH                Two-digit 24-hour clock (00–23)
M                 Minutes (0–59)
Mm               Two-digit minutes (00–59)
S                  Seconds (0–59)
Ss                Two-digit seconds (00–59)

Comments

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net