1.安装carbon
go get github.com/uniplaces/carbon
2.使用demo
package main
import (
"log"
"time"
"github.com/uniplaces/carbon"
)
func main() {
log.Println("now:",carbon.Now().DateTimeString()) //标准格式 "2006-01-02 15:04:05" 1 2 3 4 5
today, _ := carbon.NowInLocation("Japan") //日本当地时间
log.Println("japan today:",today)
log.Println("tomorrow:",carbon.Now().AddDays(1)) //明天
log.Println("last week:",carbon.Now().SubWeek()) //上一周
nextOlympics,_ := carbon.CreateFromDate(2016, time.August, 5, "Europe/London") //创建一个时间
nextOlympics = nextOlympics.AddYears(4) //添加4年
log.Println("next olympics are is %d \n",nextOlympics.Year()) //获取时间的年份
if res:=carbon.Now().IsWeekend();res { //判断是否是周末
log.Println("isWeekend:",res)
}else {
log.Println("isWeekend:",res)
}
olytime, err := carbon.Create(2021, time.August, 20, 0, 0, 0,0, "Japan")
if err != nil {
log.Println(err)
}
log.Printf("olytime %s \n",olytime)
now := carbon.Now()
log.Println("one second later is:",now.AddSeconds(1)) //加1s
log.Println("one minute later is:",now.AddMinutes(1)) //加1minutes
log.Println("one hour later is:",now.AddHours(1)) //加1hours
log.Println("one hour one minute one second later is:",now.AddHours(1).AddMinutes(1).AddSeconds(1))
//now.AddWeekdays()
//now.AddWeeks()
//now.AddCenturies()
//now.AddQuarters()
//时间比较函数
//Before After Equal 之前 之后 相等
//Eq Ne Gt Lt Lte Between 大于 小于等于 之间
//IsPast IsFuture IsLeapYear 过去 未来 是否是闰年
//DiffInSeconds DiffInHours DiffDays 计算时间间隔 计算是使用向下取整的
now.SetWeekStartsAt(time.Sunday)
now.SetWeekEndsAt(time.Saturday)
//now.SetWeekendDays() 自定义周末
log.Println("start of day:",now.StartOfDay())
log.Println("end of day:",now.EndOfDay())
log.Println("start of month:",now.StartOfMonth())
log.Println("end of month:",now.EndOfMonth())
}
参考 https://darjun.github.io/2020...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。