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()); //
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 使用详解的更多相关文章
- C# 内置 DateTime类详解
C# 内置 DateTime类详解 摘抄自微软官方文档,用来方便自己查阅:网址:https://msdn.microsoft.com/zh-cn/library/system.datetime(v=v ...
- (转)python time模块和datetime模块详解
python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...
- datetime 模块详解 -- 基本的日期和时间类型
转自:https://www.cnblogs.com/fclbky/articles/4098204.html datetime 模块提供了各种类用于操作日期和时间,该模块侧重于高效率的格式化输出 在 ...
- C#时间格式化(Datetime)用法详解
Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...
- python time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- Java 8 Date-Time API 详解
从Java版本1.0开始就支持日期和时间,主要通过java.util.Date类. 但是,Date类设计不佳. 例如,Date中的月份从1开始,但从日期却从0开始.在JDK 1.1中使用它的许多方法已 ...
- python datetime模块详解
datetime是python当中比较常用的时间模块,用于获取时间,时间类型之间转化等,下文介绍两个实用类. 一.datetime.datetime类: datetime.datetime.now() ...
- time&datetime模块详解
一.time模块 1.时间格式转换图: 2.time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.for ...
- Python全栈之路----常用模块----datetime模块详解
相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...
- day21 Pythonpython time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
随机推荐
- everyday two problems / 3.11 - 3.17
7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...
- C/C++中的64位整数
C/C++中的64位整数(__int64 and long long) 在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31, ...
- http-runtime属性
配置httpRuntime也可以让FileUpload上传更大的文件,不过设置太大了会因用户将大量文件传递到该服务器而导致的拒绝服务攻击(属性有说明) 属性 属性 选项 说明 appRequestQu ...
- PHP array_intersect_uassoc()
定义和用法 array_intersect_uassoc() 函数使用用户自定义的回调函数计算数组的交集,用回调函数比较索引. array_intersect_uassoc() 返回一个数组,该数组包 ...
- Graphics简单汇总
1.主页面布局文件 activity_main.xml(仅仅有2个button按钮) <?xml version="1.0" encoding="utf-8&quo ...
- 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方式
本文提供了对c3p0与DBCP连接池连接MySql数据库时. 8小时内无请求自己主动断开连接的解决方式.首先介绍一下我在项目(c3p0连接池)中遇到的问题,后面还提供了使用DBCP连接池的解决方式. ...
- 开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据
项目地址:http://www.freeteam.cn/ 生成栏目列表数据 提取当前管理网站下同意移动APP訪问的栏目列表,生成json数据到/site/网站文件夹/mobile/channels.h ...
- iOS 代码方式设置按钮标题、图片的偏移
button.imageEdgeInsets = UIEdgeInsetsMake(0,1 , 2, 3); button.titleEdgeInsets = UIEdgeInsetsMake(0,1 ...
- Android studio 报错 gradel project sync failed Error:Cause: peer not authenticated
在网上找了半天,应该是找不到gradel的路径,试了网上非常多方法.本人解决例如以下: 在android studio中设置gradle.打开File-> settings->Gradle ...
- 【ODPS】阿里云ODPS中带分区的表操作
1.创建分区表: 分区表有自己的分区列,而分区表则没有. public static void createTableWithPartition(Odps odps, String createTab ...