C#中時(shí)間類的使用方法詳解
DateTime類
DateTime類是C#中最常用的時(shí)間類之一,它表示一個(gè)日期和時(shí)間。可以使用DateTime.Now屬性獲取當(dāng)前時(shí)間,也可以使用DateTime.Parse方法將字符串轉(zhuǎn)換為DateTime對象。
// 獲取當(dāng)前時(shí)間 DateTime now = DateTime.Now; // 將字符串轉(zhuǎn)換為DateTime對象 DateTime dateTime = DateTime.Parse("2022-01-01 12:00:00"); // 獲取當(dāng)前時(shí)間的年份 int year = now.Year; // 獲取當(dāng)前時(shí)間的月份 int month = now.Month; // 獲取當(dāng)前時(shí)間的日期 int day = now.Day; // 獲取當(dāng)前時(shí)間的小時(shí)數(shù) int hour = now.Hour; // 獲取當(dāng)前時(shí)間的分鐘數(shù) int minute = now.Minute; // 獲取當(dāng)前時(shí)間的秒數(shù) int second = now.Second; // 獲取當(dāng)前時(shí)間的毫秒數(shù) int millisecond = now.Millisecond;
DateTime類還提供了一些常用的方法和屬性,例如:
- DateTime.AddDays(double value):將當(dāng)前DateTime對象的日期加上指定的天數(shù)。
- DateTime.AddHours(double value):將當(dāng)前DateTime對象的時(shí)間加上指定的小時(shí)數(shù)。
- DateTime.AddMinutes(double value):將當(dāng)前DateTime對象的時(shí)間加上指定的分鐘數(shù)。
- DateTime.AddSeconds(double value):將當(dāng)前DateTime對象的時(shí)間加上指定的秒數(shù)。
- DateTime.Year:獲取當(dāng)前DateTime對象的年份。
- DateTime.Month:獲取當(dāng)前DateTime對象的月份。
- DateTime.Day:獲取當(dāng)前DateTime對象的日期。
- DateTime.Hour:獲取當(dāng)前DateTime對象的小時(shí)數(shù)。
- DateTime.Minute:獲取當(dāng)前DateTime對象的分鐘數(shù)。
- DateTime.Second:獲取當(dāng)前DateTime對象的秒數(shù)。
TimeSpan類
TimeSpan類表示時(shí)間間隔,可以用來計(jì)算兩個(gè)日期之間的時(shí)間差。可以使用TimeSpan.FromDays、TimeSpan.FromHours、TimeSpan.FromMinutes、TimeSpan.FromSeconds等方法創(chuàng)建TimeSpan對象。
// 創(chuàng)建一個(gè)表示1天的TimeSpan對象 TimeSpan oneDay = TimeSpan.FromDays(1); // 創(chuàng)建一個(gè)表示2小時(shí)的TimeSpan對象 TimeSpan twoHours = TimeSpan.FromHours(2); // 創(chuàng)建一個(gè)表示30分鐘的TimeSpan對象 TimeSpan thirtyMinutes = TimeSpan.FromMinutes(30); // 創(chuàng)建一個(gè)表示10秒的TimeSpan對象 TimeSpan tenSeconds = TimeSpan.FromSeconds(10);
TimeSpan類還提供了一些常用的方法和屬性,例如:
- TimeSpan.TotalDays:獲取TimeSpan對象表示的總天數(shù)。
- TimeSpan.TotalHours:獲取TimeSpan對象表示的總小時(shí)數(shù)。
- TimeSpan.TotalMinutes:獲取TimeSpan對象表示的總分鐘數(shù)。
- TimeSpan.TotalSeconds:獲取TimeSpan對象表示的總秒數(shù)。
DateTimeOffset類
DateTimeOffset類表示一個(gè)日期和時(shí)間,同時(shí)包含時(shí)區(qū)信息。可以使用DateTimeOffset.Now屬性獲取當(dāng)前時(shí)間,也可以使用DateTimeOffset.Parse方法將字符串轉(zhuǎn)換為DateTimeOffset對象。
// 獲取當(dāng)前時(shí)間 DateTimeOffset now = DateTimeOffset.Now // 將字符串轉(zhuǎn)換為DateTimeOffset對象 DateTimeOffset dateTimeOffset = DateTimeOffset.Parse("2022-01-01 12:00:00 +08:00");
DateTimeOffset類還提供了一些常用的方法和屬性,例如:
- DateTimeOffset.ToLocalTime():將當(dāng)前DateTimeOffset對象轉(zhuǎn)換為本地時(shí)間。
- DateTimeOffset.ToUniversalTime():將當(dāng)前DateTimeOffset對象轉(zhuǎn)換為協(xié)調(diào)世界時(shí)(UTC)時(shí)間。
靜態(tài)類的封裝
using System; namespace ToolBox.DateTimeTool { public static class DateTimeExtend { /// <summary> /// 獲取本日開始時(shí)間(0點(diǎn)0分0秒) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetDayStart(this DateTime dateTime) { return dateTime.Date; } /// <summary> /// 獲取本日結(jié)束時(shí)間(23點(diǎn)59分59秒) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetDayEnd(this DateTime dateTime) { return dateTime.Date.AddDays(1).AddMilliseconds(-1); } /// <summary> /// 獲取本周開始時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetWeekStart(this DateTime dateTime) { return dateTime.AddDays(-(int)dateTime.DayOfWeek + 1).GetDayStart(); } /// <summary> /// 獲取本周結(jié)束時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetWeekEnd(this DateTime dateTime) { return dateTime.AddDays(7 - (int)dateTime.DayOfWeek).GetDayEnd(); } /// <summary> /// 獲取本月開始時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetMonthStart(this DateTime dateTime) { return new DateTime(dateTime.Year, dateTime.Month, 1, 0, 0, 0, 0); } /// <summary> /// 獲取本月結(jié)束時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetMonthEnd(this DateTime dateTime) { return GetMonthStart(dateTime).AddMonths(1).AddMilliseconds(-1); } /// <summary> /// 獲取本季度開始時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetSeasonStart(this DateTime dateTime) { var time = dateTime.AddMonths(0 - ((dateTime.Month - 1) % 3)); return DateTime.Parse(time.AddDays(-time.Day + 1).ToString("yyyy/MM/dd 00:00:00")); } /// <summary> /// 獲取本季度結(jié)束時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetSeasonEnd(this DateTime dateTime) { var time = dateTime.AddMonths((3 - ((dateTime.Month - 1) % 3) - 1)); return DateTime.Parse(time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1).ToString("yyyy/MM/dd 23:59:59")); } /// <summary> /// 獲取本年開始時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetYearStart(this DateTime dateTime) { return DateTime.Parse(dateTime.AddDays(-dateTime.DayOfYear + 1).ToString("yyyy/MM/dd 00:00:00")); } /// <summary> /// 獲取本年結(jié)束時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime GetYearEnd(this DateTime dateTime) { var time2 = dateTime.AddYears(1); return DateTime.Parse(time2.AddDays(-time2.DayOfYear).ToString("yyyy/MM/dd 23:59:59")); } /// <summary> /// 北京時(shí)間轉(zhuǎn)換成unix時(shí)間戳(10位/秒) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static long BeijingTimeToUnixTimeStamp10(this DateTime dateTime) { return (long)(dateTime - new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds; } /// <summary> /// 格林威治時(shí)間轉(zhuǎn)換成unix時(shí)間戳(10位/秒) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static long UtcTimeToUnixTimeStamp10(this DateTime dateTime) { return (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; } /// <summary> /// 北京時(shí)間轉(zhuǎn)換成unix時(shí)間戳(13位/毫秒) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static long BeijingTimeToUnixTimeStamp13(this DateTime dateTime) { return (long)(dateTime - new DateTime(1970, 1, 1, 8, 0, 0)).TotalMilliseconds; } /// <summary> /// 格林威治時(shí)間轉(zhuǎn)換成unix時(shí)間戳(13位/毫秒) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static long UtcTimeToUnixTimeStamp13(this DateTime dateTime) { return (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; } /// <summary> /// 10位unix時(shí)間戳轉(zhuǎn)換成北京時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime UnixTimeStamp10ToBeijingTime(this long unixTimeStamp) { return new DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(unixTimeStamp); } /// <summary> /// 10位unix時(shí)間戳轉(zhuǎn)換成格林威治 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime UnixTimeStamp10ToUtcTime(this long unixTimeStamp) { return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(unixTimeStamp); } /// <summary> /// 13位unix時(shí)間戳轉(zhuǎn)換成北京時(shí)間 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime UnixTimeStamp13ToBeijingTime(this long unixTimeStamp) { return new DateTime(1970, 1, 1, 8, 0, 0).AddMilliseconds(unixTimeStamp); } /// <summary> /// 13位unix時(shí)間戳轉(zhuǎn)換成格林威治 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static DateTime UnixTimeStamp13ToUtcTime(this long unixTimeStamp) { return new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(unixTimeStamp); } /// <summary> /// 當(dāng)前日期所在月份第一個(gè)指定星期幾的日期 /// </summary> /// <param name="date">給定日期</param> /// <param name="dayOfWeek">星期幾</param> /// <returns>所對應(yīng)的日期</returns> public static DateTime GetFirstWeekDayOfMonth(this DateTime date, DayOfWeek dayOfWeek) { var dt = date.GetMonthStart(); while (dt.DayOfWeek != dayOfWeek) dt = dt.AddDays(1); return dt; } /// <summary> /// 當(dāng)前日期所在月份最后1個(gè)指定星期幾的日期 /// </summary> /// <param name="date">給定日期</param> /// <param name="dayOfWeek">星期幾</param> /// <returns>所對應(yīng)的日期</returns> public static DateTime GetLastWeekDayOfMonth(this DateTime date, DayOfWeek dayOfWeek) { var dt = date.GetMonthEnd(); while (dt.DayOfWeek != dayOfWeek) dt = dt.AddDays(-1); return dt; } /// <summary> /// 判斷是否比指定之間早 /// </summary> /// <param name="date"></param> /// <param name="other"></param> /// <returns></returns> public static bool IsBefore(this DateTime date, DateTime other) { return date < other; } /// <summary> /// 判斷是否比指定時(shí)間晚 /// </summary> /// <param name="date"></param> /// <param name="other"></param> /// <returns></returns> public static bool IsAfter(this DateTime date, DateTime other) { return date > other; } /// <summary> /// 給定日期所在月份共有多少天 /// </summary> /// <param name="date"></param> /// <returns></returns> public static int GetCountDaysOfMonth(this DateTime date) { return date.GetMonthEnd().Day; } /// <summary> /// 當(dāng)前日期與給定日期是否是同一天 /// </summary> /// <param name="date">當(dāng)前日期</param> /// <param name="dateToCompare">給定日期</param> /// <returns></returns> public static bool IsDateEqual(this DateTime date, DateTime dateToCompare) { return date.Date == dateToCompare.Date; } /// <summary> /// 是否是周未 /// </summary> /// <param name="date"></param> /// <returns></returns> public static bool IsWeekend(this DateTime date) { return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; } /// <summary> /// 是否是工作日 /// </summary> /// <param name="date"></param> /// <returns></returns> public static bool IsWeekDay(this DateTime date) { return !date.IsWeekend(); } /// <summary> /// 判斷是否為今天 /// </summary> /// <param name="date"></param> /// <returns></returns> public static bool IsToday(this DateTime date) { return date.Date == DateTime.Now.Date; } /// <summary> /// 判定公歷閏年遵循的一般規(guī)律為:四年一閏,百年不閏,四百年再閏。 /// 公歷閏年的精確計(jì)算方法:(按一回歸年365天5小時(shí)48分45.5秒) /// 普通年能被4整除而不能被100整除的為閏年。 (如2004年就是閏年,1900年不是閏年) /// 世紀(jì)年能被400整除而不能被3200整除的為閏年。 (如2000年是閏年,3200年不是閏年) /// 對于數(shù)值很大的年份能整除3200,但同時(shí)又能整除172800則又是閏年。(如172800年是閏年,86400年不是閏年) /// 公元前閏年規(guī)則如下: /// 非整百年:年數(shù)除4余數(shù)為1是閏年,即公元前1、5、9……年; /// 整百年:年數(shù)除400余數(shù)為1是閏年,年數(shù)除3200余數(shù)為1,不是閏年,年數(shù)除172800余1又為閏年,即公元前401、801……年。 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static bool IsLeap(this DateTime dateTime) { var year = dateTime.Year; if ((year % 400 == 0 && year % 3200 != 0) || (year % 4 == 0 && year % 100 != 0) || (year % 3200 == 0 && year % 172800 == 0)) return true; else return false; } /// <summary> /// 獲取當(dāng)前年天數(shù) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static int GetDaysByYear(this DateTime dateTime) { return (new DateTime(dateTime.Year + 1, 1, 1) - new DateTime(dateTime.Year, 1, 1)).Days; } /// <summary> /// 獲取當(dāng)前年天數(shù) /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static int GetWeekCountByYear(this DateTime dateTime) { //找到今年的第一天是周幾 int firstWeekend = Convert.ToInt32(DateTime.Parse(dateTime.Year + "-1-1").DayOfWeek); //獲取第一周的差額,如果是周日,則firstWeekend為0,第一周也就是從周天開始的。 int weekDay = firstWeekend == 0 ? 1 : (7 - firstWeekend + 1); //獲取今天是一年當(dāng)中的第幾天 int currentDay = dateTime.DayOfYear; //(今天 減去 第一周周末)/7 等于 距第一周有多少周 再加上第一周的1 就是今天是今年的第幾周了 // 剛好考慮了惟一的特殊情況就是,今天剛好在第一周內(nèi),那么距第一周就是0 再加上第一周的1 最后還是1 int current_week = Convert.ToInt32(Math.Ceiling((currentDay - weekDay) / 7.0)) + 1; return current_week; } } }
以上就是C#中時(shí)間類的使用方法詳解的詳細(xì)內(nèi)容,更多關(guān)于C# 時(shí)間類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)獲取電腦硬件顯卡信息的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取電腦硬件顯卡信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01c# GridControl的模糊查詢實(shí)現(xiàn)代碼
這篇文章主要介紹了c# GridControl的模糊查詢實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例
這篇文章主要介紹了c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例,需要的朋友可以參考下2014-04-04C#中數(shù)組、ArrayList和List三者的區(qū)別詳解
這篇文章主要介紹了C#中數(shù)組、ArrayList和List三者的區(qū)別詳解,對于三者之間的區(qū)別想要了解的可以進(jìn)來了解一下。2016-12-12C#實(shí)現(xiàn)讀取二維數(shù)組集合并輸出到Word預(yù)設(shè)表格
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)讀取二維數(shù)組集合并輸出到Word預(yù)設(shè)表格,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03