今天遇到了一個關於日期的問題,需要將2016-6-15 20:23:23這種形式的時間字元串轉換為2016年6月15日,由於學習經驗少,但是懵逼了0.0, 後來百度找到了相關方法。同時,也將常用的日期轉換方法的用法保存下來。供大家學習查閱。 我的代碼: 運行結果: ...
今天遇到了一個關於日期的問題,需要將2016-6-15 20:23:23這種形式的時間字元串轉換為2016年6月15日,由於學習經驗少,但是懵逼了0.0,
後來百度找到了相關方法。同時,也將常用的日期轉換方法的用法保存下來。供大家學習查閱。
我的代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 常用的時間格式 { class Program { static void Main(string[] args) { DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString()); Console.WriteLine("轉換後的格式為:"); Console.WriteLine("ToShortDateString" + dt.ToShortDateString()); Console.WriteLine("ToLongDateString" + dt.ToLongDateString()); Console.WriteLine("ToFileTime" + dt.ToFileTime().ToString()); Console.WriteLine("ToFileTimeUtc" + dt.ToFileTimeUtc().ToString()); Console.WriteLine("ToLocalTime" + dt.ToLocalTime().ToString()); Console.WriteLine("ToLongDateString" + dt.ToLongDateString().ToString()); Console.WriteLine("ToLongTimeString" + dt.ToLongTimeString().ToString()); Console.WriteLine("ToOADate" + dt.ToOADate().ToString()); Console.WriteLine("ToShortDateString" + dt.ToShortDateString().ToString()); Console.WriteLine("ToShortTimeString" + dt.ToShortTimeString().ToString()); Console.WriteLine("ToUniversalTime" + dt.ToUniversalTime().ToString()); Console.WriteLine("Year" + dt.Year.ToString()); Console.WriteLine("Date" + dt.Date.ToString()); Console.WriteLine("DayOfWeek" + dt.DayOfWeek.ToString()); Console.WriteLine("DayOfYear" + dt.DayOfYear.ToString()); Console.WriteLine("Hour" + dt.Hour.ToString()); Console.WriteLine("Millisecond" + dt.Millisecond.ToString()); Console.WriteLine("Minute" + dt.Minute.ToString()); Console.WriteLine("Month" + dt.Month.ToString()); Console.WriteLine("Second" + dt.Second.ToString()); Console.WriteLine("Ticks" + dt.Ticks.ToString()); Console.WriteLine("TimeOfDay"+dt.TimeOfDay.ToString()); Console.WriteLine("AddYears(1)"+dt.AddYears(1).ToString()); Console.WriteLine("AddDays(1.1)"+dt.AddDays(1.1).ToString()); Console.WriteLine("AddHours(1.1)"+dt.AddHours(1.1).ToString()); Console.WriteLine("AddMilliseconds"+dt.AddMilliseconds(1.1).ToString()); Console.WriteLine("AddMonths(1)"+dt.AddMonths(1).ToString()); Console.WriteLine("AddSeconds(1.1)"+dt.AddSeconds(1.1).ToString()); Console.WriteLine("AddMinutes(1.1)"+dt.AddMinutes(1.1).ToString()); Console.WriteLine("AddTicks(1000)"+dt.AddTicks(1000).ToString()); Console.WriteLine("CompareTo"+dt.CompareTo(dt).ToString()); Console.WriteLine("Equals(2005-11-6 16:11:04)" + dt.Equals("2005-11-6 16:11:04").ToString()); Console.WriteLine("Equals(dt)"+dt.Equals(dt).ToString()); Console.WriteLine("GetHashCode"+dt.GetHashCode().ToString()); Console.WriteLine("GetType"+dt.GetType().ToString()); Console.WriteLine("GetTypeCode"+dt.GetTypeCode().ToString()); Console.WriteLine("GetDateTimeFormats(s)[0]"+dt.GetDateTimeFormats('s')[0].ToString()); Console.WriteLine("GetDateTimeFormats(t)[0]"+dt.GetDateTimeFormats('t')[0].ToString()); Console.WriteLine("GetDateTimeFormats(y)[0]"+dt.GetDateTimeFormats('y')[0].ToString()); Console.WriteLine("GetDateTimeFormats(D)[0]"+dt.GetDateTimeFormats('D')[0].ToString()); Console.WriteLine("GetDateTimeFormats(D)[1]"+dt.GetDateTimeFormats('D')[1].ToString()); Console.WriteLine("GetDateTimeFormats('D')[2]"+dt.GetDateTimeFormats('D')[2].ToString()); Console.WriteLine("GetDateTimeFormats('D')[3]"+dt.GetDateTimeFormats('D')[3].ToString()); Console.WriteLine("GetDateTimeFormats('M')[0]"+dt.GetDateTimeFormats('M')[0].ToString()); Console.WriteLine("GetDateTimeFormats('f')[0]"+dt.GetDateTimeFormats('f')[0].ToString()); Console.WriteLine("GetDateTimeFormats('g')[0]"+dt.GetDateTimeFormats('g')[0].ToString()); Console.WriteLine("GetDateTimeFormats('r')[0]" + dt.GetDateTimeFormats('r')[0].ToString()); Console.WriteLine("Format({0:d}"+string.Format("{0:d}", dt)); Console.WriteLine("Format({0:D}" + string.Format("{0:D}", dt)); Console.WriteLine("Format({0:f}" + string.Format("{0:f}", dt)); Console.WriteLine("Format({0:F}" + string.Format("{0:F}", dt)); Console.WriteLine("Format({0:g}" + string.Format("{0:g}", dt)); Console.WriteLine("Format({0:G}" + string.Format("{0:G}", dt)); Console.WriteLine("Format({0:M}" + string.Format("{0:M}", dt)); Console.WriteLine("Format({0:R}" + string.Format("{0:R}", dt)); Console.WriteLine("Format({0:s}" + string.Format("{0:s}", dt)); Console.WriteLine("Format({0:t}" + string.Format("{0:t}", dt)); Console.WriteLine("Format({0:T}" + string.Format("{0:T}", dt)); Console.WriteLine("Format({0:u}" + string.Format("{0:u}", dt)); Console.WriteLine("Format({0:U}" + string.Format("{0:U}", dt)); Console.WriteLine("Format({0:Y}" + string.Format("{0:Y}", dt)); Console.WriteLine("Format({0}" + string.Format("{0}", dt)); Console.WriteLine("Format({0:yyyyMMddHHmmssffff}" + string.Format("{0:yyyyMMddHHmmssffff}", dt)); Console.Read(); } } }
運行結果: