如何使用Go語言獲取當天、昨天、明天、某天0點時間戳以及格式化時間
獲取當前時間 到 明天0點整的時間差的秒級時間戳的字符串形式
func GetTimeDifference() string {
nowTime := time.Now()
// 當天秒級時間戳
nowTimeStamp := nowTime.Unix()
nowTimeStr := nowTime.Format("2006-01-02")
//使用Parse 默認獲取為UTC時區(qū) 需要獲取本地時區(qū) 所以使用ParseInLocation
t2, _ := time.ParseInLocation("2006-01-02", nowTimeStr, time.Local)
// 第二天零點時間戳
towTimeStamp := t2.AddDate(0, 0, 1).Unix()
return strconv.FormatInt(towTimeStamp - nowTimeStamp, 10)
}
當天0點時間戳
addTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) timeSamp := addTime.Unix() fmt.Println(timeSamp) // 1649952000

當天0點格式化時間:
t := time.Now()
addTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
timeStr := addTime.Format("2006-01-02")
fmt.Println(timeStr) // 2022-04-15昨日0點時間戳:
ts := time.Now().AddDate(0, 0, -1) timeYesterday := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location()) timeStampYesterday := timeYesterday.Unix() fmt.Println(timeStampYesterday) // 1649865600
昨日0點格式化時間:
ts := time.Now().AddDate(0, 0, -1)
timeYesterday := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location()).Unix()
timeStr := time.Unix(timeYesterday, 0).Format("2006-01-02")
fmt.Println(timeStr) // 2022-04-14或者
ts := time.Now().AddDate(0, 0, -1)
timeYesterday := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location())
timeStr := timeYesterday.Format("2006-01-02")
fmt.Println(timeStr) // 2022-04-14某天的明天0點整
例子1:
過去某天,如 2022-02-28 第二天0點整:2022-03-01 00:00:00
nowTimeStr := "2022-02-28"
//使用Parse 默認獲取為UTC時區(qū) 需要獲取本地時區(qū) 所以使用ParseInLocation
t2, _ := time.ParseInLocation("2006-01-02", nowTimeStr, time.Local)
towTimeStamp := t2.AddDate(0, 0, 1).Unix()
towTimeStr := time.Unix(towTimeStamp, 0).Format("2006-01-02 15:04:05")
fmt.Println(towTimeStr) // 2022-03-01 00:00:00例子2:
未來某天,如 2022-04-30 第二天0點整:2022-05-01
nowTimeStr := "2022-04-30"
//使用Parse 默認獲取為UTC時區(qū) 需要獲取本地時區(qū) 所以使用ParseInLocation
t2, _ := time.ParseInLocation("2006-01-02", nowTimeStr, time.Local)
towTimeStamp := t2.AddDate(0, 0, 1).Unix()
towTimeStr := time.Unix(towTimeStamp, 0).Format("2006-01-02")
fmt.Println(towTimeStr) // 2022-05-01某天的前一天0點整
2022-04-01 前一天0點整:2022-03-31 00:00:00
nowTimeStr := "2022-04-01"
//使用Parse 默認獲取為UTC時區(qū) 需要獲取本地時區(qū) 所以使用ParseInLocation
t2, _ := time.ParseInLocation("2006-01-02", nowTimeStr, time.Local)
towTimeStamp := t2.AddDate(0, 0, -1).Unix()
towTimeStr := time.Unix(towTimeStamp, 0).Format("2006-01-02 15:04:05")
fmt.Println(towTimeStr) // 2022-03-31 00:00:00總結(jié)
到此這篇關(guān)于如何使用Go語言獲取當天、昨天、明天、某天0點時間戳以及格式化時間的文章就介紹到這了,更多相關(guān)Go語言獲取當前時間戳及格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang實現(xiàn)Redis分布式鎖(Lua腳本+可重入+自動續(xù)期)
本文主要介紹了Golang分布式鎖實現(xiàn),采用Redis+Lua腳本確保原子性,持可重入和自動續(xù)期,用于防止超賣及重復(fù)下單,具有一定的參考價值,感興趣的可以了解一下2025-05-05
詳解如何使用Golang操作MongoDB數(shù)據(jù)庫
在現(xiàn)代開發(fā)中,數(shù)據(jù)存儲是一個至關(guān)重要的環(huán)節(jié),MongoDB作為一種NoSQL數(shù)據(jù)庫,提供了強大的功能和靈活的數(shù)據(jù)模型,與Golang的高性能和并發(fā)性能非常契合,本文將探討Golang與MongoDB的完美組合,介紹如何使用Golang操作MongoDB數(shù)據(jù)庫,需要的朋友可以參考下2023-11-11
Go?tablewriter庫提升命令行輸出專業(yè)度實例詳解
命令行工具大家都用過,如果是運維人員可能會編寫命令行工具來完成各種任務(wù),命令行輸出的美觀和易讀性往往容易被忽視,很爛的輸出會讓人感覺不專業(yè),本文將介紹Go語言中牛逼的實戰(zhàn)工具tablewriter庫,使你在命令行輸出中展現(xiàn)出專業(yè)的一面2023-11-11

