博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Datetime 使用详解
阅读量:4695 次
发布时间:2019-06-09

本文共 8081 字,大约阅读时间需要 26 分钟。

获得当前系统时间: DateTime dt = DateTime.Now;Environment.TickCount可以得到“系统启动到现在”的毫秒值DateTime now = DateTime.Now;Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式输出sConsole.WriteLine(dt.ToString());    //  26/11/2009 AM 11:21:30Console.WriteLine(dt.ToFileTime().ToString());   //   129036792908014024// Converts the value of the current System.DateTime object to a Windows file timeConsole.WriteLine(dt.ToFileTimeUtc().ToString());  //     129036792908014024// Converts the value of the current System.DateTime object to a Windows file timeConsole.WriteLine(dt.ToLocalTime().ToString());   //       26/11/2009 AM 11:21:30// Converts the value of the current System.DateTime object to local time.Console.WriteLine(dt.ToLongDateString().ToString());   //      2009年11月26日Console.WriteLine(dt.ToLongTimeString().ToString());  //      AM 11:21:30Console.WriteLine(dt.ToOADate().ToString());   //      40143.4732731597Console.WriteLine(dt.ToShortDateString().ToString());   //     26/11/2009Console.WriteLine(dt.ToShortTimeString().ToString());   //     AM 11:21Console.WriteLine(dt.ToUniversalTime().ToString());   //       26/11/2009 AM 3:21:30Console.WriteLine(dt.Year.ToString());   //        2009Console.WriteLine(dt.Date.ToString());   //        26/11/2009 AM 12:00:00Console.WriteLine(dt.DayOfWeek.ToString());  //       ThursdayConsole.WriteLine(dt.DayOfYear.ToString());   //       330Console.WriteLine(dt.Hour.ToString());       //        11Console.WriteLine(dt.Millisecond.ToString());   //     801        (毫秒)Console.WriteLine(dt.Minute.ToString());   //      21Console.WriteLine(dt.Month.ToString());   //       11Console.WriteLine(dt.Second.ToString());   //      30Console.WriteLine(dt.Ticks.ToString());   //时间刻度(唯一值)       633948312908014024Console.WriteLine(dt.TimeOfDay.ToString());   //       12:29:51.5181524// Gets the time of day for this instance.// 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight.Console.WriteLine(dt.ToString());     //     26/11/2009 PM 12:29:51Console.WriteLine(dt.AddYears(1).ToString());    //         26/11/2010 PM 12:29:51Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51Console.WriteLine(dt.AddHours(1.1).ToString());    //       26/11/2009 PM 1:35:51Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51Console.WriteLine(dt.AddSeconds(1.1).ToString());    //     26/11/2009 PM 12:29:52Console.WriteLine(dt.AddMinutes(1.1).ToString());    //     26/11/2009 PM 12:30:57Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:51Console.WriteLine(dt.CompareTo(dt).ToString());    //       0Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString());    // 加上一个时间段(注:System.TimeSpan为一个时间段,构造函数如下public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.                           //nanosecond:十亿分之一秒   new TimeSpan(10,000,000)        为一秒public TimeSpan(int hours, int minutes, int seconds);public TimeSpan(int days, int hours, int minutes, int seconds);public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);)Console.WriteLine(dt.Equals("2005-11-6 16:11:04").ToString());     //        FalseConsole.WriteLine(dt.Equals(dt).ToString());    //      TrueConsole.WriteLine(dt.GetHashCode().ToString());    //       1103291775Console.WriteLine(dt.GetType().ToString());    //       System.DateTimeConsole.WriteLine(dt.GetTypeCode().ToString());    //       DateTime  long Start = Environment.TickCount;   //单位是毫秒long End = Environment.TickCount;Console.WriteLine("Start is : "+Start);Console.WriteLine("End is : "+End);Console.WriteLine("The Time is {0}",End-Start);Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString());    //2009-11-26T13:29:06Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString());    //PM 1:29Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString());    //2009年11月Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString());    //2009年11月26日Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString());    //星期四, 26 十一月, 2009Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString());    //26 十一月, 2009Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString());    //星期四 2009 11 26Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString());    //26 十一月Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString());    //2009年11月26日 PM 1:29Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString());    //26/11/2009 PM 1:29Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString());    //Thu, 26 Nov 2009 13:29:06 GMT(注:常用的日期时间格式:格式 说明      输出格式 d 精简日期格式 MM/dd/yyyy D 详细日期格式 dddd, MMMM dd, yyyy f  完整格式    (long date + short time) dddd, MMMM dd, yyyy HH:mm F 完整日期时间格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss g 一般格式 (short date + short time) MM/dd/yyyy HH:mm G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss m,M 月日格式 MMMM dd s 适中日期时间格式 yyyy-MM-dd HH:mm:ss t 精简时间格式 HH:mm T 详细时间格式 HH:mm:ss)Console.WriteLine(string.Format("{0:d}", dt));    //28/12/2009Console.WriteLine(string.Format("{0:D}", dt));    //2009年12月28日Console.WriteLine(string.Format("{0:f}", dt));    //2009年12月28日 AM 10:29Console.WriteLine(string.Format("{0:F}", dt));    //2009年12月28日 AM 10:29:18Console.WriteLine(string.Format("{0:g}", dt));    //28/12/2009 AM 10:29Console.WriteLine(string.Format("{0:G}", dt));    //28/12/2009 AM 10:29:18Console.WriteLine(string.Format("{0:M}", dt));    //28 十二月Console.WriteLine(string.Format("{0:R}", dt));    //Mon, 28 Dec 2009 10:29:18 GMTConsole.WriteLine(string.Format("{0:s}", dt));    //2009-12-28T10:29:18Console.WriteLine(string.Format("{0:t}", dt));    //AM 10:29Console.WriteLine(string.Format("{0:T}", dt));    //AM 10:29:18Console.WriteLine(string.Format("{0:u}", dt));    //2009-12-28 10:29:18ZConsole.WriteLine(string.Format("{0:U}", dt));    //2009年12月28日 AM 2:29:18Console.WriteLine(string.Format("{0:Y}", dt));    //2009年12月Console.WriteLine(string.Format("{0}", dt));    //28/12/2009 AM 10:29:18Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt));    //200912281029182047计算2个日期之间的天数差DateTime dt1 = Convert.ToDateTime("2007-8-1");    DateTime dt2 = Convert.ToDateTime("2007-8-15");   TimeSpan span = dt2.Subtract(dt1);              int dayDiff = span.Days ;                    计算某年某月的天数int days = DateTime.DaysInMonth(2009, 8);       days = 31;                                      给日期增加一天、减少一天DateTime dt =DateTime.Now;dt.AddDays(1); //增加一天 dt本身并不改变dt.AddDays(-1);//减少一天 dt本身并不改变参数format格式详细用法:格式字符关联属性/说明dShortDatePatternDLongDatePatternf完整日期和时间(长日期和短时间)FFullDateTimePattern(长日期和长时间)g常规(短日期和短时间)G常规(短日期和长时间)m、MMonthDayPatternr、RRFC1123Patterns使用当地时间的SortableDateTimePattern(基于ISO8601)tShortTimePatternTLongTimePatternuUniversalSortableDateTimePattern用于显示通用时间的格式U使用通用时间的完整日期和时间(长日期和长时间)y、YYearMonthPattern下面列出可被合并以构造自定义模式的模式:d月中的某一天。一位数的日期没有前导零。dd月中的某一天。一位数的日期有一个前导零。ddd周中某天的缩写名称,在AbbreviatedDayNames中定义。dddd周中某天的完整名称,在DayNames中定义。M月份数字。一位数的月份没有前导零。MM月份数字。一位数的月份有一个前导零。MMM月份的缩写名称,在AbbreviatedMonthNames中定义。MMMM月份的完整名称,在MonthNames中定义。y不包含纪元的年份。如果不包含纪元的年份小于10,则显示不具有前导零的年份。yy不包含纪元的年份。如果不包含纪元的年份小于10,则显示具有前导零的年份。yyyy包括纪元的四位数的年份。gg时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。h12小时制的小时。一位数的小时数没有前导零。hh12小时制的小时。一位数的小时数有前导零。H24小时制的小时。一位数的小时数没有前导零。HH24小时制的小时。一位数的小时数有前导零。m分钟。一位数的分钟数没有前导零。mm分钟。一位数的分钟数有一个前导零。s秒。一位数的秒数没有前导零。ss秒。一位数的秒数有一个前导零。f秒的小数精度为一位。其余数字被截断。ff秒的小数精度为两位。其余数字被截断。fff秒的小数精度为三位。其余数字被截断。ffff秒的小数精度为四位。其余数字被截断。fffff秒的小数精度为五位。其余数字被截断。ffffff秒的小数精度为六位。其余数字被截断。fffffff秒的小数精度为七位。其余数字被截断。t在AMDesignator或PMDesignator中定义的AM/PM指示项的第一个字符(如果存在)。tt在AMDesignator或PMDesignator中定义的AM/PM指示项(如果存在)。z时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。zz时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。zzz完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。:在TimeSeparator中定义的默认时间分隔符。/在DateSeparator中定义的默认日期分隔符。%c其中c是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。\c其中c是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。

转载:https://www.cnblogs.com/tmdsleep/p/5700058.html

转载于:https://www.cnblogs.com/zpblogs/p/9809973.html

你可能感兴趣的文章
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>
用户画像展示
查看>>
C#中StreamReader读取中文出现乱码
查看>>
使用BufferedReader的时候出现的问题
查看>>