1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/docs/datetime_zh-CN.md
2022-10-14 10:20:38 +08:00

15 KiB
Raw Blame History

Datetime

datetime日期时间处理包格式化日期比较日期。

源码:

用法:

import (
    "github.com/duke-git/lancet/v2/datetime"
)

目录

文档

注:

  1. 方法FormatTimeToStr和FormatStrToTime中的format参数值需要传以下类型之一
  • yyyy-mm-dd hh:mm:ss
  • yyyy-mm-dd hh:mm
  • yyyy-mm-dd hh
  • yyyy-mm-dd
  • yyyy-mm
  • mm-dd
  • dd-mm-yy hh:mm:ss
  • yyyy/mm/dd hh:mm:ss
  • yyyy/mm/dd hh:mm
  • yyyy-mm-dd hh
  • yyyy/mm/dd
  • yyyy/mm
  • mm/dd
  • dd/mm/yy hh:mm:ss
  • yyyy
  • mm
  • hh:mm:ss
  • mm:ss

AddDay

将日期加/减天数

函数签名:

func AddDay(t time.Time, day int64) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
	after2Days := datetime.AddDay(now, 2)
	before2Days := datetime.AddDay(now, -2)

    fmt.Println(after2Days, before2Days)
}

AddHour

将日期加/减小时数

函数签名:

func AddHour(t time.Time, hour int64) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    after2Hours := datetime.AddHour(now, 2)
    before2Hours := datetime.AddHour(now, -2)

    fmt.Println(after2Hours, after2Hours)
}

AddMinute

将日期加/减分钟数

函数签名:

func AddMinute(t time.Time, minute int64) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    after2Minute := datetime.AddMinute(now, 2)
    before2Minute := datetime.AddMinute(now, -2)

    fmt.Println(after2Minute, before2Minute)
}

BeginOfMinute

返回指定时间的分钟开始时间

函数签名:

func BeginOfMinute(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.BeginOfMinute(td)
    fmt.Println(bm) //2022-02-15 15:48:00 +0800 CST
}

BeginOfHour

返回指定时间的小时开始时间

函数签名:

func BeginOfHour(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.BeginOfHour(td)
    fmt.Println(bm) //2022-02-15 15:00:00 +0800 CST
}

BeginOfDay

返回指定时间的当天开始时间

函数签名:

func BeginOfDay(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.BeginOfDay(td)
    fmt.Println(bm) //2022-02-15 00:00:00 +0800 CST
}

BeginOfWeek

返回指定时间的每周开始时间,默认开始时间星期日

函数签名:

func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.BeginOfWeek(td)
    fmt.Println(bm) //2022-02-13 00:00:00 +0800 CST
}

BeginOfMonth

返回指定时间的当月开始时间

函数签名:

func BeginOfMonth(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.BeginOfMonth(td)
    fmt.Println(bm) //2022-02-01 00:00:00 +0800 CST
}

BeginOfYear

返回指定时间的当年开始时间

函数签名:

func BeginOfYear(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.BeginOfYear(td)
    fmt.Println(bm) //2022-01-01 00:00:00 +0800 CST
}

EndOfMinute

返回指定时间的分钟结束时间

函数签名:

func EndOfMinute(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.EndOfMinute(td)
    fmt.Println(bm) //2022-02-15 15:48:59.999999999 +0800 CST
}

EndOfHour

返回指定时间的小时结束时间

函数签名:

func EndOfHour(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.EndOfHour(td)
    fmt.Println(bm) //2022-02-15 15:59:59.999999999 +0800 CST
}

EndOfDay

返回指定时间的当天结束时间.

函数签名:

func EndOfDay(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.EndOfDay(td)
    fmt.Println(bm) //2022-02-15 23:59:59.999999999 +0800 CST
}

EndOfWeek

返回指定时间的星期结束时间,默认结束时间星期六

函数签名:

func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.EndOfWeek(td)
    fmt.Println(bm) //2022-02-19 23:59:59.999999999 +0800 CST
}

EndOfMonth

返回指定时间的月份结束时间

函数签名:

func EndOfMonth(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.EndOfMonth(td)
    fmt.Println(bm) //2022-02-28 23:59:59.999999999 +0800 CST
}

EndOfYear

返回指定时间的年份结束时间

函数签名:

func EndOfYear(t time.Time) time.Time

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
    bm := datetime.EndOfYear(td)
    fmt.Println(bm) //2022-12-31 23:59:59.999999999 +0800 CST
}

GetNowDate

获取当天日期返回格式yyyy-mm-dd

函数签名:

func GetNowDate() string

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
	currentDate := datetime.GetNowDate()
    fmt.Println(currentDate) // 2022-01-28
}

GetNowTime

获取当时时间返回格式hh:mm:ss

函数签名:

func GetNowTime() string

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    currentTime := datetime.GetNowTime()
    fmt.Println(currentDate) // 15:57:33
}

GetNowDateTime

获取当时日期和时间返回格式yyyy-mm-dd hh:mm:ss.

函数签名:

func GetNowDateTime() string

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    current := datetime.GetNowDateTime()
    fmt.Println(current) // 2022-01-28 15:59:33
}

GetZeroHourTimestamp

获取零时时间戳(timestamp of 00:00).

函数签名:

func GetZeroHourTimestamp() int64

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    zeroTime := datetime.GetZeroHourTimestamp()
    fmt.Println(zeroTime) // 1643299200
}

GetNightTimestamp

获取午夜时间戳(timestamp of 23:59).

函数签名:

func GetNightTimestamp() int64

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    nightTime := datetime.GetNightTimestamp()
    fmt.Println(nightTime) // 1643385599
}

FormatTimeToStr

将日期格式化成字符串,`format` 参数格式参考注1

函数签名:

func FormatTimeToStr(t time.Time, format string) string

例子:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    now := time.Now()
    timeStr := datetime.FormatTimeToStr(now, "yyyy/mm/dd hh:mm:ss")
    fmt.Println(timeStr) //2022/01/28 16:07:44
}

FormatStrToTime

将字符串格式化成时间,`format` 参数格式参考注1

函数签名:

func FormatStrToTime(str, format string) (time.Time, error)

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    time := datetime.FormatStrToTime("2006-01-02 15:04:05", "yyyy/mm/dd hh:mm:ss")
    fmt.Println(time)
}

NewUnixNow

创建一个当前时间的unix时间戳

函数签名:

type theTime struct {
    unix int64
}
func NewUnixNow() *theTime

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm := datetime.NewUnixNow()
    fmt.Println(tm) //&{1647597438}
}

NewUnix

创建一个unix时间戳

函数签名:

type theTime struct {
    unix int64
}
func NewUnix(unix int64) *theTime

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm := datetime.NewUnix(1647597438)
    fmt.Println(tm) //&{1647597438}
}

NewFormat

创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳

函数签名:

type theTime struct {
    unix int64
}
func NewFormat(t string) (*theTime, error)

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm, err := datetime.NewFormat("2022-03-18 17:04:05")
    fmt.Println(tm) //&{1647594245}
}

NewISO8601

创建一个iso8601格式时间字符串的unix时间戳

函数签名:

type theTime struct {
    unix int64
}
func NewISO8601(iso8601 string) (*theTime, error)

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
    fmt.Println(tm) //&{1136214245}
}

ToUnix

返回unix时间戳

函数签名:

func (t *theTime) ToUnix() int64

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm := datetime.NewUnixNow()
    fmt.Println(tm.ToUnix()) //1647597438
}

ToFormat

返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串

函数签名:

func (t *theTime) ToFormat() string

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
    fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
}

ToFormatForTpl

返回tpl格式指定的日期字符串

函数签名:

func (t *theTime) ToFormatForTpl(tpl string) string

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
    ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
    fmt.Println(ts) //"2022/03/18 17:04:05"
}

ToIso8601

返回iso8601日期字符串

函数签名:

func (t *theTime) ToIso8601() string

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/datetime"
)

func main() {
    tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
    ts := tm.ToIso8601()
    fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
}