获得当前系统时间: 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. PAT 1113 Integer Set Partition

    Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...

  2. PAT 1057. Stack

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  3. Java基础之构造方法及其应用

    构造方法是一种特殊的方法,它是一个与类同名且无返回值类型(连void也不能有)的方法. 对象的创建就是通过构造方法来完成,其功能主要是完成对象的初始化. 当类实例化一个对象时会自动调用构造方法.构造方 ...

  4. Python 7 列表 for 字典,嵌套

    列表: 基本格式:变量名 = [元素1,元素2,元素3] 创建:A = ['访客','admin',19]  或  A = list(['armin','admin',19]),  后者更倾向于转换为 ...

  5. [Usaco2010 Dec]Exercise 奶牛健美操

    [Usaco2010 Dec]Exercise 奶牛健美操 题目 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连 ...

  6. 08springMVC拦截器

    u  概述 u  拦截器接口 u  拦截器适配器 u  运行流程图 u  拦截器HelloWorld u  常见应用之性能监控 1      概述 1.1    简介     Spring Web M ...

  7. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

  8. MVC.Net:将Reponse Redirect从Get变为Post

    在我们使用Response.Redirect命令的时候,有时候希望以POST的方式将参数传递过去.那么Redirect支持这种做法吗?答案是不支持... ======= 怎么办呢? ======= 我 ...

  9. Eclipse搭建maven开发环境

    上一篇学习了maven开发环境的搭建,而且手动编写了一个mavenproject,可是这样子效率非常低下.今天带大家学习在eclipse下搭建maven开发环境. 经常使用的maven命令 mvn c ...

  10. 改动mysqlpassword

    1.假设没有password,则 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); ...