时间格式转换
怎样才能把日期从2009-8-26 13:43:52转换为2009-8-26 下午1:43:52呢,谢谢
private void Form1_Load(object sender, EventArgs e) { // 2009-8-26 13:43:52 2009-8-26 下午1:43:52 DateTime dt = DateTime.Now; int year = dt.Year; int month = dt.Month; int day = dt.Day; int hour = dt.Hour; int minute=dt.Minute; int second=dt.Second; if (hour > 12) { hour -= 12; } string time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; MessageBox.Show(time.ToString()); }
private void Form1_Load(object sender, EventArgs e) { // 2009-8-26 13:43:52 2009-8-26 下午1:43:52 DateTime dt = DateTime.Now; int year = dt.Year; int month = dt.Month; int day = dt.Day; int hour = dt.Hour; int minute=dt.Minute; int second=dt.Second; string ampm = "上午 ";//上下午 if (hour> 12) { hour -= 12; ampm = "下午 "; } string time = year + "-" + month + "-" + day + " " + ampm + hour + ":" + minute + ":" + second; MessageBox.Show(time.ToString()); }
private void Form1_Load(object sender, EventArgs e) { DateTime dt = DateTime.Now; int hour = dt.Hour; string ampm = " 上午 "; //注意上下午前后都有个空格 if (hour>= 12) { ampm = " 下午 "; } string time = dt.ToString("yyyy-MM-dd hh:mm:ss").Replace(" ",ampm); MessageBox.Show(time.ToString()); }