.NET Formatting Strings

formatting strings

Example Output
String.Format(“–{0,10}–“, “test”); —      test–
String.Format(“–{0,-10}–“, “test”); –test      —

formatting numbers

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%

Group separator

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

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 June 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
: Separator, e.g. {0:hh:mm:ss} 12:30:59
/ Separator, e.g. {0:dd/MM/yyyy} 08/06/1970
Advertisements

DateTime to simple recognized string (Facebook time format)

// DateAndTime Helper
public static string GetDateAndTimeFormNow(this HtmlHelper helper, string datetime)
{
    string value = string.Empty;

    if (!string.IsNullOrEmpty(datetime))
    {
        DateTime d1 = DateTime.Now;
        DateTime d2 = Convert.ToDateTime(datetime);

        TimeSpan t = d1 - d2;

        if (t.TotalDays >= 2)
            value = Math.Round(t.TotalDays, 0).ToString() + " days ago.";
        else if (t.TotalDays >= 1)
            value = "a day ago.";
        else if (t.TotalHours >= 2)
            value = Math.Round(t.TotalHours, 0).ToString() + " hours ago.";
        else if (t.TotalHours >= 1)
            value = "an hour ago.";
        else if (t.TotalMinutes >= 2)
            value = Math.Round(t.TotalMinutes, 0).ToString() + " minutes ago.";
        else
            value = "a minute ago.";
    }

    return value;
}