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 ¶
- 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 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 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) (time.Time, error)
- func FormatTimeToStr(t time.Time, format string) string
- func GetNightTimestamp() int64
- func GetNowDate() string
- func GetNowDateTime() string
- func GetNowDateTimestampV1() string
- func GetNowDateTimestampV2() string
- func GetNowTime() string
- func GetZeroHourTimestamp() int64
- func NewFormat(t string) (*theTime, error)
- func NewISO8601(iso8601 string) (*theTime, error)
- func NewUnix(unix int64) *theTime
- func NewUnixNow() *theTime
Examples ¶
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 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 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") 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 GetNowDateTimestampV1 ¶
func GetNowDateTimestampV1() string
GetNowDateTimestampV1 获取当前时间的 年月日时分秒 字符串
func GetNowDateTimestampV2 ¶
func GetNowDateTimestampV2() string
GetNowDateTimestampV2 获取当前时间的 年月日时分 字符串
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 ¶
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
Types ¶
This section is empty.