你的位置:首页 > ASP.net教程

[ASP.net教程]常用的日期格式


  今天遇到了一个关于日期的问题,需要将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();    }  }}

运行结果: