获得当前系统时间: 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 使用详解的更多相关文章

  1. C# 内置 DateTime类详解

    C# 内置 DateTime类详解 摘抄自微软官方文档,用来方便自己查阅:网址:https://msdn.microsoft.com/zh-cn/library/system.datetime(v=v ...

  2. (转)python time模块和datetime模块详解

    python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...

  3. datetime 模块详解 -- 基本的日期和时间类型

    转自:https://www.cnblogs.com/fclbky/articles/4098204.html datetime 模块提供了各种类用于操作日期和时间,该模块侧重于高效率的格式化输出 在 ...

  4. C#时间格式化(Datetime)用法详解

    Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...

  5. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  6. Java 8 Date-Time API 详解

    从Java版本1.0开始就支持日期和时间,主要通过java.util.Date类. 但是,Date类设计不佳. 例如,Date中的月份从1开始,但从日期却从0开始.在JDK 1.1中使用它的许多方法已 ...

  7. python datetime模块详解

    datetime是python当中比较常用的时间模块,用于获取时间,时间类型之间转化等,下文介绍两个实用类. 一.datetime.datetime类: datetime.datetime.now() ...

  8. time&datetime模块详解

     一.time模块 1.时间格式转换图: 2.time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.for ...

  9. Python全栈之路----常用模块----datetime模块详解

    相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...

  10. day21 Pythonpython time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

随机推荐

  1. from __future__ import absolute_import的作用

    关于这句from future import absolute_import的作用: 直观地看就是说"加入绝对引入这个新特性".说到绝对引入,当然就会想到相对引入.那么什么是相对引 ...

  2. C#关键字详解第六节

    3.28 日志记录:前段时间参加技能大赛,所以未更新博客,特此补上,第一次写博客,希望自己认真下去,努力,天道酬勤! 比赛给我的感悟很深!古语云:山外有山,强中自有强中手! do:执行语句 说do之前 ...

  3. Excel 2010/2013/2016在鼠标右键新建xls或xlsx文件后,打开报错“无法打开文件”“文件格式或文件扩展名无效”

    近段时间,陆续有两个同事先后出现同样的问题(在Excel多个版本都可能出现),问题描述: 当用鼠标右键在任意文件夹或电脑桌面“新建”→“ Microsoft Excel 工作表”,再用鼠标双击打开这个 ...

  4. linux的ulimit各种限制之深入分析

    一般可以通过ulimit命令或编辑/etc/security/limits.conf重新加载的方式使之生效 通过ulimit比较直接,但只在当前的session有效,limits.conf中可以根据用 ...

  5. [BZOJ 4563]放棋子

    [BZOJ 4563]放棋子 题目 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放N枚棋子(障碍的位置不能放棋子),要求你放N个棋子 ...

  6. 使用git bash向github远程仓库提交代码

    1.登录github,创建仓库. 2.切换到要提交的文件目录下. 3.打开git bash 3.1.初始化仓库 git init 3.2.将本地仓库与远程仓库关联 git remote add ori ...

  7. 洛谷 P1481 魔族密码

    P1481 魔族密码 题目描述 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之子:我呕……(杀死人的眼神)快说题目!否则……-_-### 花 ...

  8. 最简单的基于FFmpeg的移动端样例:Windows Phone HelloWorld

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  9. 很强大的shell写的俄罗斯方块

    网上看到的一个用linux的shell脚本写的俄罗斯方块. 是我至今见过写的最牛逼的shell了.共享一下. 原作者信息在脚本的凝视中有. 下载地址:点击下载 #!/bin/bash # Tetris ...

  10. Manacher求最长回文

    #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助 ...