Tuesday 13 March 2012

DateTime Structure In C#


// Program to demonstrate DateTime
using System;

class Program
{
static void Main()
{
DateTime d=DateTime.Now;

//To Print Only Date
Console.WriteLine("------------Date Only------------");
Console.WriteLine(d.ToString("d"));
Console.WriteLine(d.ToString("D"));


//To Print Only Time
Console.WriteLine("------------Time Only------------");
Console.WriteLine(d.ToString("t"));
Console.WriteLine(d.ToString("T"));

//To Print Only Day
Console.WriteLine("------------Day Only------------");
Console.WriteLine(d.ToString("dd"));
Console.WriteLine(d.ToString("ddd"));
Console.WriteLine(d.ToString("dddd"));

//To Print Only Month
Console.WriteLine("------------Month Only------------");
Console.WriteLine(d.ToString("M"));
Console.WriteLine(d.ToString("MM"));
Console.WriteLine(d.ToString("MMM"));
Console.WriteLine(d.ToString("MMMM"));

//To Print Only Year
Console.WriteLine("------------Year Only------------");
Console.WriteLine(d.ToString("YYYY"));
Console.WriteLine(d.ToString("YY"));

//To Print Only hours
Console.WriteLine("------------Hours Only------------");
//Console.WriteLine(d.ToString("H"));
Console.WriteLine(d.ToString("HH"));

//To Print Only Minutes
Console.WriteLine("------------Minutes Only------------");
Console.WriteLine(d.ToString("m"));
Console.WriteLine(d.ToString("mm"));

//To Print Only Seconds
Console.WriteLine("------------Seconds Only------------");
Console.WriteLine(d.ToString("s"));
Console.WriteLine(d.ToString("ss"));

//To Print Diffrent Cobinations of Date Time
Console.WriteLine("------------Combinations of above formats------------");
Console.WriteLine(d.ToString("ddd dd-MM-YYYY"));
Console.WriteLine(d.ToString("dddd, dd MMMM YYYY hh:mm:ss"));
}
}

No comments:

Post a Comment