1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00
Files
lancet/docs/datetime.md
2023-05-31 17:23:47 +08:00

18 KiB

Datetime

Package datetime supports date and time format and compare.

Source:

https://github.com/duke-git/lancet/blob/v1/datetime/datetime.go

Usage:

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

Index

Documentation

Note:

  1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
  • 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

Add or sub days to time.

Signature:

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

Example:

package main

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

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

    fmt.Println(after2Days, before2Days)
}

AddHour

Add or sub hours to time.

Signature:

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

Example:

package main

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

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

    fmt.Println(after2Hours, after2Hours)
}

AddMinute

Add or sub minutes to time.

Signature:

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

Example:

package main

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

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

    fmt.Println(after2Minute, before2Minute)
}

AddYear

Add or sub year to the time.

Signature:

func AddYear(t time.Time, year int64) time.Time

Example:

package main

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

func main() {
    now := time.Now()

	after1Year := datetime.AddYear(now, 1)
	diff1 := after1Year.Sub(now)

	before1Year := datetime.AddYear(now, -1)
	diff2 := before1Year.Sub(now)

	fmt.Println(diff1)
	fmt.Println(diff2)

	// Output:
	// 8760h0m0s
	// -8760h0m0s
}

BeginOfMinute

Return beginning minute time of day.

Signature:

func BeginOfMinute(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return zero time of day.

Signature:

func BeginOfHour(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return begin time of day.

Signature:

func BeginOfDay(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return beginning time of week, week begin from Sunday.

Signature:

func BeginOfWeek(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return beginning time of month

Signature:

func BeginOfMonth(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return beginning time of year.

Signature:

func BeginOfYear(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return end time minute of day.

Signature:

func EndOfMinute(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return end time hour of day.

Signature:

func EndOfHour(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return end time hour of day.

Signature:

func EndOfDay(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return end time of week, week end with Saturday.

Signature:

func EndOfWeek(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return end time of month

Signature:

func EndOfMonth(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Return beginning time of year.

Signature:

func EndOfYear(t time.Time) time.Time

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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

Get current date string, format is yyyy-mm-dd.

Signature:

func GetNowDate() string

Example:

package main

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

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

GetNowTime

Get current time string, format is hh:mm:ss.

Signature:

func GetNowTime() string

Example:

package main

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

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

GetNowDateTime

Get current date time string, format is yyyy-mm-dd hh:mm:ss.

Signature:

func GetNowDateTime() string

Example:

package main

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

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

GetZeroHourTimestamp

Return timestamp of zero hour (timestamp of 00:00).

Signature:

func GetZeroHourTimestamp() int64

Example:

package main

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

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

GetNightTimestamp

Return timestamp of zero hour (timestamp of 23:59).

Signature:

func GetNightTimestamp() int64

Example:

package main

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

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

FormatTimeToStr

Format time to string, `format` param specification see note 1.

Signature:

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

Example:

package main

import (
    "fmt"
    "time"
    "github.com/duke-git/lancet/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 string to time, `format` param specification see note 1.

Signature:

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

Example:

package main

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

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

NewUnixNow

Return unix timestamp of current time

Signature:

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

Example:

package main

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

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

NewUnix

Return unix timestamp of specified int64 value.

Signature:

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

Example:

package main

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

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

NewFormat

Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".

Signature:

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

Example:

package main

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

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

NewISO8601

Return unix timestamp of specified iso8601 time string.

Signature:

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

Example:

package main

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

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

ToUnix

Return unix timestamp.

Signature:

func (t *theTime) ToUnix() int64

Example:

package main

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

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

ToFormat

Return time string 'yyyy-mm-dd hh:mm:ss'.

Signature:

func (t *theTime) ToFormat() string

Example:

package main

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

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

ToFormatForTpl

Return the time string which format is specified tpl.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/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

Return iso8601 time string.

Signature:

func (t *theTime) ToIso8601() string

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/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"
}

IsLeapYear

check if param `year` is leap year or not.

Signature:

func IsLeapYear(year int) bool

Example:

package main

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

func main() {
    result1 := datetime.IsLeapYear(2000)
    result2 := datetime.IsLeapYear(2001)

    fmt.Println(result1)
    fmt.Println(result2)

    // Output:
    // true
    // false
}

BetweenSeconds

Return the number of seconds between two times.

Signature:

func BetweenSeconds(t1 time.Time, t2 time.Time) int64

Example:

package main

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

func main() {
    today := time.Now()
    tomorrow := AddDay(today, 1)
    yesterday := AddDay(today, -1)

    result1 := datetime.BetweenSeconds(today, tomorrow)
    result2 := datetime.BetweenSeconds(today, yesterday)

    fmt.Println(result1)
    fmt.Println(result2)

    // Output:
    // 86400
    // -86400
}

DayOfYear

Returns which day of the year the parameter date `t` is.

Signature:

func DayOfYear(t time.Time) int

Example:

package main

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

func main() {
    date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
    result1 := datetime.DayOfYear(date1)

    date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
    result2 := datetime.DayOfYear(date2)

    date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
    result3 := datetime.DayOfYear(date3)

    fmt.Println(result1)
    fmt.Println(result2)
    fmt.Println(result3)

    // Output:
    // 31
    // 1
    // 0
}

IsWeekend

Checks if passed time is weekend or not.

Signature:

func IsWeekend(t time.Time) bool

Example:

package main

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

func main() {
    date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
    date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
    date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)

    result1 := datetime.IsWeekend(date1)
    result2 := datetime.IsWeekend(date2)
    result3 := datetime.IsWeekend(date3)

    fmt.Println(result1)
    fmt.Println(result2)
    fmt.Println(result3)

    // Output:
    // true
    // true
    // false
}

IsWeekend

判断日期是否是周末。

函数签名:

func IsWeekend(t time.Time) bool

示例:

package main

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

func main() {
    date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
    date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
    date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)

    result1 := datetime.IsWeekend(date1)
    result2 := datetime.IsWeekend(date2)
    result3 := datetime.IsWeekend(date3)

    fmt.Println(result1)
    fmt.Println(result2)
    fmt.Println(result3)

    // Output:
    // true
    // true
    // false
}