日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

Go 日期時間包裝器:15 條更便捷的時間處理

來源: 責(zé)編: 時間:2024-01-19 17:29:14 270觀看
導(dǎo)讀在Go編程中,處理日期和時間是一項常見任務(wù),涉及到精確性和靈活性。盡管Go的標(biāo)準(zhǔn)庫提供了時間包(time)用于處理時間相關(guān)操作,但在某些情況下,我們需要額外的實用函數(shù)來簡化這些任務(wù)。本文將介紹一系列實用函數(shù),它們充當(dāng)time包

在Go編程中,處理日期和時間是一項常見任務(wù),涉及到精確性和靈活性。盡管Go的標(biāo)準(zhǔn)庫提供了時間包(time)用于處理時間相關(guān)操作,但在某些情況下,我們需要額外的實用函數(shù)來簡化這些任務(wù)。本文將介紹一系列實用函數(shù),它們充當(dāng)time包的包裝器,提供了更便捷的操作方式。Zli28資訊網(wǎng)——每日最新資訊28at.com

1.獲取月初和月底

獲取月初

func StartOfMonth(date time.Time) time.Time { return time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, date.Location())}// output:2024-01-01 00:00:00 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在月份的第一天。例如,StartOfMonth(time.Now())將返回當(dāng)前月份的第一天的時間戳。Zli28資訊網(wǎng)——每日最新資訊28at.com

獲取月底

func EndOfMonth(date time.Time) time.Time { firstDayOfNextMonth := StartOfMonth(date).AddDate(0, 1, 0) return firstDayOfNextMonth.Add(-time.Second)}// output:2024-01-31 23:59:59 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在月份的最后一天的最后一秒。通過結(jié)合StartOfMonth函數(shù),我們確保了準(zhǔn)確的計算。Zli28資訊網(wǎng)——每日最新資訊28at.com

2.獲取每周的開始日和結(jié)束日

獲取每周的開始日

func StartOfDayOfWeek(date time.Time) time.Time { daysSinceSunday := int(date.Weekday()) return date.AddDate(0, 0, -daysSinceSunday+1)}// output:2024-01-15 00:19:42.869678 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在周的第一天。Zli28資訊網(wǎng)——每日最新資訊28at.com

獲取每周的結(jié)束日

func EndOfDayOfWeek(date time.Time) time.Time { daysUntilSaturday := 7 - int(date.Weekday()) return date.AddDate(0, 0, daysUntilSaturday)}// output:2024-01-21 00:22:06.955558 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在周的最后一天。Zli28資訊網(wǎng)——每日最新資訊28at.com

3.獲取給定月份每周的開始日和結(jié)束日

func StartAndEndOfWeeksOfMonth(year, month int) []struct{ Start, End time.Time } { startOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) weeks := make([]struct{ Start, End time.Time }, 0) for current := startOfMonth; current.Month() == time.Month(month); current = current.AddDate(0, 0, 7) {  startOfWeek := StartOfDayOfWeek(current)  endOfWeek := EndOfDayOfWeek(current)  if endOfWeek.Month() != time.Month(month) {   endOfWeek = EndOfMonth(current)  }  weeks = append(weeks, struct{ Start, End time.Time }{startOfWeek, endOfWeek}) } return weeks}// output:[ {2024-01-01 00:00:00 +0000 UTC 2024-01-07 00:00:00 +0000 UTC}  {2024-01-08 00:00:00 +0000 UTC 2024-01-14 00:00:00 +0000 UTC}  {2024-01-15 00:00:00 +0000 UTC 2024-01-21 00:00:00 +0000 UTC}  {2024-01-22 00:00:00 +0000 UTC 2024-01-28 00:00:00 +0000 UTC}  {2024-01-29 00:00:00 +0000 UTC 2024-01-31 23:59:59 +0000 UTC}]

上述函數(shù)接受年份和月份,返回一個包含給定月份中每周的開始日和結(jié)束日的切片。通過調(diào)用前述的獲取每周開始日和結(jié)束日的函數(shù),我們得到了全面的每周視圖。Zli28資訊網(wǎng)——每日最新資訊28at.com

4.獲取從日期開始的一個月的周數(shù)

func WeekNumberInMonth(date time.Time) int { startOfMonth := StartOfMonth(date) _, week := date.ISOWeek() _, startWeek := startOfMonth.ISOWeek() return week - startWeek + 1}// output:3

上述函數(shù)接受一個日期,返回該日期所在月份的相對周數(shù)。通過利用ISO周數(shù)的概念,我們實現(xiàn)了簡便的計算。Zli28資訊網(wǎng)——每日最新資訊28at.com

5.獲取新年伊始和年底

獲取新年伊始

func StartOfYear(date time.Time) time.Time { return time.Date(date.Year(), time.January, 1, 0, 0, 0, 0, date.Location())}// output:2024-01-01 00:00:00 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在年份的第一天。Zli28資訊網(wǎng)——每日最新資訊28at.com

獲取年底

func EndOfYear(date time.Time) time.Time { startOfNextYear := StartOfYear(date).AddDate(1, 0, 0) return startOfNextYear.Add(-time.Second)}// output:2024-12-31 23:59:59 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在年份的最后一天的最后一秒。Zli28資訊網(wǎng)——每日最新資訊28at.com

6.獲取季度初數(shù)據(jù)和季度末

獲取季度初數(shù)據(jù)

