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()); //
- Console.WriteLine(dt.Date.ToString()); // 26/11/2009 AM 12:00:00
- Console.WriteLine(dt.DayOfWeek.ToString()); // Thursday
- Console.WriteLine(dt.DayOfYear.ToString()); //
- Console.WriteLine(dt.Hour.ToString()); //
- Console.WriteLine(dt.Millisecond.ToString()); // 801 (毫秒)
- Console.WriteLine(dt.Minute.ToString()); //
- Console.WriteLine(dt.Month.ToString()); //
- Console.WriteLine(dt.Second.ToString()); //
- Console.WriteLine(dt.Ticks.ToString()); //
- 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().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().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().ToString()); // 26/11/2009 PM 12:29:51
- Console.WriteLine(dt.CompareTo(dt).ToString()); //
- Console.WriteLine(dt.Add(new TimeSpan(,,,)).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()); //
- 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')[].ToString()); //2009-11-26T13:29:06
- Console.WriteLine(dt.GetDateTimeFormats('t')[].ToString()); //PM 1:29
- Console.WriteLine(dt.GetDateTimeFormats('y')[].ToString()); //2009年11月
- Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //2009年11月26日
- Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //星期四, 26 十一月, 2009
- Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //26 十一月, 2009
- Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //星期四 2009 11 26
- Console.WriteLine(dt.GetDateTimeFormats('M')[].ToString()); //26 十一月
- Console.WriteLine(dt.GetDateTimeFormats('f')[].ToString()); //2009年11月26日 PM 1:29
- Console.WriteLine(dt.GetDateTimeFormats('g')[].ToString()); //26/11/2009 PM 1:29
- Console.WriteLine(dt.GetDateTimeFormats('r')[].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)); //
- 计算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(, );
- days = ;
- 给日期增加一天、减少一天
- DateTime dt =DateTime.Now;
- dt.AddDays(); //增加一天 dt本身并不改变
- dt.AddDays(-);//减少一天 dt本身并不改变
- 参数format格式详细用法:
- 格式字符关联属性/说明
- dShortDatePattern
- DLongDatePattern
- f完整日期和时间(长日期和短时间)
- FFullDateTimePattern(长日期和长时间)
- g常规(短日期和短时间)
- G常规(短日期和长时间)
- m、MMonthDayPattern
- r、RRFC1123Pattern
- s使用当地时间的SortableDateTimePattern(基于ISO8601)
- tShortTimePattern
- TLongTimePattern
- uUniversalSortableDateTimePattern用于显示通用时间的格式
- 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时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-”。
- zz时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-”。
- zzz完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-:”。
- :在TimeSeparator中定义的默认时间分隔符。
- /在DateSeparator中定义的默认日期分隔符。
- %c其中c是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
- \c其中c是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。
C# Datetime 使用详解的更多相关文章
- C# 内置 DateTime类详解
C# 内置 DateTime类详解 摘抄自微软官方文档,用来方便自己查阅:网址:https://msdn.microsoft.com/zh-cn/library/system.datetime(v=v ...
- (转)python time模块和datetime模块详解
python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...
- datetime 模块详解 -- 基本的日期和时间类型
转自:https://www.cnblogs.com/fclbky/articles/4098204.html datetime 模块提供了各种类用于操作日期和时间,该模块侧重于高效率的格式化输出 在 ...
- C#时间格式化(Datetime)用法详解
Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...
- python time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- Java 8 Date-Time API 详解
从Java版本1.0开始就支持日期和时间,主要通过java.util.Date类. 但是,Date类设计不佳. 例如,Date中的月份从1开始,但从日期却从0开始.在JDK 1.1中使用它的许多方法已 ...
- python datetime模块详解
datetime是python当中比较常用的时间模块,用于获取时间,时间类型之间转化等,下文介绍两个实用类. 一.datetime.datetime类: datetime.datetime.now() ...
- time&datetime模块详解
一.time模块 1.时间格式转换图: 2.time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.for ...
- Python全栈之路----常用模块----datetime模块详解
相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...
- day21 Pythonpython time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
随机推荐
- 小白两篇博客熟练操作MySQL 之 第一篇
小白两篇博客熟悉操作MySQL 之 第一篇 一.概述 1. 什么是数据库? 答: 储存数据的仓库, 如: 在ATM的事例中创建的一个db 目录, 称为数据库 2. 什么是Mysql, Oracl ...
- 20170704-WNDR4300uboot help info
Unknown command 'env' - try 'help'ar7240> help? - alias for 'help'base - print or set address off ...
- vue组件 $children,$refs,$parent的使用详解
1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"></firstchild> 然后在父组件中,通过this.$ ...
- JavaScript中的call()和apply()方法,借此实现继承
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- N天学习一个linux命令之ssh-keygen
用途 生成ssh加密算法需要使用到的秘钥以及管理和转换 用法 ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment] [- ...
- MySQL的各种网络IO超时的用法和实现
2016-04-06 赵伟 数据库开发者 客户端C API 在C API中调用mysql_options()来设置mysql_init() 所创建的连接对象的属性,使用这三个选项可以设置连接超时和读写 ...
- HDU 5016 Mart Master II (树上点分治)
题目地址:pid=5016">HDU 5016 先两遍DFS预处理出每一个点距近期的基站的距离与基站的编号. 然后找重心.求出每一个点距重心的距离.然后依据dis[x]+dis[y] ...
- Android平台Airplay的实现方法
Airplay属于局域网内异构设备之间分享多媒体数据的一种通信协议.Airplay设备有客户端和服务器之分,一般将小屏IOS设备实现为Airplay客户端,大屏幕设备实现为Airplay服务器.即iP ...
- hdu5119 Happy Matt Friends(dp)
题目链接:点击打开链接 题目描写叙述:给出n个数.求从这n个数中随意取出一些数(至少取一个)相互异或之后大于m的方案数? 解题思路:分析因为n<=40&&m<=10^6,因 ...
- 数据结构(C实现)------- 顺序栈
栈是限定仅在表的一端进行插入或删除的纯属表,通常称同意插入.删除的一端为栈顶(Top),对应在的.则称还有一端为栈底(Bottom). 不含元素的栈则称为空栈. 所设栈S={a1,a2,a3,..., ...