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#string类;math类;datetime类
String类: .Length字符的长度 .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格 .ToUpper()全 ...
- string、math、random、datetime类
1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确 ...
- 当碰到unix纪元问题时strtotime怎么转时间戳(DateTime类的使用方法)
UNIX纪元时间又称POSIX时间/新纪元时间(Epoch Time):从协调世界时1970年1月1日0时0分0秒起到现在的总秒数,不包括闰秒.正值表示1970以後,负值则表示1970年以前. Uni ...
- PHP使用DateTime类做时间日期到字符串转换
PHP关于时间日期的处理不是很规范,简单就简单了,就是不知道输入的字符串是否能够正确转化为需要的DateTime类型. 面向对象的PHP应该使用DateTime类来做string和dateTime的转 ...
- C# - 系统类 - DateTime类
DateTime类 ns:System 此类是一个结构 提供了访问和修改它所代表的时间 创建DateTime实例的几种方式 DateTime time = , , , , , ); Console.W ...
- C# DateTime类,TimeSpan类
DateTime类是.Net中用于处理时间类型数据的. 一.字段 MaxValue 表示 DateTime 的最大可能值.此字段为只读. MinValue 表示 DateTime 的最小可能值 ...
- 类之string类、Math类、DateTime类
String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...
- Python datetime模块的datetime类
datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...
- 【2017-02-26】String类、Math类、DateTime类
一.String类 黑色小扳手 - 属性 后面不带括号紫色立方体 - 方法 后面带括号 字符串.Length - 字符串长度,返回int类型 字符串.TrimStart() - 去 ...
- 【2-26】string/math/datetime类的定义及其应用
一string类 (1)字符串.Length Length作用于求字符串的长度,返回一个int值 (2)字符串.TrimStart(); TrimStart():可删除前空格,返回一个stri ...
随机推荐
- zabbix监控php-fpm性能状态
1. 启用php-fpm状态功能 # cat /usr/local/php/etc/php-fpm.conf | grep status_path pm.status_path = /status 2 ...
- Java 内存分配策略
内存有分配,就有回收,Java 的 GC 算法在前一篇文章中已经介绍过了,这篇文章着重介绍 Java 的内存分配策略. 从大方向讲,除去 JIT ,对象的内存分配就是在堆上分配,对象主要分配在新生代的 ...
- 【技术干货】听阿里云CDN安防技术专家金九讲tengine+lua开发
一.介绍 二.安装 三.运行 四.开发 1.介绍 Tengine:轻量级.高性能.高并发.配置化.模块化.可扩展.可移植的Web和反向代理 服务器,Tengine是nginx超集,但做了很多优化,包含 ...
- Linux配置全局jdk
Linux配置全局jdk 1.确保相应文件夹下有apache-tomcat和jdk的压缩文件 注意:jdk文件必须为适应Linux版本的文件 (如果已经有了相应文件,可以跳过以下第2-3个步骤) 2. ...
- 70. Climbing Stairs【leetcode】递归,动态规划,java,算法
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【.net 深呼吸】获取日期的干支纪年
其实,中国的农历是用干支纪年法的,“干支”即,十天干与十二地支. 十天干为:甲.乙.丙.丁.戊.己.庚.辛.壬.癸. 十二地支为:子.丑.寅.卯.辰.巳.午.未.申.酉.戌.亥. 十天干的含义如下: ...
- MySQL Flush导致的等待问题
--MySQL Flush导致的等待问题 -------------------------------2014/07/13 前言 在实际生产环境中有时会发现大量的sql语句处于waiting for ...
- UNIX 高手的 10 个习惯
引言 当您经常使用某个系统时,往往会陷入某种固定的使用模式.有时,您没有养成以尽可能最好的方式做事的习惯.有时,您的不良习惯甚至会导致出现混乱.纠正此类缺点的最佳方法之一,就是有意识地采用抵制这些坏习 ...
- 寻人启事(O(∩_∩)O哈哈~)
注:本文纯属扯淡,如有雷同,纯属巧合! 寻人启事格式 1:标题 标题通常在第一行中央写上<寻人启事>.<寻找XXX>或<寻XXX))等字样. 2:正文 正文一般须包括 ...
- this指针的值
this指针指向的是对象的地址,与对象第一个成员变量的关系是怎样的呢?看下面这个例子: struct CPlex{ CPlex* pNext; void* data() { return this+1 ...