C# format 格式化 日期时间 数字 字符串 datetime

C#格式化日期时间

DateTime dt = DateTime.Now;

DateTime.Now.ToString("ddd MM dd yyyy HH:mm:ss.ffffff")
Label1.Text = dt.ToString();                                           //2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();                      //127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();                 //127756704859912816
Label4.Text = dt.ToLocalTime().ToString();                   //2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();            //2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();            //13:21:25
Label7.Text = dt.ToOADate().ToString();                       //38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();            //2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();            //13:21
Label10.Text = dt.ToUniversalTime().ToString();             //2005-11-5 5:21:25

Label1.Text = dt.Year.ToString();                                         //2005
Label2.Text = dt.Date.ToString();                                         //2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();                             //Saturday
Label4.Text = dt.DayOfYear.ToString();                               //309
Label5.Text = dt.Hour.ToString();                                         //13
Label6.Text = dt.Millisecond.ToString();                               //441
Label7.Text = dt.Minute.ToString();                                      //30
Label8.Text = dt.Month.ToString();                                       //11
Label9.Text = dt.Second.ToString();                                      //28
Label10.Text = dt.Ticks.ToString();                                       //632667942284412864
Label11.Text = dt.TimeOfDay.ToString();                              //13:30:28.4412864
Label1.Text = dt.ToString();                                                   //2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();                              //2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();                            //2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();                           //2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();                  //2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();                            //2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();                        //2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();                         //2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();                          //2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();                          //0
//Label11.Text = dt.Add(?).ToString();                                     //问号为一个时间段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();      //False
Label2.Text = dt.Equals(dt).ToString();                                     //True
Label3.Text = dt.GetHashCode().ToString();                            //1474088234
Label4.Text = dt.GetType().ToString();                                     //System.DateTime
Label5.Text = dt.GetTypeCode().ToString();                             //DateTime

Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();          //2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();           //14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();           //2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();          //2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();          //2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();          //星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();          //星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();         //11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();           //2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();        //2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();         //Sat, 05 Nov 2005 14:06:25 GMT

Label1.Text =? string.Format("{0:d}",dt);                                   //2005-11-5
Label2.Text =? string.Format("{0:D}",dt);                                  //2005年11月5日
Label3.Text =? string.Format("{0:f}",dt);                                    //2005年11月5日 14:23
Label4.Text =? string.Format("{0:F}",dt);                                  //2005年11月5日 14:23:23
Label5.Text =? string.Format("{0:g}",dt);                                   //2005-11-5 14:23
Label6.Text =? string.Format("{0:G}",dt);                                  //2005-11-5 14:23:23
Label7.Text =? string.Format("{0:M}",dt);                                 //11月5日
Label8.Text =? string.Format("{0:R}",dt);                                  //Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text =? string.Format("{0:s}",dt);                                   //2005-11-05T14:23:23
Label10.Text = string.Format("{0:t}",dt);                                   //14:23
Label11.Text = string.Format("{0:T}",dt);                                  //14:23:23
Label12.Text = string.Format("{0:u}",dt);                                   //2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);                                  //2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);                                  //2005年11月
Label15.Text = string.Format("{0}",dt);                                      //2005-11-5 14:23:23?
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);  //yyyymm等可以设置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);

  //dt.tostring("..");      

dddd: Thursday/wendesday ;   (%A)
ddd: Thu/wen   (%a)

MMMM:August /June   ;    (%B)
MMM:Aug/Jun   (%b)

dd:02/30 th  (%d)

yyyy:1985/2012; (%Y) 

yy :85 (%y)

HH:mm:ss:18:23:55

hh:mm:ss fffff tt:06:23:55 23423 PM

 

hh 12hours  (%I)

HH 24hours  (%H)

mm (%M)

ss (%S)

fffff  

tt:AM/PM  (%p)

// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"


//Following examples show usage of standard format //specifiers in String.Format method and the resulting output.

String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                      LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"          LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"  LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"                ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"             ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                        MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                     YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"             SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"            UniversalSortableDateTime

 

 

数字:

double d=35.2342351

string.Format("{0,-8:0.0000}",d); -8:width,0.0000fraction

 

//String Format for Double [C#]

// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

//Thousands separator
//To format double to string with use of thousands separator use zero and comma separator before an usual float formatting pattern, e.g. pattern „0,0.0“ formats the number to use thousands separators and to have one decimal place.

String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67);       // "12,346"

//ZERO
String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0);            // "0"
String.Format("{0:#.0}", 0.0);            // ".0"
String.Format("{0:#.#}", 0.0);            // ""

//Align numbers with spaces
String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "
String.Format("{0,8:0.000000}",3.23); //%08.06lf
string.Format("{0:+00.00000;-00.00000;0.0}", 123.1234); //+123.12340
C++ %12.4lf == C# {1,12:0.0000}  0x{6:x}==0x{6:x}
%-32.32s=={0,-32}, lp.Name.MaxLength(32)

double d1=12345.1234;
double d2=12345678.12345678;
double d3=-123.123;
double d4=123456789.12345678;


printf("%%08.06lf: ~%08.06lf~\n" ,d1);   //%08.06lf: ~12345.123400~
printf("%%08.06lf: ~%08.06lf~\n" ,d2);   //%08.06lf: ~12345678.123457~
printf("%%+08.06lf: ~%+08.06lf~\n" ,d3); //%+08.06lf: ~-123.123000~
printf("%%+08.06lf: ~%+08.06lf~\n" ,d4); //%+08.06lf: ~+123456789.123457~

printf("%%+08.03lf: ~%+08.03lf~\n" ,1.1234);  //%+08.06lf: ~+001.123~        
+:force to precede the result with aplus or minus sign(+ or -).
-:Left-justify with in the given field widtd;
08: the total minimum with of the field ,such as %+08.03lf +001.123

 03: the fixed precision.


"%04.4d"   So in this case, %04.4d, the .4 specifies that all four digits of the number should be printed. Of course, the 04 part just pads the number with leading zeros if it is less than 1000. However, in this case, as the above manual page states,
%[flags][width][.precision][length]specifier
    int t =123;
    printf("%%04.4d:\t""%04.4d\n", t);
    printf("%%4.4d:\t"   "%4.4d\n", t);
    printf("%%04d:\t"     "%04d\n", t);
    printf("%%4d:\t"       "%4d\n", t);
C:\Temp> z
%04.4d: 0123
%4.4d:  0123
%04d:   0123
%4d:     123
 
 


//Custom formatting for negative numbers and zero //If you need to use custom format for negative float numbers or zero, use semicolon separator „;“ to split pattern to three sections. The first section formats positive numbers, the second section formats negative numbers and the third section formats zero. If you omit the last section, zero will be formatted using the first section. String.Format("{0:0.00;minus 0.00;zero}", 123.4567); // "123.46" String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46" String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero" //Some funny examples String.Format("{0:my number is 0.0}", 12.3); // "my number is 12.3" String.Format("{0:0aaa.bbb0}", 12.3); // "12aaa.bbb3"

 

//String Format for Int [C#]

//Add zeroes before number
String.Format("{0:00000}", 15);          // "00015"   C++: %03d  %04d  %08d
String.Format("{0:00000}", -15);         // "-00015"  

//Align number to the right or left
String.Format("{0,5}", 15);              // "   15"     %5d
String.Format("{0,-5}", 15);             // "15   "     %-5d
String.Format("{0,5:000}", 15);          // "  015"     
String.Format("{0,-5:000}", 15);         // "015  "

//int to Hex 16进制
int value; 

value = 0x2045e;
Console.WriteLine(value.ToString("x"));
// Displays 2045e
Console.WriteLine(value.ToString("X"));
// Displays 2045E
Console.WriteLine(value.ToString("X8"));  {0:x8}
// Displays 0002045E

value = 123456789;
Console.WriteLine(value.ToString("X"));
// Displays 75BCD15
Console.WriteLine(value.ToString("X2"));
// Displays 75BCD15


String.Format("{0:#;minus #}", 15); // "15" String.Format("{0:#;minus #}", -15); // "minus 15" String.Format("{0:#;minus #;zero}", 0); // "zero" String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456" String.Format("{0:##-####-####}", 8958712551); // "89-5871-2551"

 

字符串

string.Format("{0,-10}",str);to left.        string.Format("{0,10}",str);to right.

To Limited the maximum length of the string:

    • 1. Format each string to no more than ten characters via truncation.

      string test = "chachachachacha";
      test = test.Substring(0, 10);
      Console.WriteLine(test); // yields "chachachac"

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element. For more information about the composite formatting feature, including the syntax of a format item, see Composite Formatting.

 

posted on 2010-08-13 15:35  Henry_Wang  阅读(1334)  评论(0编辑  收藏  举报

导航