C#中時間類的使用方法詳解
c#中時間類的使用方法詳解
datetime類
datetime類是c#中最常用的時間類之一,它表示一個日期和時間。可以使用datetime.now屬性獲取當前時間,也可以使用datetime.parse方法將字符串轉換為datetime對象。
// 獲取當前時間 datetime now = datetime.now; // 將字符串轉換為datetime對象 datetime datetime = datetime.parse("2022-01-01 12:00:00"); // 獲取當前時間的年份 int year = now.year; // 獲取當前時間的月份 int month = now.month; // 獲取當前時間的日期 int day = now.day; // 獲取當前時間的小時數 int hour = now.hour; // 獲取當前時間的分鐘數 int minute = now.minute; // 獲取當前時間的秒數 int second = now.second; // 獲取當前時間的毫秒數 int millisecond = now.millisecond;
datetime類還提供了一些常用的方法和屬性,例如:
- datetime.adddays(double value):將當前datetime對象的日期加上指定的天數。
- datetime.addhours(double value):將當前datetime對象的時間加上指定的小時數。
- datetime.addminutes(double value):將當前datetime對象的時間加上指定的分鐘數。
- datetime.addseconds(double value):將當前datetime對象的時間加上指定的秒數。
- datetime.year:獲取當前datetime對象的年份。
- datetime.month:獲取當前datetime對象的月份。
- datetime.day:獲取當前datetime對象的日期。
- datetime.hour:獲取當前datetime對象的小時數。
- datetime.minute:獲取當前datetime對象的分鐘數。
- datetime.second:獲取當前datetime對象的秒數。
timespan類
timespan類表示時間間隔,可以用來計算兩個日期之間的時間差。可以使用timespan.fromdays、timespan.fromhours、timespan.fromminutes、timespan.fromseconds等方法創建timespan對象。
// 創建一個表示1天的timespan對象 timespan oneday = timespan.fromdays(1); // 創建一個表示2小時的timespan對象 timespan twohours = timespan.fromhours(2); // 創建一個表示30分鐘的timespan對象 timespan thirtyminutes = timespan.fromminutes(30); // 創建一個表示10秒的timespan對象 timespan tenseconds = timespan.fromseconds(10);
timespan類還提供了一些常用的方法和屬性,例如:
- timespan.totaldays:獲取timespan對象表示的總天數。
- timespan.totalhours:獲取timespan對象表示的總小時數。
- timespan.totalminutes:獲取timespan對象表示的總分鐘數。
- timespan.totalseconds:獲取timespan對象表示的總秒數。
datetimeoffset類
datetimeoffset類表示一個日期和時間,同時包含時區信息。可以使用datetimeoffset.now屬性獲取當前時間,也可以使用datetimeoffset.parse方法將字符串轉換為datetimeoffset對象。
// 獲取當前時間 datetimeoffset now = datetimeoffset.now // 將字符串轉換為datetimeoffset對象 datetimeoffset datetimeoffset = datetimeoffset.parse("2022-01-01 12:00:00 +08:00");
datetimeoffset類還提供了一些常用的方法和屬性,例如:
- datetimeoffset.tolocaltime():將當前datetimeoffset對象轉換為本地時間。
- datetimeoffset.touniversaltime():將當前datetimeoffset對象轉換為協調世界時(utc)時間。
靜態類的封裝
using system; namespace toolbox.datetimetool { public static class datetimeextend { /// /// 獲取本日開始時間(0點0分0秒) /// /// /// public static datetime getdaystart(this datetime datetime) { return datetime.date; } /// /// 獲取本日結束時間(23點59分59秒) /// /// /// public static datetime getdayend(this datetime datetime) { return datetime.date.adddays(1).addmilliseconds(-1); } /// /// 獲取本周開始時間 /// /// /// public static datetime getweekstart(this datetime datetime) { return datetime.adddays(-(int)datetime.dayofweek + 1).getdaystart(); } /// /// 獲取本周結束時間 /// /// /// public static datetime getweekend(this datetime datetime) { return datetime.adddays(7 - (int)datetime.dayofweek).getdayend(); } /// /// 獲取本月開始時間 /// /// /// public static datetime getmonthstart(this datetime datetime) { return new datetime(datetime.year, datetime.month, 1, 0, 0, 0, 0); } /// /// 獲取本月結束時間 /// /// /// public static datetime getmonthend(this datetime datetime) { return getmonthstart(datetime).addmonths(1).addmilliseconds(-1); } /// /// 獲取本季度開始時間 /// /// /// 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")); } /// /// 獲取本季度結束時間 /// /// /// 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")); } /// /// 獲取本年開始時間 /// /// /// public static datetime getyearstart(this datetime datetime) { return datetime.parse(datetime.adddays(-datetime.dayofyear + 1).tostring("yyyy/mm/dd 00:00:00")); } /// /// 獲取本年結束時間 /// /// /// 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")); } /// /// 北京時間轉換成unix時間戳(10位/秒) /// /// /// public static long beijingtimetounixtimestamp10(this datetime datetime) { return (long)(datetime - new datetime(1970, 1, 1, 8, 0, 0)).totalseconds; } /// /// 格林威治時間轉換成unix時間戳(10位/秒) /// /// /// public static long utctimetounixtimestamp10(this datetime datetime) { return (long)(datetime - new datetime(1970, 1, 1, 0, 0, 0)).totalseconds; } /// /// 北京時間轉換成unix時間戳(13位/毫秒) /// /// /// public static long beijingtimetounixtimestamp13(this datetime datetime) { return (long)(datetime - new datetime(1970, 1, 1, 8, 0, 0)).totalmilliseconds; } /// /// 格林威治時間轉換成unix時間戳(13位/毫秒) /// /// /// public static long utctimetounixtimestamp13(this datetime datetime) { return (long)(datetime - new datetime(1970, 1, 1, 0, 0, 0)).totalmilliseconds; } /// /// 10位unix時間戳轉換成北京時間 /// /// /// public static datetime unixtimestamp10tobeijingtime(this long unixtimestamp) { return new datetime(1970, 1, 1, 8, 0, 0).addseconds(unixtimestamp); } /// /// 10位unix時間戳轉換成格林威治 /// /// /// public static datetime unixtimestamp10toutctime(this long unixtimestamp) { return new datetime(1970, 1, 1, 0, 0, 0).addseconds(unixtimestamp); } /// /// 13位unix時間戳轉換成北京時間 /// /// /// public static datetime unixtimestamp13tobeijingtime(this long unixtimestamp) { return new datetime(1970, 1, 1, 8, 0, 0).addmilliseconds(unixtimestamp); } /// /// 13位unix時間戳轉換成格林威治 /// /// /// public static datetime unixtimestamp13toutctime(this long unixtimestamp) { return new datetime(1970, 1, 1, 0, 0, 0).addmilliseconds(unixtimestamp); } /// /// 當前日期所在月份第一個指定星期幾的日期 /// /// 給定日期 /// 星期幾 /// 所對應的日期 public static datetime getfirstweekdayofmonth(this datetime date, dayofweek dayofweek) { var dt = date.getmonthstart(); while (dt.dayofweek != dayofweek) dt = dt.adddays(1); return dt; } /// /// 當前日期所在月份最后1個指定星期幾的日期 /// /// 給定日期 /// 星期幾 /// 所對應的日期 public static datetime getlastweekdayofmonth(this datetime date, dayofweek dayofweek) { var dt = date.getmonthend(); while (dt.dayofweek != dayofweek) dt = dt.adddays(-1); return dt; } /// /// 判斷是否比指定之間早 /// /// /// /// public static bool isbefore(this datetime date, datetime other) { return date < other; } /// /// 判斷是否比指定時間晚 /// /// /// /// public static bool isafter(this datetime date, datetime other) { return date > other; } /// /// 給定日期所在月份共有多少天 /// /// /// public static int getcountdaysofmonth(this datetime date) { return date.getmonthend().day; } /// /// 當前日期與給定日期是否是同一天 /// /// 當前日期 /// 給定日期 /// public static bool isdateequal(this datetime date, datetime datetocompare) { return date.date == datetocompare.date; } /// /// 是否是周未 /// /// /// public static bool isweekend(this datetime date) { return date.dayofweek == dayofweek.saturday || date.dayofweek == dayofweek.sunday; } /// /// 是否是工作日 /// /// /// public static bool isweekday(this datetime date) { return !date.isweekend(); } /// /// 判斷是否為今天 /// /// /// public static bool istoday(this datetime date) { return date.date == datetime.now.date; } /// /// 判定公歷閏年遵循的一般規律為:四年一閏,百年不閏,四百年再閏。 /// 公歷閏年的精確計算方法:(按一回歸年365天5小時48分45.5秒) /// 普通年能被4整除而不能被100整除的為閏年。 (如2004年就是閏年,1900年不是閏年) /// 世紀年能被400整除而不能被3200整除的為閏年。 (如2000年是閏年,3200年不是閏年) /// 對于數值很大的年份能整除3200,但同時又能整除172800則又是閏年。(如172800年是閏年,86400年不是閏年) /// 公元前閏年規則如下: /// 非整百年:年數除4余數為1是閏年,即公元前1、5、9……年; /// 整百年:年數除400余數為1是閏年,年數除3200余數為1,不是閏年,年數除172800余1又為閏年,即公元前401、801……年。 /// /// /// 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; } /// /// 獲取當前年天數 /// /// /// public static int getdaysbyyear(this datetime datetime) { return (new datetime(datetime.year + 1, 1, 1) - new datetime(datetime.year, 1, 1)).days; } /// /// 獲取當前年天數 /// /// /// 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); //獲取今天是一年當中的第幾天 int currentday = datetime.dayofyear; //(今天 減去 第一周周末)/7 等于 距第一周有多少周 再加上第一周的1 就是今天是今年的第幾周了 // 剛好考慮了惟一的特殊情況就是,今天剛好在第一周內,那么距第一周就是0 再加上第一周的1 最后還是1 int current_week = convert.toint32(math.ceiling((currentday - weekday) / 7.0)) + 1; return current_week; } } }