datetime

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package datetime implements some functions to format date and time. Note: 1. `format` param in FormatTimeToStr function should be as flow: "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"

Index

Examples

Constants

View Source
const (
	DateTimeTemplate  = "2006-01-02 15:04:05"
	ZeroTimeTemplate  = "2006-01-02 00:00:00"
	DateTemplate      = "2006-01-02"
	OnlyTimeTemplate  = "15:04:05"
	ParseTimeTemplate = "2006-01-02T15:04:05+08:00"
	YearMonthTemplate = "2006-01"
)

日期时间模板

Variables

This section is empty.

Functions

func AddDay

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

AddDay add or sub day to the time. Play: https://go.dev/play/p/dIGbs_uTdFa

Example
now := time.Now()

tomorrow := AddDay(now, 1)
diff1 := tomorrow.Sub(now)

yesterday := AddDay(now, -1)
diff2 := yesterday.Sub(now)

fmt.Println(diff1)
fmt.Println(diff2)
Output:

24h0m0s
-24h0m0s

func AddHour

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

AddHour add or sub hour to the time. Play: https://go.dev/play/p/rcMjd7OCsi5

Example
now := time.Now()

after2Hours := AddHour(now, 2)
diff1 := after2Hours.Sub(now)

before2Hours := AddHour(now, -2)
diff2 := before2Hours.Sub(now)

fmt.Println(diff1)
fmt.Println(diff2)
Output:

2h0m0s
-2h0m0s

func AddMinute

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

AddMinute add or sub minute to the time. Play: https://go.dev/play/p/nT1heB1KUUK

Example
now := time.Now()

after2Minutes := AddMinute(now, 2)
diff1 := after2Minutes.Sub(now)

before2Minutes := AddMinute(now, -2)
diff2 := before2Minutes.Sub(now)

fmt.Println(diff1)
fmt.Println(diff2)
Output:

2m0s
-2m0s

func BeginOfDay

func BeginOfDay(t time.Time) time.Time

BeginOfDay return beginning hour time of day. Play: https://go.dev/play/p/94m_UT6cWs9

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := BeginOfDay(input)

fmt.Println(result)
Output:

2023-01-08 00:00:00 +0000 UTC

func BeginOfHour

func BeginOfHour(t time.Time) time.Time

BeginOfHour return beginning hour time of day. Play: https://go.dev/play/p/GhdGFnDWpYs

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := BeginOfHour(input)

fmt.Println(result)
Output:

2023-01-08 18:00:00 +0000 UTC

func BeginOfMinute

func BeginOfMinute(t time.Time) time.Time

BeginOfMinute return beginning minute time of day. Play: https://go.dev/play/p/ieOLVJ9CiFT

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := BeginOfMinute(input)

fmt.Println(result)
Output:

2023-01-08 18:50:00 +0000 UTC

func BeginOfMonth

func BeginOfMonth(t time.Time) time.Time

BeginOfMonth return beginning of month. Play: https://go.dev/play/p/bWXVFsmmzwL

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := BeginOfMonth(input)

fmt.Println(result)
Output:

2023-01-01 00:00:00 +0000 UTC

func BeginOfWeek

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

BeginOfWeek return beginning week, default week begin from Sunday. Play: https://go.dev/play/p/ynjoJPz7VNV

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := BeginOfWeek(input)

fmt.Println(result)
Output:

2023-01-08 00:00:00 +0000 UTC

func BeginOfYear

func BeginOfYear(t time.Time) time.Time

BeginOfYear return the date time at the begin of year. Play: https://go.dev/play/p/i326DSwLnV8

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := BeginOfYear(input)

fmt.Println(result)
Output:

2023-01-01 00:00:00 +0000 UTC

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay return end time of day. Play: https://go.dev/play/p/eMBOvmq5Ih1

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := EndOfDay(input)

fmt.Println(result)
Output:

2023-01-08 23:59:59.999999999 +0000 UTC

func EndOfHour

func EndOfHour(t time.Time) time.Time

EndOfHour return end hour time of day. Play: https://go.dev/play/p/6ce3j_6cVqN

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := EndOfHour(input)

fmt.Println(result)
Output:

2023-01-08 18:59:59.999999999 +0000 UTC

func EndOfMinute

func EndOfMinute(t time.Time) time.Time

EndOfMinute return end minute time of day. Play: https://go.dev/play/p/yrL5wGzPj4z

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := EndOfMinute(input)

