C# 中DateTime的各种使用

获得当前系统时间: DateTime dt = DateTime.Now;
Environment.TickCount可以得到“系统启动到现在”的毫秒值
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式输出s
Console.WriteLine(dt.ToString());    //  26/11/2009 AM 11:21:30
Console.WriteLine(dt.ToFileTime().ToString());   //   129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToFileTimeUtc().ToString());  //     129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.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:30
Console.WriteLine(dt.ToOADate().ToString());   //      40143.4732731597
Console.WriteLine(dt.ToShortDateString().ToString());   //     26/11/2009
Console.WriteLine(dt.ToShortTimeString().ToString());   //     AM 11:21
Console.WriteLine(dt.ToUniversalTime().ToString());   //       26/11/2009 AM 3:21:30
Console.WriteLine(dt.Year.ToString());   //        2009
Console.WriteLine(dt.Date.ToString());   //        26/11/2009 AM 12:00:00
Console.WriteLine(dt.DayOfWeek.ToString());  //       Thursday
Console.WriteLine(dt.DayOfYear.ToString());   //       330
Console.WriteLine(dt.Hour.ToString());       //        11
Console.WriteLine(dt.Millisecond.ToString());   //     801        (毫秒)
Console.WriteLine(dt.Minute.ToString());   //      21
Console.WriteLine(dt.Month.ToString());   //       11
Console.WriteLine(dt.Second.ToString());   //      30
Console.WriteLine(dt.Ticks.ToString());   //       633948312908014024

Console.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:51
Console.WriteLine(dt.AddYears(1).ToString());    //         26/11/2010 PM 12:29:51
Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51
Console.WriteLine(dt.AddHours(1.1).ToString());    //       26/11/2009 PM 1:35:51
Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51
Console.WriteLine(dt.AddSeconds(1.1).ToString());    //     26/11/2009 PM 12:29:52
Console.WriteLine(dt.AddMinutes(1.1).ToString());    //     26/11/2009 PM 12:30:57
Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:51
Console.WriteLine(dt.CompareTo(dt).ToString());    //       0
Console.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());     //        False
Console.WriteLine(dt.Equals(dt).ToString());    //      True
Console.WriteLine(dt.GetHashCode().ToString());    //       1103291775
Console.WriteLine(dt.GetType().ToString());    //       System.DateTime
Console.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:06
Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString());    //PM 1:29
Console.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 十一月, 2009
Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString());    //26 十一月, 2009
Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString());    //星期四 2009 11 26
Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString());    //26 十一月
Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString());    //2009年11月26日 PM 1:29
Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString());    //26/11/2009 PM 1:29
Console.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/2009
Console.WriteLine(string.Format("{0:D}", dt));    //2009年12月28日
Console.WriteLine(string.Format("{0:f}", dt));    //2009年12月28日 AM 10:29
Console.WriteLine(string.Format("{0:F}", dt));    //2009年12月28日 AM 10:29:18
Console.WriteLine(string.Format("{0:g}", dt));    //28/12/2009 AM 10:29
Console.WriteLine(string.Format("{0:G}", dt));    //28/12/2009 AM 10:29:18
Console.WriteLine(string.Format("{0:M}", dt));    //28 十二月
Console.WriteLine(string.Format("{0:R}", dt));    //Mon, 28 Dec 2009 10:29:18 GMT
Console.WriteLine(string.Format("{0:s}", dt));    //2009-12-28T10:29:18
Console.WriteLine(string.Format("{0:t}", dt));    //AM 10:29
Console.WriteLine(string.Format("{0:T}", dt));    //AM 10:29:18
Console.WriteLine(string.Format("{0:u}", dt));    //2009-12-28 10:29:18Z
Console.WriteLine(string.Format("{0:U}", dt));    //2009年12月28日 AM 2:29:18
Console.WriteLine(string.Format("{0:Y}", dt));    //2009年12月
Console.WriteLine(string.Format("{0}", dt));    //28/12/2009 AM 10:29:18
Console.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本身并不改变

