2013年5月7日 星期二

[C#] DateTime convert String

Using DateTime.ToString(string format) to covert it to string. The following is:
y: represents the year
M: represents the month
d: represents the day
h, H: represents the hour: h with a 12-hour; H with a 24-hour clock.
m: represents the minute
s: represents the second

Format Output Samples
year
y 13-04 (if now is 2013-04-28)
 6-04 (if now is 2006-04-28)
string str = DateTime.Now.ToString("y-MM");
yy 13-05 string yy = DateTime.Now.ToString("yy-MM");
yyyy 2013 string yy = DateTime.Now.ToString("yyyy");
month
M 2013-5 string mon = DateTime.Parse("2013-05-09").ToString("yyyy-M");
MM 05 string mon = DateTime.Parse("2013-05-09").ToString("MM");
MMM English OS: Jul Chinese OS: 七月 string mon = DateTime.Parse("2013-07-01").ToString("MMM");
MMMM English OS: July
Chinese OS: 七月
string mon = DateTime.Parse("2013-07-01").ToString("MMMM");
date & week
d 5/9/2013 string dd= DateTime.Parse("2013-05-09").ToString("d");
dd 09 string dd= DateTime.Parse("2013-05-09").ToString("dd");
ddd English OS:Thu
Chinese OS: 星期四
string dd = DateTime.Parse("2013-05-09").ToString("ddd");
dddd English OS:Thursday
Chinese OS: 星期四
string dd = DateTime.Parse("2006-07-01").ToString("dddd")
hour
hh 01 string hh = DateTime.Now.ToString("hh");
H 2013-13 string hh = DateTime.Now.ToString("yyyy-H");
HH 2013-13
2006-18
string hh = DateTime.Now.ToString("yyyy-HH");
string hh = DateTime.Parse("2006-7-4 18:00:00").ToString("yyyy-HH");
minute
m 2013-05-07-15 string mm =  DateTime.Now.ToString("yyyy-MM-dd-m");
mm 2013-05-07-15 string mm =  DateTime.Now.ToString("yyyy-MM-dd-mm");
second
s 2013-05-07-27 string ss =  DateTime.Now.ToString("yyyy-MM-dd-s");
ss 2013-05-07-27 string ss =  DateTime.Now.ToString("yyyy-MM-dd-ss");

沒有留言:

張貼留言