Showing posts with label Asp.net Format. Show all posts
Showing posts with label Asp.net Format. Show all posts

20 May, 2010

String.Format("{0}", "formatting string"};


One of the painful things about good old ASP was string formatting, VBScript
simply didn't have anything useful. C# (and VB.Net) do, but MSDN doesn't provide
a quick reference to the formatting options. So here's a quick reference.


To compare string formatting in C# to those in C lets have an example,

char szOutput[256];

sprintf(szOutput, "At loop position %d.\n", i);


sprintf takes an output buffer, a format string and any number of arguments to
substitute into the format string.

The C# equivalent for sprintf is String.Format, which takes a format string and
the arguments. It returns a string, and because you're not passing in a buffer
there's no chance of a buffer overflow.

string outputString = String.Format("At loop position {0}.\n", i);

So why doesn't have the format argument have parameters specifying what data
type you're formatting? The CLR objects have metadata which informs the CLR what
the objects are, and each object has a standard ToString() method which returns
a string representation of that object. Much nicer than C where if you passed
the wrong type of variable into sprintf everything could come crashing down.

The ToString method can accept a string parameter which tells the object how to
format itself. In the call to String.Format , the formatting string is passed
after the position, for example, "{0:##}". The text inside the curly braces is
{argumentIndex[,alignment][:formatString]}. If alignment is positive, the text
is right-padding to fill the specified field length, if it's negative, it's
left-padded.

formatting strings

There's not much formatting that can be applied to a string. Only the padding /
alignment formatting options can be applied. These options are also available to
every argument, regardless of type.


example

output

String.Format("--{0,10}--", "test");

-- test--

String.Format("--{0,-10}--", "test");

--test --


formatting numbers

Number formatting is culture dependant. For
example, formatting a currency string on my laptop will return a result like
£9.99, formatting a currency on a machine set for the US region would return
$9.99.


specifier

type

format

output

(double 1.2345)

output

(int -12345)

c

currency

{0:c}

£1.23

-£12,345.00

d

decimal

(whole number)

{0:d}

System.FormatException

-12345

e

exponent / scientific

{0:e}

1.234500e+000

-1.234500e+004

f

fixed point

{0:f}

1.23

-12345.00

g

general

{0:g}

1.2345

-12345

n

number

{0:n}

1.23

-12,345.00

r

round trippable

{0:r}

1.23

System.FormatException

x

hexadecimal

{0:x4}

System.FormatException

ffffcfc7


custom number formatting



specifier

type

format

output

(double 1234.56)

0

zero placeholder

{0:00.000}

1234.560

#

digit placeholder

{0:#.##}

1234.56

.

decimal point placeholder

{0:0.0}

1234.6

,

thousand separator

{0:0,0}

1,235

%

percentage

{0:0%}

123456%


In addition there is the group separator; this is useful for varying the format,
depending on the value of the parameter passed. For example

String.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);


This will output "£1,240.00" if passed 1243.56. It will output the same
format bracketed if the value is negative "(£1,240.00)", and will output the
string "Nothing" if the number is zero.


date formatting


Date formats are very dependant on the culture
information passed. The examples below are shown using the UK culture.


specifier

type

output

(June 8, 1970 12:30:59)

d

Short Date

08/06/1970

D

Long Date

08 June 1970

t

Short Time

12:30

T

Long Time

12:30:59

f

Full date and time

08 June 1970 12:30

F

Full date and time (long)

08 June 1970 12:30:59

g

Default date and time

08/06/1970 12:30

G

Default date and time (long)

08/06/1970 12:30:59

M

Day / Month

8 June

r

RFC1123 date string

Mon, 08 Jun 1970 12:30:59 GMT

s

Sortable date/time

1970-06-08T12:30:59

u

Universal time, local timezone

1970-06-08 12:30:59Z

Y

Month / Year

June 1970


custom date formatting



specifier

type

output

(June 8, 1970 12:30:59)

dd

Day

08

ddd

Short Day Name

Mon

dddd

Full Day Name

Monday

hh

2 digit hour

12

HH

2 digit hour (24 hour)

12

mm

2 digit minute

30

MM

Month

06

MMM

Short Month name

Jun

MMMM

Month name

June

ss

seconds

59

tt

AM/PM

PM

yy

2 digit year

70

yyyy

4 digit year

1970

:

seperator, e.g. {0:hh:mm:ss}

12:30:59

/

seperator, e.g. {0:dd/MM/yyyy}

08/06/1970


There are others, including time zone formatting and so on, but the ones above
are the most commonly used.


culture information


string.format also provides a method which accepts a

CultureInfo
argument, as an IFormatProvider. This is important when trying
to write portable and localisable code, as, for example, month names will change
according to the local culture of the machine you are running on. Rather than
simply call the standard String.Format you should consider always calling the
overloaded culture method. If you don't need to specify a culture you can use
the System.Globalization.CultureInfo.InvariantCulture. This will then default
your formatting to English, as opposed to the culture of the current thread.

26 April, 2010

DateTime Format DateTime.ToString() Patterns

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. Not 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 ("\\").
Any other character copies any other character to the result string, without affecting formatting. ||

Get Cheet Sheets for Format