C# 中DateTime的各种使用相关推荐

  1. C#中DateTime.Now.Ticks的用法和说明

    在C#中DateTime.Now.Ticks的常用于标示: 自 0001 年 1 月 1 日午夜 12:00:00以来,到当前时间为止:以0.1纳秒(1纳秒=0.00000 0001秒)为单位的时间间 ...

  2. python时间函数报错_python3中datetime库,time库以及pandas中的时间函数区别与详解...

    1介绍datetime库之前 我们先比较下time库和datetime库的区别 先说下time 在 Python 文档里,time是归类在Generic Operating System Servic ...

  3. python获取系统时间函数_python3中datetime库,time库以及pandas中的时间函数区别与详解...

    1介绍datetime库之前 我们先比较下time库和datetime库的区别 先说下time 在 Python 文档里,time是归类在Generic Operating System Servic ...

  4. 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题

    问题背景: 在使用asp.net mvc 结合jquery esayui做一个系统,但是在使用使用this.json方法直接返回一个json对象,在列表中显示时发现datetime类型的数据在转为字符 ...

  5. C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法...

    C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法 原文:C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决 ...

  6. 总结PHP中DateTime的常用方法

    本文对DateTime对象使用的方法进行了一些整理,方便大家查找和翻阅,有需要的可以参考学习. 前言 实例化对象前面加\表示的是,在命名空间中使用原生的类,如果没有使用命名空间的话,可以把前面的\给删 ...

  7. python mysql写入速度加快_解决python写入mysql中datetime类型遇到的问题

    解决python写入mysql中datetime类型遇到的问题 发布时间:2020-08-31 16:46:47 来源:脚本之家 阅读:89 作者:WilliamDescant 刚开始使用python ...

  8. python mysql驱动写入datetime类型的数据_解决python写入mysql中datetime类型遇到的问题...

    刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题: 在mysql数据库中,有一个datetime类型的字段用于存储记录的日期时间值.python程序中有对应的一个dateti ...

  9. php中datetime,珍藏 PHP中DateTime的常用方法

    前言 实例化对象前面加\表示的是,在命名空间中使用原生的类,如果没有使用命名空间的话,可以把前面的\给删除掉 输出当前时间 $datetime = new \DateTime; print_r($da ...

最新文章

  1. VMware vSphere Client(4.1/5.0/5.1/5.5/6.0) 客户端下载地
  2. 盘点海外动力电池巨头 细数日韩锂电设备企业
  3. 如何查看某个端口被哪个进程占用
  4. ElasticSearch 2 (37) - 信息聚合系列之内存与延时
  5. 在Win平台得到磁盘、CPU、用户信息
  6. 英特尔cpu发布时间表_英特尔10nm芯片开始大规模出货,先进制程时间表浮出水面...
  7. sklearn中ValueError: Unknown label type: ‘continuous‘错误解决
  8. python matplotlib画图遇到的问题——画多个子图
  9. (69)信号发生器DDS正弦波设计(一)(第14天)
  10. php 合并重复数据合并单元格,通过js合并表格重复出现的数据
  11. React Router 全部
  12. java 向后台传两个json数据类型_前台向后台传递JSON字符串,处理JSON字符串类型的方法...
  13. 分享一个外泌体数据库
  14. CRCNN PCNN
  15. 微信服务号的模板消息有哪些优势和使用限制?
  16. 微信小程序实现上传图片的功能
  17. sql server使用杂记(二)
  18. 基于Scala设计简易的会员卡管理系统
  19. ShaderJoy —— “水面波纹消散” 的实现 【GLSL】
  20. Unity存档系统——Json格式的文件

热门文章

  1. SAP LSMW 导入Open PO单据,系统报错 --- GL account 670100 cannot be used ---
  2. 资讯丨NVIDIA自造AI超级计算机:轻松进入世界前五
  3. 干货丨从硬件配置到框架选择,请以这种姿势入坑深度学习
  4. 深度学习在工业推荐如何work?Netflix这篇论文「深度学习推荐系统Netflix案例分析」阐述DL在RS的优劣与经验教训...
  5. 费米悖论的三十种解释 | 观点
  6. 张亚勤、韦乐平等综述论文:通信人工智能的下一个十年
  7. 清华微电子副所长尹首一:中国AI芯片的技术路线最全面
  8. 深度解析:国产化软硬件全景梳理
  9. AI人必看!89页全网最全清华知识图谱报告(附PDF)
  10. 百度:2020年十大科技趋势