func StartOfQuarter(date time.Time) time.Time { // you can directly use 0, 1, 2, 3 quarter quarter := (int(date.Month()) - 1) / 3 startMonth := time.Month(quarter*3 + 1) return time.Date(date.Year(), startMonth, 1, 0, 0, 0, 0, date.Location())}// output:2024-01-01 00:00:00 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在季度的第一天。Zli28資訊網(wǎng)——每日最新資訊28at.com

獲取季度末

func EndOfQuarter(date time.Time) time.Time { startOfNextQuarter := StartOfQuarter(date).AddDate(0, 3, 0) return startOfNextQuarter.Add(-time.Second)}// output:2024-03-31 23:59:59 +0800 CST

上述函數(shù)接受一個日期,返回該日期所在季度的最后一天的最后一秒。Zli28資訊網(wǎng)——每日最新資訊28at.com

7.獲取當(dāng)前周范圍

func CurrentWeekRange(timeZone string) (startOfWeek, endOfWeek time.Time) { loc, _ := time.LoadLocation(timeZone) now := time.Now().In(loc) startOfWeek = StartOfDayOfWeek(now) endOfWeek = EndOfDayOfWeek(now) return startOfWeek, endOfWeek}// output:2024-01-15 00:37:18.812985 +0800 CST 2024-01-21 00:37:18.812985 +0800 CST

上述函數(shù)接受一個時區(qū)字符串,返回該時區(qū)中當(dāng)前周的開始時間和結(jié)束時間。通過調(diào)用前述的獲取每周開始日和結(jié)束日的函數(shù),我們獲得了當(dāng)前周的范圍。Zli28資訊網(wǎng)——每日最新資訊28at.com

8.計算兩個日期之間的持續(xù)時間

func DurationBetween(start, end time.Time) time.Duration { return end.Sub(start)}// output:10.000000101s

上述函數(shù)接受兩個日期,返回它們之間的持續(xù)時間。這個函數(shù)在測量兩個事件之間經(jīng)過的時間時非常有用。Zli28資訊網(wǎng)——每日最新資訊28at.com

9.獲取給定月份的星期幾的日期

func GetDatesForDayOfWeek(year, month int, day time.Weekday) []time.Time { var dates []time.Time firstDayOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) diff := int(day) - int(firstDayOfMonth.Weekday()) if diff < 0 {  diff += 7 } firstDay := firstDayOfMonth.AddDate(0, 0, diff) for current := firstDay; current.Month() == time.Month(month); current = current.AddDate(0, 0, 7) {  dates = append(dates, current) } return dates}// output:[2024-01-05 00:00:00 +0000 UTC 2024-01-12 00:00:00 +0000 UTC 2024-01-19 00:00:00 +0000 UTC 2024-01-26 00:00:00 +0000 UTC]

上述函數(shù)接受年份、月份和目標(biāo)星期幾,返回給定月份中指定日期的所有出現(xiàn)情況。這為獲取一個月中特定日期的出現(xiàn)提供了通用的解決方案。Zli28資訊網(wǎng)——每日最新資訊28at.com

10.將工作日添加到日期

func AddBusinessDays(startDate time.Time, daysToAdd int) time.Time { currentDate := startDate for i := 0; i < daysToAdd; {  currentDate = currentDate.AddDate(0, 0, 1)  if currentDate.Weekday() != time.Saturday && currentDate.Weekday() != time.Sunday {   i++  } } return currentDate}// output:2024-03-01 00:46:38.131747 +0800 CST

上述函數(shù)接受一個起始日期和要添加的工作日數(shù),返回加上指定工作日后的日期。這對于處理只涉及工作日的應(yīng)用程序非常有用。Zli28資訊網(wǎng)——每日最新資訊28at.com

11.將持續(xù)時間格式化為人類可讀的字符串

func FormatDuration(duration time.Duration) string { days := int(duration.Hours() / 24) hours := int(duration.Hours()) % 24 minutes := int(duration.Minutes()) % 60 seconds := int(duration.Seconds()) % 60 return fmt.Sprintf("%d天 %02d小時 %02d分 %02d秒", days, hours, minutes, seconds)}// output:3天 04小時 15分 30秒

上述函數(shù)接受一個持續(xù)時間,返回一個格式化的字符串,以便更友好地顯示。這對于向用戶呈現(xiàn)持續(xù)時間時非常實用。Zli28資訊網(wǎng)——每日最新資訊28at.com

通過使用這些高級實用函數(shù),我們擴展了日期時間包裝器的功能,為開發(fā)人員提供了一套全面的工具來處理各種與時間相關(guān)的操作。這些函數(shù)可以輕松集成到您的代碼庫中,簡化了復(fù)雜的日期和時間操作,無論是構(gòu)建計劃應(yīng)用程序、生成報告,還是處理各種時間敏感的任務(wù)。Zli28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-65369-0.htmlGo 日期時間包裝器:15 條更便捷的時間處理

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: React高手都善于使用useImprativeHandle

下一篇: 抖音發(fā)布 2023 年不實信息治理盤點:“識別擺拍模型”準(zhǔn)確率達 85%

標(biāo)簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 河曲县| 同心县| 封丘县| 平谷区| 泸水县| 波密县| 黔东| 始兴县| 南通市| 玛纳斯县| 津市市| 长治县| 田林县| 密山市| 温州市| 美姑县| 柞水县| 观塘区| 石林| 玛曲县| 女性| 台东市| 得荣县| 民乐县| 仁布县| 宜川县| 河池市| 冷水江市| 庆元县| 南宫市| 宣化县| 尼勒克县| 冕宁县| 年辖:市辖区| 阿拉善盟| 汶川县| 闽清县| 临夏县| 特克斯县| 德惠市| 中方县|