日期:2014-05-17  浏览次数:20472 次

求助.net时间格式化问题
我要获取到当前时间并且格式化成为年月日时分秒的样子比如20120623 15:16:00
 求助大神

------解决方案--------------------
string str=String.Replace(DateTime.NOW.ToLongString(),"-","");
------解决方案--------------------
DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
------解决方案--------------------
探讨

DateTime.Now.ToString("yyyyMMdd HH:mm:ss");

------解决方案--------------------
DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
------解决方案--------------------
Response.Write(DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
------解决方案--------------------
C# code

string date = DateTime.Now.ToString("yyyyMMdd hh:mm:ss");
Console.WriteLine(date);
//20120628 03:23:36

------解决方案--------------------
探讨

DateTime.Now.ToString("yyyyMMdd HH:mm:ss");

------解决方案--------------------
楼上的都对了,送个完整的给你

DateTime dt = DateTime.Now; 
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 
?2005-11-5 13:30:28.4412864 
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