Documentation ¶
Overview ¶
Package datetime implements some functions to format date and time. Note: 1. `format` param in FormatTimeToStr function should be as flow (case no sensitive): "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" "yyyymmdd" "mmddyy" "yyyy" "yy" "mm" "hh:mm:ss" "hh:mm" "mm:ss"
Index ¶
- func AddDay(t time.Time, day int64) time.Time
- func AddHour(t time.Time, hour int64) time.Time
- func AddMinute(t time.Time, minute int64) time.Time
- func AddYear(t time.Time, year int64) time.Time
- func BeginOfDay(t time.Time) time.Time
- func BeginOfHour(t time.Time) time.Time
- func BeginOfMinute(t time.Time) time.Time
- func BeginOfMonth(t time.Time) time.Time
- func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
- func BeginOfYear(t time.Time) time.Time
- func BetweenSeconds(t1 time.Time, t2 time.Time) int64
- func DayOfYear(t time.Time) int
- func EndOfDay(t time.Time) time.Time
- func EndOfHour(t time.Time) time.Time
- func EndOfMinute(t time.Time) time.Time
- func EndOfMonth(t time.Time) time.Time
- func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
- func EndOfYear(t time.Time) time.Time
- func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
- func FormatTimeToStr(t time.Time, format string, timezone ...string) string
- func GetNightTimestamp() int64
- func GetNowDate() string
- func GetNowDateTime() string
- func GetNowTime() string
- func GetTodayEndTime() string
- func GetTodayStartTime() string
- func GetZeroHourTimestamp() int64
- func IsLeapYear(year int) bool
- func IsWeekend(t time.Time) bool
- func NewFormat(t string) (*theTime, error)
- func NewISO8601(iso8601 string) (*theTime, error)
- func NewUnix(unix int64) *theTime
- func NewUnixNow() *theTime
- func NowDateOrTime(format string, timezone ...string) string
- func Timestamp(timezone ...string) int64
- func TimestampMicro(timezone ...string) int64
- func TimestampMilli(timezone ...string) int64
- func TimestampNano(timezone ...string) int64
- func TraceFuncTime() func()
Examples ¶
- AddDay
- AddHour
- AddMinute
- AddYear
- BeginOfDay
- BeginOfHour
- BeginOfMinute
- BeginOfMonth
- BeginOfWeek
- BeginOfYear
- BetweenSeconds
- DayOfYear
- EndOfDay
- EndOfHour
- EndOfMinute
- EndOfMonth
- EndOfWeek
- EndOfYear
- FormatStrToTime
- FormatTimeToStr
- GetNowDate
- GetNowDateTime
- GetNowTime
- IsLeapYear
- IsWeekend
- NewUnix
- NewUnixNow
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddDay ¶
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 ¶
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 ¶
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 AddYear ¶
AddYear add or sub year to the time. Play: https://go.dev/play/p/MqW2ujnBx10
Example ¶
now := time.Now() after1Year := AddYear(now, 1) diff1 := after1Year.Sub(now) before1Year := AddYear(now, -1) diff2 := before1Year.Sub(now) fmt.Println(diff1) fmt.Println(diff2)
Output: 8760h0m0s -8760h0m0s
func BeginOfDay ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 BetweenSeconds ¶
BetweenSeconds returns the number of seconds between two times. Play: https://go.dev/play/p/n3YDRyfyXJu
Example ¶
today := time.Now() tomorrow := AddDay(today, 1) yesterday := AddDay(today, -1) result1 := BetweenSeconds(today, tomorrow) result2 := BetweenSeconds(today, yesterday) fmt.Println(result1) fmt.Println(result2)
Output: 86400 -86400
func DayOfYear ¶
DayOfYear returns which day of the year the parameter date `t` is. Play: https://go.dev/play/p/0hjqhTwFNlH
Example ¶
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local) result1 := DayOfYear(date1) date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local) result2 := DayOfYear(date2) date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local) result3 := DayOfYear(date3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 31 1 0
func EndOfDay ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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") result4 := FormatTimeToStr(datetime, "yyyy-mm-dd hh") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: 2021-01-02 16:04:08 2021-01-02 02-01-21 16:04:08 2021-01-02 16
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 GetTodayEndTime ¶
func GetTodayEndTime() string
GetTodayEndTime return the end time of today, format: yyyy-mm-dd 23:59:59. Play: https://go.dev/play/p/jjrLnfoqgn3
func GetTodayStartTime ¶
func GetTodayStartTime() string
GetTodayStartTime return the start time of today, format: yyyy-mm-dd 00:00:00. Play: https://go.dev/play/p/84siyYF7t99
func GetZeroHourTimestamp ¶
func GetZeroHourTimestamp() int64
GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00). Play: https://go.dev/play/p/QmL2oIaGE3q
func IsLeapYear ¶
IsLeapYear check if param year is leap year or not. Play: https://go.dev/play/p/xS1eS2ejGew
Example ¶
result1 := IsLeapYear(2000) result2 := IsLeapYear(2001) fmt.Println(result1) fmt.Println(result2)
Output: true false
func IsWeekend ¶
IsWeekend checks if passed time is weekend or not. Play: https://go.dev/play/p/cupRM5aZOIY Deprecated Use '== Weekday' instead
Example ¶
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 := IsWeekend(date1) result2 := IsWeekend(date2) result3 := IsWeekend(date3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: true true false
func NewFormat ¶
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 ¶
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 NowDateOrTime ¶
NowDateOrTime return current datetime with specific format and timezone. Play: https://go.dev/play/p/EZ-begEjtT0
func Timestamp ¶
Timestamp return current second timestamp. Play: https://go.dev/play/p/iU5b7Vvjx6x
func TimestampMicro ¶
TimestampMicro return current micro second timestamp. Play: https://go.dev/play/p/2maANglKHQE
func TimestampMilli ¶
TimestampMilli return current mill second timestamp. Play: https://go.dev/play/p/4gvEusOTu1T
func TimestampNano ¶
TimestampNano return current nano second timestamp. Play: https://go.dev/play/p/A9Oq_COrcCF
func TraceFuncTime ¶
func TraceFuncTime() func()
TraceFuncTime: trace the func costed time,just call it at top of the func like `defer TraceFuncTime()()`
Types ¶
This section is empty.