fmt.Println(result)
Output:

2023-01-08 18:50:59.999999999 +0000 UTC

func EndOfMonth

func EndOfMonth(t time.Time) time.Time

EndOfMonth return end of month. Play: https://go.dev/play/p/_GWh10B3Nqi

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := EndOfMonth(input)

fmt.Println(result)
Output:

2023-01-31 23:59:59.999999999 +0000 UTC

func EndOfWeek

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

EndOfWeek return end week time, default week end with Saturday. Play: https://go.dev/play/p/i08qKXD9flf

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := EndOfWeek(input)

fmt.Println(result)
Output:

2023-01-14 23:59:59.999999999 +0000 UTC

func EndOfYear

func EndOfYear(t time.Time) time.Time

EndOfYear return the date time at the end of year. Play: https://go.dev/play/p/G01cKlMCvNm

Example
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)

result := EndOfYear(input)

fmt.Println(result)
Output:

2023-12-31 23:59:59.999999999 +0000 UTC

func FormatStrToTime

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

FormatStrToTime convert string to time. Play: https://go.dev/play/p/1h9FwdU8ql4

Example
result1, _ := FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss")
result2, _ := FormatStrToTime("2021-01-02", "yyyy-mm-dd")
result3, _ := FormatStrToTime("02-01-21 16:04:08", "dd-mm-yy hh:mm:ss")

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

2021-01-02 16:04:08 +0000 UTC
2021-01-02 00:00:00 +0000 UTC
2021-01-02 16:04:08 +0000 UTC

func FormatTimeToStr

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

FormatTimeToStr convert time to string. Play: https://go.dev/play/p/_Ia7M8H_OvE

Example
datetime, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08")

result1 := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss")
result2 := FormatTimeToStr(datetime, "yyyy-mm-dd")
result3 := FormatTimeToStr(datetime, "dd-mm-yy hh:mm:ss")

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

2021-01-02 16:04:08
2021-01-02
02-01-21 16:04:08

func GetNightTimestamp

func GetNightTimestamp() int64

GetNightTimestamp return timestamp of zero hour (timestamp of 23:59). Play: https://go.dev/play/p/UolysR3MYP1

func GetNowDate

func GetNowDate() string

GetNowDate return format yyyy-mm-dd of current date. Play: https://go.dev/play/p/PvfkPpcpBBf

Example
result := GetNowDate()

expected := time.Now().Format("2006-01-02")

fmt.Println(result == expected)
Output:

true

func GetNowDateTime

func GetNowDateTime() string

GetNowDateTime return format yyyy-mm-dd hh-mm-ss of current datetime. Play: https://go.dev/play/p/pI4AqngD0al

Example
result := GetNowDateTime()

expected := time.Now().Format("2006-01-02 15:04:05")

fmt.Println(result == expected)
Output:

true

func GetNowTime

func GetNowTime() string

GetNowTime return format hh-mm-ss of current time. Play: https://go.dev/play/p/l7BNxCkTmJS

Example
result := GetNowTime()

expected := time.Now().Format("15:04:05")

fmt.Println(result == expected)
Output:

true

func GetZeroHourTimestamp

func GetZeroHourTimestamp() int64

GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00). Play: https://go.dev/play/p/QmL2oIaGE3q

func NewFormat

func NewFormat(t string) (*theTime, error)

NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss". Play: https://go.dev/play/p/VkW08ZOaXPZ

func NewISO8601

func NewISO8601(iso8601 string) (*theTime, error)

NewISO8601 return unix timestamp of specified iso8601 time string. Play: https://go.dev/play/p/mkhOHQkdeA2

func NewUnix

func NewUnix(unix int64) *theTime

NewUnix return unix timestamp of specified time. Play: https://go.dev/play/p/psoSuh_kLRt

Example
result := NewUnix(1647597438)

fmt.Println(result)
Output:

&{1647597438}

func NewUnixNow

func NewUnixNow() *theTime

NewUnixNow return unix timestamp of current time. Play: https://go.dev/play/p/U4PPx-9D0oz

Example
tm1 := NewUnixNow()

unixTimestamp := tm1.ToUnix()

tm2 := NewUnix(unixTimestamp)

fmt.Println(reflect.DeepEqual(tm1, tm2))
Output:

true

func TodayStartTime added in v0.2.1

func TodayStartTime() string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL