您的位置:首页 > 编程语言 > C#

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

2010-08-13 15:35 996 查看
C#格式化日期时间

DateTimedt=DateTime.Now;

DateTime.Now.ToString("dddMMddyyyyHH:mm:ss.ffffff")
Label1.Text=dt.ToString();//2005-11-513:21:25
Label2.Text=dt.ToFileTime().ToString();//127756416859912816
Label3.Text=dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text=dt.ToLocalTime().ToString();//2005-11-521: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-55:21:25

Label1.Text=dt.Year.ToString();//2005
Label2.Text=dt.Date.ToString();//2005-11-50: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-513:47:04
Label2.Text=dt.AddYears(1).ToString();//2006-11-513:47:04
Label3.Text=dt.AddDays(1.1).ToString();//2005-11-616:11:04
Label4.Text=dt.AddHours(1.1).ToString();//2005-11-514:53:04
Label5.Text=dt.AddMilliseconds(1.1).ToString();//2005-11-513:47:04
Label6.Text=dt.AddMonths(1).ToString();//2005-12-513:47:04
Label7.Text=dt.AddSeconds(1.1).ToString();//2005-11-513:47:05
Label8.Text=dt.AddMinutes(1.1).ToString();//2005-11-513:48:10
Label9.Text=dt.AddTicks(1000).ToString();//2005-11-513:47:04
Label10.Text=dt.CompareTo(dt).ToString();//0
//Label11.Text=dt.Add(?).ToString();//问号为一个时间段
Label1.Text=dt.Equals("2005-11-616: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();//20051105
Label6.Text=dt.GetDateTimeFormats('D')[2].ToString();//星期六20051105
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-514:06
Label11.Text=dt.GetDateTimeFormats('r')[0].ToString();//Sat,05Nov200514:06:25GMT

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-514:23
Label6.Text=?string.Format("{0:G}",dt);//2005-11-514:23:23
Label7.Text=?string.Format("{0:M}",dt);//11月5日
Label8.Text=?string.Format("{0:R}",dt);//Sat,05Nov200514:23:23GMT
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-0514: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-514: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/30th(%d)

yyyy:1985/2012;(%Y)

yy:85(%y)

HH:mm:ss:18:23:55

hh:mm:ssffffftt:06:23:5523423PM

hh12hours(%I)

HH24hours(%H)

mm(%M)

ss(%S)

fffff

tt:AM/PM(%p)

//createdatetime2008-03-0916:05:07.123
DateTimedt=newDateTime(2008,3,9,16,5,7,123);

String.Format("{0:yyyyyyyyyy}",dt);//"8080082008"year
String.Format("{0:MMMMMMMMMM}",dt);//"303MarMarch"month
String.Format("{0:dddddddddd}",dt);//"909SunSunday"day
String.Format("{0:hhhHHH}",dt);//"4041616"hour12/24
String.Format("{0:mmm}",dt);//"505"minute
String.Format("{0:sss}",dt);//"707"second
String.Format("{0:ffffffffff}",dt);//"1121231230"sec.fraction
String.Format("{0:FFFFFFFFFF}",dt);//"112123123"withoutzeroes
String.Format("{0:ttt}",dt);//"PPM"A.M.orP.M.
String.Format("{0:zzzzzz}",dt);//"-6-06-06:00"timezone


//dateseparatoringermancultureis"."(so"/"changesto".")
String.Format("{0:d/M/yyyyHH:mm:ss}",dt);//"9/3/200816:05:07"-english(en-US)
String.Format("{0:d/M/yyyyHH:mm:ss}",dt);//"9.3.200816:05:07"-german(de-DE)

//month/daynumberswithout/withleadingzeroes
String.Format("{0:M/d/yyyy}",dt);//"3/9/2008"
String.Format("{0:MM/dd/yyyy}",dt);//"03/09/2008"

//day/monthnames
String.Format("{0:ddd,MMMd,yyyy}",dt);//"Sun,Mar9,2008"
String.Format("{0:dddd,MMMMd,yyyy}",dt);//"Sunday,March9,2008"

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

//Followingexamplesshowusageofstandardformat//specifiersinString.Formatmethodandtheresultingoutput.

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


数字:

doubled=35.2342351

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

//StringFormatforDouble[C#]

//justtwodecimalplaces
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.twodecimalplaces
String.Format("{0:0.##}",123.4567);//"123.46"
String.Format("{0:0.##}",123.4);//"123.4"
String.Format("{0:0.##}",123.0);//"123"

//atleasttwodigitsbeforedecimalpoint
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"

//Thousandsseparator
//Toformatdoubletostringwithuseofthousandsseparatorusezeroandcommaseparatorbeforeanusualfloatformattingpattern,e.g.pattern„0,0.0“formatsthenumbertousethousandsseparatorsandtohaveonedecimalplace.

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);//""

