Wednesday, February 2, 2011

Date Time Various Format Conversion in C#

DateTime Conversion in Various Formats - DateTime.ToString()

All the patterns:

0

MM/dd/yyyy

08/22/2006

1

dddd, dd MMMM yyyy

Tuesday, 22 August 2006

2

dddd, dd MMMM yyyy

HH:mm Tuesday, 22 August 2006 06:30

3

dddd, dd MMMM yyyy

hh:mm tt Tuesday, 22 August 2006 06:30 AM

4

dddd, dd MMMM yyyy

H:mm Tuesday, 22 August 2006 6:30

5

dddd, dd MMMM yyyy

h:mm tt Tuesday, 22 August 2006 6:30 AM

6

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

7

MM/dd/yyyy HH:mm

08/22/2006 06:30

8

MM/dd/yyyy hh:mm tt

08/22/2006 06:30 AM

9

MM/dd/yyyy H:mm

08/22/2006 6:30

10

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

10

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

10

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

11

MM/dd/yyyy HH:mm:ss

08/22/2006 06:30:07

12

MMMM dd

August 22

13

MMMM dd

August 22

14

yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK

2006-08-22T06:30:07.7199222-04:00

15

yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK

2006-08-22T06:30:07.7199222-04:00

16

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

Tue, 22 Aug 2006 06:30:07 GMT

17

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

Tue, 22 Aug 2006 06:30:07 GMT

18

yyyy'-'MM'-'dd'T'HH':'mm':'ss

2006-08-22T06:30:07

19

HH:mm

06:30

20

hh:mm tt

06:30 AM

21

H:mm

6:30

22

h:mm tt

6:30 AM

23

HH:mm:ss

06:30:07

24

yyyy'-'MM'-'dd HH':'mm':'ss'Z'

2006-08-22 06:30:07Z

25

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

26

yyyy MMMM

2006 August

27

yyyy MMMM

2006 August

The patterns for DateTime.ToString ( 'd' ) :

0

MM/dd/yyyy

08/22/2006

The patterns for DateTime.ToString ( 'D' ) :

0

dddd, dd MMMM yyyy

Tuesday, 22 August 2006

The patterns for DateTime.ToString ( 'f' ) :

0

dddd, dd MMMM yyyy HH:mm

Tuesday, 22 August 2006 06:30

1

dddd, dd MMMM yyyy hh:mm

tt Tuesday, 22 August 2006 06:30 AM

2

dddd, dd MMMM yyyy H:mm

Tuesday, 22 August 2006 6:30

3

dddd, dd MMMM yyyy h:mm

tt Tuesday, 22 August 2006 6:30 AM

The patterns for DateTime.ToString ( 'F' ) :

0

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'g' ) :

0

MM/dd/yyyy HH:mm

08/22/2006 06:30

1

MM/dd/yyyy hh:mm

tt 08/22/2006 06:30 AM

2

MM/dd/yyyy H:mm

08/22/2006 6:30

3

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

The patterns for DateTime.ToString ( 'G' ) :

0

MM/dd/yyyy HH:mm:ss

08/22/2006 06:30:07

The patterns for DateTime.ToString ( 'm' ) :

0

MMMM dd

August 22

The patterns for DateTime.ToString ( 'r' ) :

0

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

Tue, 22 Aug 2006 06:30:07 GMT

The patterns for DateTime.ToString ( 's' ) :

0

yyyy'-'MM'-'dd'T'HH':'mm':'ss

2006-08-22T06:30:07

The patterns for DateTime.ToString ( 'u' ) :

0

yyyy'-'MM'-'dd HH':'mm':'ss'Z'

2006-08-22 06:30:07Z

The patterns for DateTime.ToString ( 'U' ) :

0

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'y' ) :

0

yyyy MMMM 2006 August

Building a custom DateTime.ToString Patterns

The following details the meaning of each pattern character. Note the K and z character.

d

Represents the day of the month as a number from 1 through 31. A single-digit day is
formatted without a leading zero

dd

Represents the day of the month as a number from 01 through 31. A single-digit day is
formatted with a leading zero

ddd

Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc)

dddd

Represents the full name of the day of the week (Monday, Tuesday etc)

h

12-hour clock hour (e.g. 7)

hh

12-hour clock, with a leading 0 (e.g. 07)

H

24-hour clock hour (e.g. 19)

HH

24-hour clock hour, with a leading 0 (e.g. 19)

m

Minutes

mm

Minutes with a leading zero

M

Month number

MM

Month number with leading zero

MMM

Abbreviated Month Name (e.g. Dec)

MMMM

Full month name (e.g. December)

s

Seconds

ss

Seconds with leading zero

t

Abbreviated AM / PM (e.g. A or P)

tt

AM / PM (e.g. AM or PM

y

Year, no leading zero (e.g. 2001 would be 1)

yy

Year, leadin zero (e.g. 2001 would be 01)

yyy

Year, (e.g. 2001 would be 2001)

yyyy

Year, (e.g. 2001 would be 2001)

K

Represents the time zone information of a date and time value (e.g. +05:00)

z

With DateTime values, represents the signed offset of the local operating system's time zone
from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)

zz

As z but with leadin zero (e.g. +06)

