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的使用的更多相关文章
- C# DateTime与时间戳转换
C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳: ...
- C# DateTime日期格式化
在C#中DateTime是一个包含日期.时间的类型,此类型通过ToString()转换为字符串时,可根据传入给Tostring()的参数转换为多种字符串格式. 目录 1. 分类 2. 制式类型 3. ...
- 在面试中忽然发现DateTime的一些...
今天说说我面试中碰到的一个小问题,在我问起DateTime为什么无法赋值NULL值,一般第一反应都认为它是值类型,不是引用类型,但随后我查阅了度娘自我学习到它是结构类型,那么随之而然就无法赋值NULL ...
- LINQ to SQL语句(14)之Null语义和DateTime
Null语义 说明:下面第一个例子说明查询ReportsToEmployee为null的雇员.第二个例子使用Nullable<T>.HasValue查询雇员,其结果与第一个例子相同.在第三 ...
- .NET DateTime类型变量作为参数时设置默认值
一个小的 Tips. .NET 中函数参数的默认值需要是编译时常量.如果参数是引用类型,可以设置Null,如果是值类型,可以设置相应的编译时常量,如整型可以用整数,但对于DateTime(结构体,值类 ...
- BCS datetime 时间区间问题
BCS 整合sql表时发现以下问题: datetime字段在列表中带了时区,比如插入12-6号的数据,在sql中显示的是12-5 date类型字段无法正确识别,插入成功但报错 LobSystem (外 ...
- C#中DateTime.Ticks属性及Unix时间戳转换
1.相关概念 DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). ...
- WPF 自定义DateControl DateTime控件
自定义日期控件,月份选择.如下是日期的一些效果图. 具体的样式.颜色可以根据下面的代码,自己调节即可 1.日期控件的界面 <UserControl x:Class="WpfApp ...
- JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决
JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...
- python标准模块(time、datetime及hashlib模块)
一.time,datetime模块 时间相关的操作 import time time.sleep(5) # ==> 停顿多少秒 print(time.time()) # ==> 返回时间戳 ...
随机推荐
- php类内方法使用类外变量和类外函数
如果要调用另一个类的属性和方法,直接实例化后调用就可以 但是要使用的变量和函数是独立的,不在本类或其他类里面,这时调用就要用下面的方法(本例是在同一个php文件里,如果不再同一个类文件可以requir ...
- 二次开发php
AB模板 http://www.adminbuy.cn/ 码源 (站长之家)http://down.chinaz.com/ 易无袖资源 http://www.ewuxiu.com/ A5码源 htt ...
- SpringMVC(二)高级应用
一.参数绑定-----集合类型 二.数据回显(例如提交表单失败了,数据没有丢失) 三.上传图片 四.json数据的交互 五.restful 支持 六.拦截器
- LeetCode 069 Sqrt(x) 求平方根
Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negati ...
- 028 Implement strStr() 实现 strStr()
实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...
- ForkJoin有参无返回值、有参有返回值实例
介绍: a . Fork/Join为JKD1.7引入,适用于对大量数据进行拆分成多个小任务进行计算的框架,最后把所有小任务的结果汇总合并得到最终的结果 b . 相关类 public abstract ...
- 引用 Session详解 作者:郎云鹏
本文转载自leeldy<Session详解 作者:郎云鹏> 引用 leeldy 的 Session详解 作者:郎云鹏 目录: 一.术语session 二.HTTP协议与状态保持 三.理 ...
- MVC中 Remote的用法
一.web.config加入 <appSettings> <add key="ClientValidationEnabled" value="t ...
- 虚拟机安装CentOS7 Minimal、jdk和hadoop
虚拟机安装CentOS7 Minimal.jdk和hadoop Table of Contents 1. 安装版本 2. PD安装 3. vim安装和配置 4. 主机名变为bogon的解决办法 5. ...
- lambda匿名函数,sorted排序,filter()筛选,map()映射
一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...