日期类型的格式化和文件大小的格式化

日期格式化

格式化字符串的方法:《String.Format()》

日期格式化的参考:《标准日期和时间格式字符串》

日期类型的格式化中,长格式与短格式指操作系统的区域选项的格式设置。

日期类型的格式化和文件大小的格式化

date formatting

Date formats are very dependant on the culture information passed. The examples below are shown using the UK culture.


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

文件大小格式化

写了3个扩展方法,用来将long类型格式化成1024为权的字节表示法。

使用方法:

long size=10000000000;

string r1=size.FormatFileSize();

string r2=size.FormatFileSize(MyExtendMethod.FormatFileSizeUnit.KB);

string r3=size.FormatFileSizeWithoutUnit(MyExtendMethod.FormatFileSizeUnit.KB);

结果分别为:

r1: "9 GB"

r2: "9,765,625 KB"

r3: "9,765,625"

源码和演示

日期类型的格式化和文件大小的格式化

你可能感兴趣的:(F#)