乐趣区

关于carbon:carbon基本使用

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…

退出移动版