获得当前系统时间: 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本身并不改变

C# 中DateTime的各种使用的更多相关文章

  1. .NET中DateTime.Now.ToString的格式化字符串

    .NET中DateTime.Now.ToString显示毫秒:DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") DateTime.N ...

  2. easyui datagrid中datetime字段的显示和增删改查问题

    datagrid中datetime字段的异常显示: 使用过easyui datagrid的应该都知道,如果数据库中的字段是datetime类型,绑定在datagrid显式的时候会不正常显示,一般需要借 ...

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

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

  4. C#中 DateTime , DateTime2 ,DateTimeOffset 之间的小区别 (转载)

    闲来无事列了个表比对一下这3兄弟之间还是有一点差距的╮(╯_╰)╭   DateTime DateTime2 DateTimeOffset 日期范围 1753-01-01到 9999-12-31 00 ...

  5. SQL中DateTime转换成Varchar样式

    SQL中DateTime转换成Varchar样式语句及查询结果:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect ...

  6. python中datetime模块中datetime对象的使用方法

    本文只讲述datetime模块中datetime对象的一些常用的方法,如果读者需要更多datetime模块的信息,请查阅此文档. datetime模块的对象有如下: timedelta date da ...

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

    原文:C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法 本人新手,在.Net中写WebAPI的时候,当接口返回的json数据含有日期时间类型的字段时, ...

  8. 解决python写入mysql中datetime类型遇到的问题

    解决python写入mysql中datetime类型遇到的问题 刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题: 在mysql数据库中,有一个datetime类型的字段用于 ...

  9. sql查询中datetime显示的格式为yyyy-DD-mm

    datetime数据库中保存的形式为2008/9/29 星期一 上午 12:00:00,希望界面显示2008-09-29,则可以用到以下sql语句. ),kgrq, ),),jhjgrq, ),'/' ...

  10. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

随机推荐

  1. Redis Scan迭代器遍历操作原理(一)

    Redis在2.8.0版本新增了众望所归的scan操作,从此再也不用担心敲入了keys*, 然后举起双手看着键盘等待漫长的系统卡死了··· 命令的官方介绍在这里, 中文版由huangz同学细心翻译了, ...

  2. 【loj2033】生成魔咒

    Portal --> loj2033 Solution 这题..虽然说好像也是sam的裸题不过既然在智力康复那就强制后缀数组吧qwq ​ (晚点再用sam写一次qwq) 首先如果是要求本质不同的 ...

  3. python 常用 time, datetime处理

    python 中 time 有三种格式: float, struct tuple(time.struct_time 或 datetime.datetime), str 常用的: float --> ...

  4. Educational Codeforces Round 20 C 数学/贪心/构造

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. 把一个文件中所有文件名或者文件路径读取到一个txt文件,然后在matlab中读取

    链接: http://blog.csdn.net/dreamgchuan/article/details/51113295 dir /on/b/s  这个读取的是这样的格式:

  6. jinja2 中的 Template 批量替换json字符串中的内容

    项目中用到elasticsearch,使用Json格式查询方式,一个查询语句中有好几个地方需要替换,且替换的值都相同.最开始把json转为字符串发方式,利用format函数处理,发现再转回json时无 ...

  7. [JSOI2010]Group

    1821: [JSOI2010]Group 部落划分 Group Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] De ...

  8. 实用的 Node.js 教程,工具和资源

    这里分享一批实用的实用的 Node.js 教程,工具和资源. Node.js是一个建立在Chrome之上的JavaScript运行时平台,可方便地构建快速,可扩展的网络应用程序.Node.js使用事件 ...

  9. 使用TortoiseGit时如何实现SSH免密码登录

    1.      Git配置 连接GIT服务器使用的是SSH连接,因此无密码登录,需要使用公钥和私钥. 1)     生成公钥/私钥 在Git Shell中输入ssh-keygen命令,直接回车使用默认 ...

  10. js中不能做变量名的字符

    JavaScript中不能作为变量名的关键字和保留字总结: 1.js中的关键字: break case catch continue default delete do else finally fo ...