Thursday, May 03, 2007

Custom DateTime Format Specifier

      Yesterday during surfing I foud really important material on net. I was feeling sleepy so not addded site in favorite and hence notable to give reference here. However I have copied the content in notepad. Its really very helpfull.

d

Displays the current day of the month.

dd

Displays the current day of the month, where values < 10 have a leading zero.

ddd

Displays the three-letter abbreviation of the name of the day of the week.

dddd(+)

Displays the full name of the day of the week represented by the given DateTime value.

f(+)

Displays the x most significant digits of the seconds value. The more f's in the format specifier, the more significant digits. This is total seconds, not the number of seconds passed since the last minute.

F(+)

Same as f(+), except trailing zeros are not displayed.

g

Displays the era for a given DateTime (for example, "A.D.")

h

Displays the hour, in range 112.

hh

Displays the hour, in range 112, where values < 10 have a leading zero.

H

Displays the hour in range 023.

HH

Displays the hour in range 023, where values < 10 have a leading zero.

m

Displays the minute, range 059.

mm

Displays the minute, range 059, where values < 10 have a leading zero.

M

Displays the month as a value ranging from 112.

MM

Displays the month as a value ranging from 112 where values < 10 have a leading zero.

MMM

Displays the three-character abbreviated name of the month.

MMMM

Displays the full name of the month.

s

Displays the number of seconds in range 059.

ss(+)

Displays the number of seconds in range 059, where values < 10 have a leading 0.

t

Displays the first character of the AM/PM indicator for the given time.

tt(+)

Displays the full AM/PM indicator for the given time.

y/yy/yyyy

Displays the year for the given time.

z/zz/zzz(+)

Displays the timezone offset for the given time.

 

       Take a look at the following lines of code, which demonstrate using string format specifiers to create custom-formatted date and time strings:  

DateTime dt = DateTime.Now;
Console.WriteLine(string.Format(
"Default format: {0}", dt.ToString()));
Console.WriteLine(dt.ToString(
"dddd dd MMMM, yyyy g"));
Console.WriteLine(string.Format(
("Custom Format 1: {0:MM/dd/yy hh:mm:sstt}", dt));
Console.WriteLine(string.Format(
("Custom Format 2: {0:hh:mm:sstt G\\MT zz}", dt));
 

Here is the output from the preceding code:

Default format: 9/24/2005 12:59:49 PM
Saturday 24 September, 2005 A.D.
Custom Format 1: 09/24/05 12:59:49PM

Custom Format 2: 12:59:49PM GMT -06


Happy Programming!!

3 comments:

Jalpesh P. Vadgama said...

Hi chirag, I am jalpesh working as .net developer in rajkot. I have seen your both blog and I found your collection very use full. It is good that lots of people from gujrat are doing blogging and help techies.
I have also got my blog at jalpesh.blogspot.com

Please do visit and send your feedback

Anonymous said...

hi Mr. Chirag, I am Sonal working as s/w developer i have seen you blog, its good i am trying to set GMT +1 date/time formate in my webapplication can you help me how to set it?

if you have any idea then plz send me rep. on sonal.100nal@gmail.com

Thanks in Advance.

Anonymous said...

Thanks
Quite helpful
Rajat