DateTime class allows us to get current date,time ,manipulating dates,time spans operations,comparing dates . It has both Static methods and Instance methods.
Get Today's date
DateTime today = System.DateTime.Today;
today.ToString();
Default culture for Programs is en-US LCID 1033.
//Output: month/date/year midnight time
2/1/2013 12:00:00 AM
Get Day of the Month
DateTime dt = new DateTime(2013,12,31)
dt.Day;
//Output: displays Day
31
DateTime today = DateTime.Today;
today.Day
//Output: displays Day
31
DateTime.Now.ToString("%d");
//Output: display single digit/two digit date.
First Day 1
For 10th Day 10
DateTime.Now.ToString("dd");
//Output: display two digit date.
First Day 01
For 10th Day 10
Get Day from the Date in Words
DateTime dt = new DateTime(2013,12,31)
dt.DayOfWeek;
//Output: displays Day of the week fullname
Tuesday
DateTime today = DateTime.Today;
today.DayOfWeek
//Output: displays Day of the Week fullname
Tuesday
DateTime.Now.ToString("ddd");
//Output: Displays day of the week in short form
Fri
DateTime.Now.ToString("dddd");
//Output:Displays day of the week in full name
Friday.
Get Day from the Date in Number
DateTime dt = new DateTime(2013,12,31)
(int)dt.DayOfWeek;
//Output: displays Day of the week number
2 2'd day of the week
DateTime today = DateTime.Today;
(int)today.DayOfWeek
//Output: displays Day of the Week fullname
Friday 5th day of the week
for Sunday 0
for Monday 1 etc.,
Get Month from Date
DateTime dtNow = DateTime.Now;
dt.Month
//OUTPUT: Display single digit or two digit Month
2
DateTime dt = new DateTime(2013,12,31)
dt.ToString("%M")
//OUTPUT: Display single digit or two digit Month
12
DateTime dt = new DateTime(2013,12,31)
dt.ToString("MM")
//OUTPUT: Display two digit Month 01-12
12
DateTime dt = new DateTime(2013,12,31)
dt.ToString("MMM")
//OUTPUT: Display Short name of the Month
Dec
DateTime dt = new DateTime(2013,12,31)
dt.ToString("MMMM")
//OUTPUT: Display Full name of the Month
December
DateTime.Now.ToString("MMMM");
//Displays full name of the month for current datetime.
DateTime.Today.ToString("MMMM");
//Displays full name of the month for today(above case displays same name).
Get 4-digit Year from Date
DateTime dtNow = DateTime.Now;
dt.Year
//OUTPUT: Displays 4 digit year
2013
DateTime dt = new DateTime(2020,12,31) dt.ToString("yyy")
// 4 yyyy <=> 3 yyy
//OUTPUT: Displays four digit Year
2020
Get 2-digit Year from Date
DateTime dtNow = DateTime.Now;
dt.ToString("%y");
//OUTPUT: Displays 2 digit year for year 2013
13
DateTime dt = new DateTime(2020,12,31) dt.ToString("yy")
//OUTPUT: Displays two digit Year
20
DateTime dt = DateTime.Today dt.ToString("yy")
Get Time in AM/PM format
Formats:
h --> hour in single digit AM/PM format
hh -> hour in two digit AM/PM format
m --> minute in single digit
mm --> two digit format
s --> second in single digit format
ss --> Seconds two digit format
t -> AM/PM represents single/first letter i.e A or P
tt -> represents 2 chars AM or PM.
DateTime dt = DateTime.Now;
dt.ToString("hh:mm:ss tt")
//OUTPUT: displays hours minutes and Seconds in AM/PM format
03:01:11 PM
h hours from 0-9(single digit hour)
hh hours from 00-12 (double digit hour)
dt.ToString("%h:%m:%s tt")
//Output: displays hours minutes seconds in single digit format(if applicable)
3:1:11 PM
Get Time in 24-hours format
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("H:%m:%s"));
//OUTPUT: Time in 24 hours format
15:35:35 or
01:01:11
Format: Capital H or HH or %H gives two digit 24 hour format
Get Today's date
DateTime today = System.DateTime.Today;
today.ToString();
Default culture for Programs is en-US LCID 1033.
//Output: month/date/year midnight time
2/1/2013 12:00:00 AM
Get Day of the Month
DateTime dt = new DateTime(2013,12,31)
dt.Day;
//Output: displays Day
31
DateTime today = DateTime.Today;
today.Day
//Output: displays Day
31
DateTime.Now.ToString("%d");
//Output: display single digit/two digit date.
First Day 1
For 10th Day 10
DateTime.Now.ToString("dd");
//Output: display two digit date.
First Day 01
For 10th Day 10
Get Day from the Date in Words
DateTime dt = new DateTime(2013,12,31)
dt.DayOfWeek;
//Output: displays Day of the week fullname
Tuesday
DateTime today = DateTime.Today;
today.DayOfWeek
//Output: displays Day of the Week fullname
Tuesday
DateTime.Now.ToString("ddd");
//Output: Displays day of the week in short form
Fri
DateTime.Now.ToString("dddd");
//Output:Displays day of the week in full name
Friday.
Get Day from the Date in Number
DateTime dt = new DateTime(2013,12,31)
(int)dt.DayOfWeek;
//Output: displays Day of the week number
2 2'd day of the week
DateTime today = DateTime.Today;
(int)today.DayOfWeek
//Output: displays Day of the Week fullname
Friday 5th day of the week
for Sunday 0
for Monday 1 etc.,
Get Month from Date
DateTime dtNow = DateTime.Now;
dt.Month
//OUTPUT: Display single digit or two digit Month
2
DateTime dt = new DateTime(2013,12,31)
dt.ToString("%M")
//OUTPUT: Display single digit or two digit Month
12
DateTime dt = new DateTime(2013,12,31)
dt.ToString("MM")
//OUTPUT: Display two digit Month 01-12
12
DateTime dt = new DateTime(2013,12,31)
dt.ToString("MMM")
//OUTPUT: Display Short name of the Month
Dec
DateTime dt = new DateTime(2013,12,31)
dt.ToString("MMMM")
//OUTPUT: Display Full name of the Month
December
DateTime.Now.ToString("MMMM");
//Displays full name of the month for current datetime.
DateTime.Today.ToString("MMMM");
//Displays full name of the month for today(above case displays same name).
Get 4-digit Year from Date
DateTime dtNow = DateTime.Now;
dt.Year
//OUTPUT: Displays 4 digit year
2013
DateTime dt = new DateTime(2020,12,31) dt.ToString("yyy")
// 4 yyyy <=> 3 yyy
//OUTPUT: Displays four digit Year
2020
Get 2-digit Year from Date
DateTime dtNow = DateTime.Now;
dt.ToString("%y");
//OUTPUT: Displays 2 digit year for year 2013
13
DateTime dt = new DateTime(2020,12,31) dt.ToString("yy")
//OUTPUT: Displays two digit Year
20
DateTime dt = DateTime.Today dt.ToString("yy")
Get Time in AM/PM format
Formats:
h --> hour in single digit AM/PM format
hh -> hour in two digit AM/PM format
m --> minute in single digit
mm --> two digit format
s --> second in single digit format
ss --> Seconds two digit format
t -> AM/PM represents single/first letter i.e A or P
tt -> represents 2 chars AM or PM.
DateTime dt = DateTime.Now;
dt.ToString("hh:mm:ss tt")
//OUTPUT: displays hours minutes and Seconds in AM/PM format
03:01:11 PM
h hours from 0-9(single digit hour)
hh hours from 00-12 (double digit hour)
dt.ToString("%h:%m:%s tt")
//Output: displays hours minutes seconds in single digit format(if applicable)
3:1:11 PM
Get Time in 24-hours format
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("H:%m:%s"));
//OUTPUT: Time in 24 hours format
15:35:35 or
01:01:11
Format: Capital H or HH or %H gives two digit 24 hour format
No comments:
Post a Comment