Suddenly a program is biting me,
Now.Format() being saved to a string variable, and then passed into a stored procedure, and somehow it is recorded in the database as 12:15 pm, instead of 12:15 am.
I'm not sure if this is because of the stored procedure (implicit conversion of string to datetime for the parameter, very bad), or the Now.Format() in the code net setting up the string variable.
Either way some poor planning and lack of code review, as both these items should have been caught and easily corrected.
So for my own purposes i'm recording the Now.Format and datetime stuff...
i.e. now.ToString("d"); // "09/27/2006" now.ToString("D"); // "Tuesday, 27 September 2006" now.ToString("G"); // "09/27/2006 14:15:39"
I have included my own table mapping Standard Format String to Custom Format string below. MSDN actually has a pretty good table that describe what each item does, and DateTime.ToString() has a pretty good code example that shows what each format string specifier do. Also if you just want samples, MSDN has a "Standard Date Time Format String Output example" here. Because documentation is so good. I won't go into this too much.
Custom Format String
Custom format string gives you the flexibility to build your own formatting. When using a single character format string specifier, you will need to prepend it with a "%", otherwise it will be interpreted as a Standard Format String. Here are the basics for building your own string:
DateTime now = new DateTime(2006, 9, 07, 15, 06, 01, 08, DateTimeKind.Local);
now.ToString(); //"09/27/2006 15:06:01"
Year
now.ToString("%y"); //"6"
now.ToString("yy"); //"06"
now.ToString("yyy"); //"2006"
now.ToString("yyyy"); //"2006"
Month
now.ToString("%M"); //"9"
now.ToString("MM"); //"09"
now.ToString("MMM"); //"Sep"
now.ToString("MMMM"); //"September"
Day
now.ToString("%d"); //"7"
now.ToString("dd"); //"07"
now.ToString("ddd"); //"Thu"
now.ToString("dddd"); //"Thursday"
Hour
now.ToString("%h"); //"3"
now.ToString("hh"); //"03"
now.ToString("hhh"); //"03"
now.ToString("hhhh"); //"03"
now.ToString("%H"); //"15"
now.ToString("HH"); //"15"
now.ToString("HHH"); //"15"
now.ToString("HHHH"); //"15"
Minutes
now.ToString("%m"); //"3"
now.ToString("mm"); //"03"
now.ToString("mmm"); //"03"
now.ToString("mmmm"); //"03"
Seconds
now.ToString("%s"); //"1"
now.ToString("ss"); //"01"
now.ToString("sss"); //"01"
now.ToString("ssss"); //"01"
Milliseconds
now.ToString("%f"); //"0"
now.ToString("ff"); //"00"
now.ToString("fff"); //"008"
now.ToString("ffff"); //"0080"
now.ToString("%F"); //""
now.ToString("FF"); //""
now.ToString("FFF"); //"008"
now.ToString("FFFF"); //"008"
Kind
now.ToString("%K"); //"-07:00"
now.ToString("KK"); //"-07:00-07:00"
now.ToString("KKK"); //"-07:00-07:00-07:00"
now.ToString("KKKK"); //"-07:00-07:00-07:00-07:00"
// Note: The multiple K were just read as multiple instances of the
// single K
DateTime unspecified = new DateTime(now.Ticks, DateTimeKind.Unspecified);
unspecified.ToString("%K"); //""
DateTime utc = new DateTime(now.Ticks, DateTimeKind.Utc);
utc.ToString("%K"); //"Z"
TimeZone
now.ToString("%z"); //"-7"
now.ToString("zz"); //"-07"
now.ToString("zzz"); //"-07:00"
now.ToString("zzzz"); //"-07:00"
Other
now.ToString("%g"); //"A.D."
now.ToString("gg"); //"A.D."
now.ToString("ggg"); //"A.D."
now.ToString("gggg"); //"A.D."
now.ToString("%t"); //"P"
now.ToString("tt"); //"PM"
now.ToString("ttt"); //"PM"
now.ToString("tttt"); //"PM"
Additional Resources
Now that you understand what Standard and Custom format strings are, here is a table of Standard Format String to Custom Format String mapping:
Year Month Day Patterns: d = "MM/dd/yyyy" D = "dddd, dd MMMM yyyy" M or m = "MMMM dd" Y or y = "yyyy MMMM"
Time Patterns: t = "HH:mm" T = "HH:mm:ss"
Year Month Day and Time without Time Zones: f = "dddd, dd MMMM yyyy HH:mm" F = "dddd, dd MMMM yyyy HH:mm:ss" g = "MM/dd/yyyy HH:mm" G = "MM/dd/yyyy HH:mm:ss"
Year Month Day and Time with Time Zones: o = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK" R or r = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'" s = "yyyy'-'MM'-'dd'T'HH':'mm':'ss" u = "yyyy'-'MM'-'dd HH':'mm':'ss'Z'" U = "dddd, dd MMMM yyyy HH:mm:ss"
All other single characters will throw an exception.
|