//获得当前系统时间:
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());   //       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: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());    //       DateTimelong 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. Python时间库—datetime的详解及使用

    一.概述 datetime库定义了2个常量和5个类.   常量名/类名 描述 2个常量 MINYEAR=1   AXYEAR=9999   5个类 date类 表示日期的类 time类 表示时间的类 ...

  2. Python标准库datetime之datetime模块详解

    Python标准库datetime之datetime模块详解 1.日期时间对象 日期时间对象是指具有日期(年月日)和时间(时分秒)双重属性的实例 日期时间对象的类型为datetime.datetime ...

  3. joi模块验证日期格式_python datetime模块详解

    一.datetime模块介绍 通过print(dir(datetime)),我们可以看到模块内属性和类: ['MAXYEAR', 'MINYEAR', '__builtins__', '__cache ...

  4. C#时间格式化(Datetime)用法详解

    Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...

  5. 刻意练习:Python基础 -- Task13. datetime模块详解

    datetime模块 datetime 是 Python 中处理日期的标准模块,它提供了 4 种对日期和时间进行处理的类:datetime.date.time 和 timedelta. datetim ...

  6. Java 8 Date-Time API 详解

    从Java版本1.0开始就支持日期和时间,主要通过java.util.Date类. 但是,Date类设计不佳. 例如,Date中的月份从1开始,但从日期却从0开始.在JDK 1.1中使用它的许多方法已 ...

  7. 【python】 time模块和datetime模块详解 【转】

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  8. DateTime 操作详解

    /大家在做报表或查询的时候都会有给用户预设一些可选的日期范围 2                //如本年度销售额.本季度利润.本月新增客户 3                //C#里内置的Date ...

  9. Python||datetime.timedelta()详解,核心是minutes与minute

    datetime官方文档 from datetime import datetime,timedelta timedelta代表两个datetime之间的时间差. class datetime.tim ...

  10. mysql datetime详解,MySQL datetime类型详解

    研发反馈问题,数据库中datetime数据类型存储的值末尾会因四舍五入出现不一致数据,影响查询结果,比如:程序中自动获取带毫秒精度的日期'2019-03-05 01:53:55.63',存入数据库后变 ...

最新文章

  1. Python入门教程:很多人推荐学 Python 入 IT ,如果学完 Python 找不到工作怎么办...
  2. RxJava 常用总结以及原理理解
  3. 开始我的c++学习之路
  4. html css盒子顶层,HTML学习之四CSS盒子
  5. mysql忽略列,MySQL:使用DISTINCT时忽略选定的列
  6. fastjson反序列化漏洞研究(下)
  7. 五分钟教你在Go-Bigger中设计自己的游戏AI智能体
  8. 文本分类和提取关键词算法_文本内容之间的关键词提取和相似度计算
  9. React开发(235):react可以这样返回dom
  10. java mysql修改表结构字段_【开发技术】java+mysql 更改表字段的步骤
  11. Linux学习——Makefile
  12. Mathtype公式编辑器常用快捷键
  13. 在 Windows XP Embedded 中使用 Enhanced Write Filter (EWF)[微软影子系统]
  14. 数据字典在mysql中怎么做_如何编写数据字典
  15. WNMP环境源码安装
  16. 2019年清华计算机系本校保研推免机考题目
  17. tlc5615 c语言程序,第10章 TLC5615数模转换器DAC
  18. 【计算机系统结构】~ ROM/PROM/EPROM/E2PROM/FLASH、SOC 片上系统、总线、CPU 处理器、Cache、DDR、ARM 体系结构、虚拟内存、内核 kernel
  19. cdsn博客书写小技巧(不定时更新)
  20. IP地址分类详解:A、B、C、D类地址如何划分的以及保留地址(特殊地址)介绍

热门文章

  1. Debian Cacti(仙人掌)
  2. 简历求职01:STAR法则
  3. Unreal Engine4人物模板编辑与解析(1)
  4. C语言基础知识点复习
  5. 公路路基路面回弹弯沉检测技术(转载)
  6. oracle统计个数函数,oracle中字符串统计的函数
  7. STM32MP157 Linux系统移植开发篇19:Linux内核Wi-Fi驱动移植
  8. CentOS6.x安装方法超详细教程
  9. 【odoo15】添加Chatter到表单视图,创建[发送消息][记录备注][安排活动]
  10. CAD批量打图精灵入门教程--CAD批量打印、CAD批量转PDF