zzz

With DateTime values, represents the signed offset of the local operating system's time zone
from UTC, measured in hours and minutes. (e.g. +06:00)

f

Represents the most significant digit of the seconds fraction; that is, it represents the tenths of
a second in a date and time value.

ff

Represents the two most significant digits of the seconds fraction; that is, it represents the
hundredths of a second in a date and time value.

fff

Represents the three most significant digits of the seconds fraction; that is, it represents the
milliseconds in a date and time value.

ffff

Represents the four most significant digits of the seconds fraction; that is, it represents the ten
thousandths of a second in a date and time value. While it is possible to display the ten
thousandths of a second component of a time value, that value may not be meaningful.
The precision of date and time values depends on the resolution of the system clock. On
Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is
approximately 10-15 milliseconds.

fffff

Represents the five most significant digits of the seconds fraction; that is, it represents the
hundred thousandths of a second in a date and time value. While it is possible to display the
hundred thousandths of a second component of a time value, that value may not be meaningful.

The precision of date and time values depends on the resolution of the system clock. On
Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is
approximately 10-15 milliseconds.

ffffff

Represents the six most significant digits of the seconds fraction; that is, it represents the millionths
of a second in a date and time value. While it is possible to display the millionths of a second
component of a time value, that value may not be meaningful. The precision of date and time
values depends on the resolution of the system clock. On Windows NT 3.5 and later, and
Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

fffffff

Represents the seven most significant digits of the seconds fraction; that is, it represents the ten
millionths of a second in a date and time value. While it is possible to display the ten millionths
of a second component of a time value, that value may not be meaningful. The precision of
date and time values depends on the resolution of the system clock. On Windows NT 3.5
and later, and Windows Vista operating systems, the clock's resolution is approximately
10-15 milliseconds.

F

Represents the most significant digit of the seconds fraction; that is, it represents the tenths
of a second in a date and time value. Nothing is displayed if the digit is zero.

:

Represents the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator
property. This separator is used to differentiate hours, minutes, and seconds.

/

Represents the date separator defined in the current DateTimeFormatInfo..::.
DateSeparator property. This separator is used to differentiate years, months, and days.

"

Represents a quoted string (quotation mark). Displays the literal value of any string between
two quotation marks ("). Your application should precede each quotation mark with an escape
character (\).

'

Represents a quoted string (apostrophe). Displays the literal value of any string between two
apostrophe (') characters.

%c

Represents the result associated with a c custom format specifier, when the custom date and
time format string consists solely of that custom format specifier. That is, to use the d, f, F, h,
m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f,
%F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format
specifier, see Using Single Custom Format Specifiers.

||\c || Represents the escape character, and displays the character "c" as a literal when that character is
preceded by the escape character (\). To insert the backslash character itself in the result string, the application
should use two escape characters ("\\").

 

Various Datetime formats in SQL Query

Problem
There are many instances when dates and times don't show up at your doorstep in the format you'd like it to be, nor does the output of a query fit the needs of the people viewing it. One option is to format the data in the application itself. Another option is to use the built-in functions SQL Server provides to format the date string for you.

Solution
SQL Server provides a number of options you can use to format a date/time string. One of the first considerations is the actual date/time needed. The most common is the current date/time using getdate(). This provides the current date and time according to the server providing the date and time. If a universal date/time is needed, then getutcdate() should be used. To change the format of the date, you convert the requested date to a string and specify the format number corresponding to the format needed. Below is a list of formats and an example of the output:

DATE FORMATS

Format #

Query (current date: 12/30/2006)

Sample

1

select convert(varchar, getdate(), 1)

12/30/06

2

select convert(varchar, getdate(), 2)

06.12.30

3

select convert(varchar, getdate(), 3)

30/12/06

4

select convert(varchar, getdate(), 4)

30.12.06

5

select convert(varchar, getdate(), 5)

30-12-06

6

select convert(varchar, getdate(), 6)

30 Dec 06

7

select convert(varchar, getdate(), 7)

Dec 30, 06

10

select convert(varchar, getdate(), 10)

12-30-06

11

select convert(varchar, getdate(), 11)

06/12/30

101

select convert(varchar, getdate(), 101)

12/30/2006

102

select convert(varchar, getdate(), 102)

2006.12.30

103

select convert(varchar, getdate(), 103)

30/12/2006

104

select convert(varchar, getdate(), 104)

30.12.2006

105

select convert(varchar, getdate(), 105)

30-12-2006

106

select convert(varchar, getdate(), 106)

30 Dec 2006

107

select convert(varchar, getdate(), 107)

Dec 30, 2006

110

select convert(varchar, getdate(), 110)

12-30-2006

111

select convert(varchar, getdate(), 111)

2006/12/30

TIME FORMATS

8 or 108

select convert(varchar, getdate(), 8)

00:38:54

9 or 109

select convert(varchar, getdate(), 9)

Dec 30 2006 12:38:54:840AM

14 or 114

select convert(varchar, getdate(), 14)

00:38:54:840

You can also format the date or time without dividing characters, as well as concatenate the date and time string:

Sample statement

Output

select replace(convert(varchar, getdate(),101),'/','')

12302006

select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','')

12302006004426