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

工作的时候 用到了 获取时间 DateTime 整理了一下的更多相关文章

  1. C#深入浅出获取时间DateTime

    首先,先了解微软.net里面的DateTime的DateTime.Now.DateTime.Now.Date.DateTime.Now.Day.DateTime.Now.DayOfWeek.DateT ...

  2. C#:获取时间年月日时分秒格式

    //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToStri ...

  3. C#获取时间的函数

    //获取日期+时间DateTime.Now.ToString();            // 2012-9-4 20:02:10DateTime.Now.ToLocalTime().ToString ...

  4. c#.net 获取时间日期年月日时分秒格式

    今天写代码发现两个比较不错的分享下:1.DateTime.ParseExact很多时候我们获取的时间是数字形式表示的,好比20140127134015.927856,通过这个方法DateTime.Pa ...

  5. c#.net 获取时间日期年月日时分秒生成自动文件名格式

    下面是日期和时间的各种方法,转换为字符串. 如果把输出的格式改下就可以做类似的文件名了,例如:2016010110101224356.doc  c#用DateTime.Now.ToString(&qu ...

  6. asp.net获取时间日期插入数据库

    //获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...

  7. POI教程之第二讲:创建一个时间格式的单元格,处理不同内容格式的单元格,遍历工作簿的行和列并获取单元格内容,文本提取

    第二讲 1.创建一个时间格式的单元格 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet=wb.createSheet("第一个 ...

  8. linux在shell中获取时间

    linux在shell中获取时间 获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 将当前日期赋值给DATE变量DATE=$(date +%Y%m%d) 有时候我们需要使用今 ...

  9. shell获取时间的相关命令

    Linux shell获取时间和时间间隔(ms级别) 说明:在进行一些性能测试的时候,有时候我们希望能计算一个程序运行的时间,有时候可能会自己写一个shell脚本方便进行一些性能测试的控制(比如希望能 ...

随机推荐

  1. 人生苦短之Python枚举类型enum

    枚举类型enum是比较重要的一个数据类型,它是一种数据类型而不是数据结构,我们通常将一组常用的常数声明成枚举类型方便后续的使用.当一个变量有几种可能的取值的时候,我们将它定义为枚举类型.在Python ...

  2. Eclipse配置python环境

    主要分为四步 1.安装java环境 2.在Eclipse下安装Pydev 1)启动 Eclipse,利用 Eclipse Update Manager 安装 PyDev.在 Eclipse 菜单栏中找 ...

  3. codeforces B. Calendar 解题报告

    题目链接:http://codeforces.com/problemset/problem/304/B 题目意思:给出两个日期,需要算出这两个日期之间有多少日. 细心模拟就可以了.特别要注意的是,两个 ...

  4. linux系统配置之bash shell的配置(centos)

    linux系统开机启动过程的最后阶段会由init进程根据启动方案(运行级:0-6)启动许多基本的服务程序,为用户提供各种各样的服务.在启动这些服务的最后会启动一个为用户提供操作环境的服务,用户就是通过 ...

  5. [Tyvj Aug11] 黄金矿工

    传送门 Description 黄金矿工是一个经典的小游戏,它可以锻炼人的反应能力.该游戏中,可以通过“挖矿”获得积分并不断升级.玩家可以在线玩flash版黄金矿工,也可以下载后玩单机版黄金矿工.目前 ...

  6. js实现打字机效果

    var s = 'Hello World! Hello World! Hello World!'; var con = $('.container'); var index = 0; var leng ...

  7. Spark Streaming之四:Spark Streaming 与 Kafka 集成分析

    前言 Spark Streaming 诞生于2013年,成为Spark平台上流式处理的解决方案,同时也给大家提供除Storm 以外的另一个选择.这篇内容主要介绍Spark Streaming 数据接收 ...

  8. 杂项:SVN 常用方法

    ylbtech-杂项:SVN 1.返回顶部 1. bin obj *.suo*.user *.csproj.user *.o *.lo *.la *.al .libs *.so *.so.[0-9]* ...

  9. 微信公众平台:微信JS-SDK Demo

    ylbtech-微信公众平台:微信JS-SDK Demo 1. HTML返回顶部 1.demo.html <!DOCTYPE html> <html> <head> ...

  10. hdu5530

    分治ntt 考虑从添加i,放在j位置,那么1->j是一个连通块,j+1->i和1->j不连通,那么我们可以列出式子dp[i]=∑j=1->i dp[i-j]*A(i-1,j-1 ...