Standard Format String
This is basically built in short hand for custom format string. You pass in the one character string to denote which custom format you want.
i.e.
now.ToString("d"); // "10/21/2009"
now.ToString("D"); // "Wednesday, October 21, 2009"
now.ToString("G"); // "10/21/2009 10:16:01 PM"
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:
Example:
StringBuilder strBuilder=new StringBuilder ();
DateTime now = new DateTime(2009,10, 21, 22, 16, 01, 08, DateTimeKind.Local);
strBuilder.AppendLine ("now.ToString() ==> " + now .ToString() );
strBuilder.AppendLine("now.ToString(\"d\")" + now.ToString("d"));
strBuilder.AppendLine("now.ToString(\"D\")" + now.ToString("D"));
strBuilder.AppendLine("now.ToString(\"G\")" + now.ToString("G"));
// Year
strBuilder.AppendLine("YEAR ");
strBuilder.AppendLine ("now.ToString(\"%y\") ==> " + now.ToString("%y") );
strBuilder.AppendLine ("now.ToString(\"yy\") ==> " + now.ToString("yy") );
strBuilder.AppendLine ("now.ToString(\"yyy\") ==>" + now.ToString("yyy") );
strBuilder.AppendLine ("now.ToString(\"yyyy\") ==>" + now.ToString("yyyy") );
//Month
strBuilder.AppendLine("MONTH ");
strBuilder.AppendLine ("now.ToString(\"%M\") ==> " + now.ToString("%M") );
strBuilder.AppendLine ("now.ToString(\"MM\") ==> " + now.ToString("MM") );
strBuilder.AppendLine ("now.ToString(\"MMM\") ==>" + now.ToString("MMM") );
strBuilder.AppendLine("now.ToString(\"MMMM\") ==>" + now.ToString("MMMM"));
//Day
strBuilder.AppendLine("Day ");
strBuilder.AppendLine("now.ToString(\"%d\") ==> " + now.ToString("%d"));
strBuilder.AppendLine("now.ToString(\"dd\") ==> " + now.ToString("dd"));
strBuilder.AppendLine("now.ToString(\"ddd\") ==>" + now.ToString("ddd"));
strBuilder.AppendLine("now.ToString(\"dddd\") ==>" + now.ToString("dddd"));
//Hour
strBuilder.AppendLine("HOUR ");
strBuilder.AppendLine("now.ToString(\"%h\") ==> " + now.ToString("%h"));
strBuilder.AppendLine("now.ToString(\"hh\") ==> " + now.ToString("hh"));
strBuilder.AppendLine("now.ToString(\"hhh\") ==>" + now.ToString("hhh"));
strBuilder.AppendLine("now.ToString(\"hhhh\") ==>" + now.ToString("hhhh"));
strBuilder.AppendLine("now.ToString(\"%H\") ==> " + now.ToString("%H"));
strBuilder.AppendLine("now.ToString(\"HH\") ==> " + now.ToString("HH"));
strBuilder.AppendLine("now.ToString(\"HHH\") ==>" + now.ToString("HHH"));
strBuilder.AppendLine("now.ToString(\"HHHH\") ==>" + now.ToString("HHHH"));
//Minutes
strBuilder.AppendLine("MINUTES ");
strBuilder.AppendLine("now.ToString(\"%m\") ==> " + now.ToString("%m"));
strBuilder.AppendLine("now.ToString(\"mm\") ==> " + now.ToString("mm"));
strBuilder.AppendLine("now.ToString(\"mmm\") ==>" + now.ToString("mmm"));
strBuilder.AppendLine("now.ToString(\"mmmm\") ==>" + now.ToString("mmmm"));
//Seconds
strBuilder.AppendLine("SECONDS ");
strBuilder.AppendLine("now.ToString(\"%s\") ==> " + now.ToString("%s"));
strBuilder.AppendLine("now.ToString(\"ss\") ==> " + now.ToString("ss"));
strBuilder.AppendLine("now.ToString(\"sss\") ==>" + now.ToString("sss"));
strBuilder.AppendLine("now.ToString(\"ssss\") ==>" + now.ToString("ssss"));
//Milliseconds
strBuilder.AppendLine("MILLISECONDS ");
strBuilder.AppendLine("now.ToString(\"%f\") ==> " + now.ToString("%f"));
strBuilder.AppendLine("now.ToString(\"ff\") ==> " + now.ToString("ff"));
strBuilder.AppendLine("now.ToString(\"fff\") ==>" + now.ToString("fff"));
strBuilder.AppendLine("now.ToString(\"ffff\") ==>" + now.ToString("ffff"));
strBuilder.AppendLine("now.ToString(\"%F\") ==> " + now.ToString("%F"));
strBuilder.AppendLine("now.ToString(\"FF\") ==> " + now.ToString("FF"));
strBuilder.AppendLine("now.ToString(\"FFF\") ==>" + now.ToString("FFF"));
strBuilder.AppendLine("now.ToString(\"FFFF\") ==>" + now.ToString("FFFF"));
//Kind
strBuilder.AppendLine("KIND ");
strBuilder.AppendLine("now.ToString(\"%K\") ==> " + now.ToString("%K"));
strBuilder.AppendLine("now.ToString(\"KK\") ==> " + now.ToString("KK"));
strBuilder.AppendLine("now.ToString(\"KKK\") ==>" + now.ToString("KKK"));
strBuilder.AppendLine("now.ToString(\"KKKK\") ==>" + now.ToString("KKKK"));
//TimeZone
strBuilder.AppendLine("TIMEZONE ");
strBuilder.AppendLine("now.ToString(\"%z\") ==> " + now.ToString("%z"));
strBuilder.AppendLine("now.ToString(\"zz\") ==> " + now.ToString("zz"));
strBuilder.AppendLine("now.ToString(\"zzz\") ==>" + now.ToString("zzz"));
strBuilder.AppendLine("now.ToString(\"zzzz\") ==>" + now.ToString("zzzz"));
//Other
strBuilder.AppendLine("OTHER ");
strBuilder.AppendLine("now.ToString(\"%g\") ==> " + now.ToString("%g"));
strBuilder.AppendLine("now.ToString(\"gg\") ==> " + now.ToString("gg"));
strBuilder.AppendLine("now.ToString(\"ggg\") ==>" + now.ToString("ggg"));
strBuilder.AppendLine("now.ToString(\"gggg\") ==>" + now.ToString("gggg"));
strBuilder.AppendLine("now.ToString(\"%t\") ==> " + now.ToString("%t"));
strBuilder.AppendLine("now.ToString(\"tt\") ==> " + now.ToString("tt"));
strBuilder.AppendLine("now.ToString(\"ttt\") ==>" + now.ToString("ttt"));
strBuilder.AppendLine("now.ToString(\"tttt\") ==>" + now.ToString("tttt"));
textBox1.Text = strBuilder.ToString();
Output:
now.ToString() ==> 10/21/2009 10:16:01 PM
now.ToString("d")10/21/2009
now.ToString("D")Wednesday, October 21, 2009
now.ToString("G")10/21/2009 10:16:01 PM
YEAR
now.ToString("%y") ==> 9
now.ToString("yy") ==> 09
now.ToString("yyy") ==>2009
now.ToString("yyyy") ==>2009
MONTH
now.ToString("%M") ==> 10
now.ToString("MM") ==> 10
now.ToString("MMM") ==>Oct
now.ToString("MMMM") ==>October
Day
now.ToString("%d") ==> 21
now.ToString("dd") ==> 21
now.ToString("ddd") ==>Wed
now.ToString("dddd") ==>Wednesday
HOUR
now.ToString("%h") ==> 10
now.ToString("hh") ==> 10
now.ToString("hhh") ==>10
now.ToString("hhhh") ==>10
now.ToString("%H") ==> 22
now.ToString("HH") ==> 22
now.ToString("HHH") ==>22
now.ToString("HHHH") ==>22
MINUTES
now.ToString("%m") ==> 16
now.ToString("mm") ==> 16
now.ToString("mmm") ==>16
now.ToString("mmmm") ==>16
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") ==> +05:00
now.ToString("KK") ==> +05:00+05:00
now.ToString("KKK") ==>+05:00+05:00+05:00
now.ToString("KKKK") ==>+05:00+05:00+05:00+05:00
TIMEZONE
now.ToString("%z") ==> +5
now.ToString("zz") ==> +05
now.ToString("zzz") ==>+05:00
now.ToString("zzzz") ==>+05: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
No comments:
Post a Comment