常用的日期格式
今天遇到了一个关于日期的问题,需要将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(); } } }
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话