//Alignnumberswithspaces
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)

doubled1=12345.1234;
doubled2=12345678.12345678;
doubled3=-123.123;
doubled4=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~
+:forcetoprecedetheresultwithaplusorminussign(+or-).
-:Left-justifywithinthegivenfieldwidtd;
08:thetotalminimumwithofthefield,suchas%+08.03lf+001.123

03:thefixedprecision.


"%04.4d"Sointhiscase,%04.4d
,the[code].4specifiesthatallfourdigitsofthenumbershouldbeprinted.Ofcourse,the
04
partjustpadsthenumberwithleadingzerosifitislessthan1000.
However,inthiscase,astheabovemanualpagestates,
[/code]
%[flags][width][.precision][length]specifier

intt=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





//Customformattingfornegativenumbersandzero
//Ifyouneedtousecustomformatfornegativefloatnumbersorzero,usesemicolonseparator„;“tosplitpatterntothreesections.Thefirstsectionformatspositivenumbers,thesecondsectionformatsnegativenumbersandthethirdsectionformatszero.Ifyouomitthelastsection,zerowillbeformattedusingthefirstsection.
String.Format("{0:0.00;minus0.00;zero}",123.4567);//"123.46"
String.Format("{0:0.00;minus0.00;zero}",-123.4567);//"minus123.46"
String.Format("{0:0.00;minus0.00;zero}",0.0);//"zero"

//Somefunnyexamples
String.Format("{0:mynumberis0.0}",12.3);//"mynumberis12.3"
String.Format("{0:0aaa.bbb0}",12.3);//"12aaa.bbb3"


//StringFormatforInt[C#]

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

//Alignnumbertotherightorleft
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"

//inttoHex16进制

intvalue;

value=0x2045e;
Console.WriteLine(value.ToString("x"));
//Displays2045e
Console.WriteLine(value.ToString("X"));
//Displays2045E
Console.WriteLine(value.ToString("X8"));{0:x8}
//Displays0002045E

value=123456789;
Console.WriteLine(value.ToString("X"));
//Displays75BCD15
Console.WriteLine(value.ToString("X2"));
//Displays75BCD15



String.Format("{0:#;minus#}",15);//"15"
String.Format("{0:#;minus#}",-15);//"minus15"
String.Format("{0:#;minus#;zero}",0);//"zero"

String.Format("{0:+############}",447900123456);//"+447900123456"
String.Format("{0:##-####-####}",8958712551);//"89-5871-2551"


字符串

string.Format("{0,-10}",str);toleft.string.Format("{0,10}",str);toright.

ToLimitedthemaximumlengthofthestring:

1.Formateachstringtonomorethantencharactersviatruncation.

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

Thesyntaxofaformatitemisasfollows:

{index[,length][:formatString]}

Elementsinsquarebracketsareoptional.Thefollowingtabledescribeseachelement.Formoreinformationaboutthecompositeformattingfeature,includingthesyntaxofaformatitem,seeCompositeFormatting.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: