https://www.cnblogs.com/tmdsleep/p/5700058.html
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()); // 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()); // DateTimelong 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本身並不改變
參數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時區偏移量(“+”或“-”后面僅跟小時)。一位數的小時數沒有前導零。例如,太平洋標准時間是“-8”。
zz時區偏移量(“+”或“-”后面僅跟小時)。一位數的小時數有前導零。例如,太平洋標准時間是“-08”。
zzz完整時區偏移量(“+”或“-”后面跟有小時和分鍾)。一位數的小時數和分鍾數有前導零。例如,太平洋標准時間是“-08:00”。
:在TimeSeparator中定義的默認時間分隔符。
/在DateSeparator中定義的默認日期分隔符。
%c其中c是格式模式(如果單獨使用)。如果格式模式與原義字符或其他格式模式合並,則可以省略“%”字符。
\c其中c是任意字符。照原義顯示字符。若要顯示反斜杠字符,請使用“\”。