Datetime
How to use Datetime
Description:
The below code shows all datetime manipulations...Code:
namespace ConsoleApplicationDatetime
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Date :"+DateTime.Now.ToString());
Console.WriteLine("Lep Year (True/False) :" + DateTime.IsLeapYear(2004).ToString());
Console.WriteLine("Days in Month :" + DateTime.DaysInMonth(2009, 5));
DateTime dt = DateTime.Now;
Console.WriteLine("Date :" + dt.Date.ToString());
Console.WriteLine("Day of Date :" + dt.Day.ToString());
// It will dispalys in total 365 days which day it is...
Console.WriteLine("Day of Year :" + dt.DayOfYear.ToString());
Console.WriteLine("Hour :" + dt.Hour.ToString());
Console.WriteLine("MilliSeconds :" + dt.Millisecond.ToString());
Console.WriteLine("Day of Week :" + dt.DayOfWeek.ToString());
Console.WriteLine("Ticks :" + dt.Ticks.ToString());
Console.WriteLine("Month :" + dt.Month.ToString());
Console.WriteLine("Minutes :" + dt.Minute.ToString());
Console.WriteLine("Time of day :" + dt.TimeOfDay.ToString());
Console.WriteLine("Time Span Max Value :" + TimeSpan.MaxValue.ToString());
Console.WriteLine(TimeSpan.FromDays(Convert.ToDouble(5)).ToString());
Console.WriteLine("Add Days:" + dt.AddDays(5));
Console.WriteLine("Add Hours:" + dt.AddHours(5));
Console.WriteLine("Add Milliseconds:" + dt.AddMilliseconds(50));
Console.WriteLine("Add Minutes:" + dt.AddMinutes(5));
Console.WriteLine("Add Months:" + dt.AddMonths(1));
Console.WriteLine("Add Seconds:" + dt.AddSeconds(1500));
Console.WriteLine("Add Years:" + dt.AddYears(1));
Console.ReadLine();
}
}
}
namespace ConAppDate_Time
{
class Program
{
static void Main(string[] args)
{
DateTime date = DateTime.Now;
Console.WriteLine("Date is{0:ddd MMM dd,yyyy}",date);
Console.WriteLine("Time is {0:hh:mm tt}",date);
Console.WriteLine("24 hour time is{0:HH:mm}",date);
Console.WriteLine("Time with seconds:{0:hh:MM:ss tt}",date);
Console.WriteLine("Day of month:{0:m}",date);
Console.WriteLine("Year:{0:yyyy}",date);
}
}
}
FORMAT SPECIFIER FOR DATE TIME
ddd=Represent the full name of the day of the week.
dddd=Represent the full name of the day of the week.
FF=Represent Two digit of the seconds fraction
H=Represent the hour from 0 to 23
HH=Represent the hour from 00 to 23
MM=Represent month as a number from 01 to 12
MMM=Represent full name of the month
s=Representthe second as a number from 0 to 59