Popular Posts

Tuesday, 5 July 2011

DateTime Functions In ASP.Net With ToString():



Function
Result
DateTime.Now;
7/5/2011 11:38:38 PM
DateTime.Now.ToString();
7/5/2011 11:38:38 PM
DateTime.Now.ToShortTimeString()
11:38 PM
DateTime.Now.ToShortDateString()
7/5/2011
DateTime.Now.ToLongTimeString()
11:38:38 PM
DateTime.Now.ToLongDateString()
Tuesday, July 05, 2011
DateTime.Now.ToString("d")
7/5/2011
DateTime.Now.ToString("D")
Tuesday, July 05, 2011
DateTime.Now.ToString("f")
Tuesday, July 05, 2011 11:38 PM
DateTime.Now.ToString("F")
Tuesday, July 05, 2011 11:38:38 PM
DateTime.Now.ToString("g")
7/5/2011 11:38 PM
DateTime.Now.ToString("G")
7/5/2011 11:38:38 PM
DateTime.Now.ToString("m")
July 05
DateTime.Now.ToString("r")
Tue, 05 Jul 2011 23:38:38 GMT
DateTime.Now.ToString("s")
2011-07-05T23:38:38
DateTime.Now.ToString("t")
11:38 PM
DateTime.Now.ToString("T")
11:38:38 PM
DateTime.Now.ToString("u")
2011-07-05 23:38:38Z
DateTime.Now.ToString("U")
Wednesday, July 06, 2011 4:38:38 AM
DateTime.Now.ToString("y")
July, 2011
DateTime.Now.ToString("dddd, MMMM dd yyyy")
Tuesday, July 05 2011
DateTime.Now.ToString("ddd, MMM d "'"yy")
Tue, Jul 5 '11
DateTime.Now.ToString("dddd, MMMM dd")
Tuesday, July 05
DateTime.Now.ToString("M/yy")
7/11
DateTime.Now.ToString("dd-MM-yy")
05-07-11




Note :

Exception
Condition
ArgumentOutOfRangeException
The resulting DateTime is less than MinValue or greater than MaxValue.

If You want to know more about datetime constructors, Properties, Methods, Operators etc. 
, Than follow this link :


Some Other examples:
string date = DateTime.Now.Date;
1. Show Date as " Friday 13 August 2003"

 date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.Year.ToString()

2. Show date as "Friday 13 August 13:00:13"  //time in 24H format

 date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.ToString("HH:mm:ss")

3. Show date in 12H format with AM/PM as: "Friday 13 August 01:00:13 PM"

date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.ToString("hh:PM:mm:ss:tt")

No comments:

Post a Comment