# Datetime Package datetime supports date and time format and compare.
## Source: - [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go) - [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go) ## Usage: ```go import ( "github.com/duke-git/lancet/v2/datetime" ) ``` ## Index - [AddDay](#AddDay) - [AddHour](#AddHour) - [AddMinute](#AddMinute) - [AddYear](#AddYear) - [BeginOfMinute](#BeginOfMinute) - [BeginOfHour](#BeginOfHour) - [BeginOfDay](#BeginOfDay) - [BeginOfWeek](#BeginOfWeek) - [BeginOfMonth](#BeginOfMonth) - [BeginOfYear](#BeginOfYear) - [EndOfMinute](#EndOfMinute) - [EndOfHour](#EndOfHour) - [EndOfDay](#EndOfDay) - [EndOfWeek](#EndOfWeek) - [EndOfMonth](#EndOfMonth) - [EndOfYear](#EndOfYear) - [GetNowDate](#GetNowDate) - [GetNowTime](#GetNowTime) - [GetNowDateTime](#GetNowDateTime) - [GetTodayStartTime](#GetTodayStartTime) - [GetTodayEndTime](#GetTodayEndTime) - [GetZeroHourTimestamp](#GetZeroHourTimestamp) - [GetNightTimestamp](#GetNightTimestamp) - [FormatTimeToStr](#FormatTimeToStr) - [FormatStrToTime](#FormatStrToTime) - [NewUnixNow](#NewUnixNow) - [NewUnix](#NewUnix) - [NewFormat](#NewFormat) - [NewISO8601](#NewISO8601) - [ToUnix](#ToUnix) - [ToFormat](#ToFormat) - [ToFormatForTpl](#ToFormatForTpl) - [ToIso8601](#ToIso8601) - [IsLeapYear](#IsLeapYear) - [BetweenSeconds](#BetweenSeconds) - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) - [NowDateOrTime](#NowDateOrTime) - [Timestamp](#Timestamp) - [TimestampMilli](#TimestampMilli) - [TimestampMicro](#TimestampMicro) - [TimestampNano](#TimestampNano) ## Documentation ## Note: 1. In below functions, the `format` string param should be one of flows value (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 ### AddDayAdd or sub days to time.
Signature: ```go func AddDay(t time.Time, day int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { now := time.Now() tomorrow := datetime.AddDay(now, 1) diff1 := tomorrow.Sub(now) yesterday := datetime.AddDay(now, -1) diff2 := yesterday.Sub(now) fmt.Println(diff1) fmt.Println(diff2) // Output: // 24h0m0s // -24h0m0s } ``` ### AddHourAdd or sub hours to time.
Signature: ```go func AddHour(t time.Time, hour int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { now := time.Now() after2Hours := datetime.AddHour(now, 2) diff1 := after2Hours.Sub(now) before2Hours := datetime.AddHour(now, -2) diff2 := before2Hours.Sub(now) fmt.Println(diff1) fmt.Println(diff2) // Output: // 2h0m0s // -2h0m0s } ``` ### AddMinuteAdd or sub minutes to time.
Signature: ```go func AddMinute(t time.Time, minute int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { now := time.Now() after2Minutes := datetime.AddMinute(now, 2) diff1 := after2Minutes.Sub(now) before2Minutes := datetime.AddMinute(now, -2) diff2 := before2Minutes.Sub(now) fmt.Println(diff1) fmt.Println(diff2) // Output: // 2m0s // -2m0s } ``` ### AddYearAdd or sub year to the time.
Signature: ```go func AddYear(t time.Time, year int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/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 } ``` ### BeginOfMinuteReturn beginning minute time of day.
Signature: ```go func BeginOfMinute(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.BeginOfMinute(input) fmt.Println(result) // Output: // 2023-01-08 18:50:00 +0000 UTC } ``` ### BeginOfHourReturn zero time of day.
Signature: ```go func BeginOfHour(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.BeginOfHour(input) fmt.Println(result) // Output: // 2023-01-08 18:00:00 +0000 UTC } ``` ### BeginOfDayReturn begin time of day.
Signature: ```go func BeginOfDay(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.BeginOfDay(input) fmt.Println(result) // Output: // 2023-01-08 00:00:00 +0000 UTC } ``` ### BeginOfWeekReturn beginning time of week, week begin from Sunday.
Signature: ```go func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.BeginOfWeek(input) fmt.Println(result) // Output: // 2023-01-08 00:00:00 +0000 UTC } ``` ### BeginOfMonthReturn beginning time of month
Signature: ```go func BeginOfMonth(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.BeginOfMonth(input) fmt.Println(result) // Output: // 2023-01-01 00:00:00 +0000 UTC } ``` ### BeginOfYearReturn beginning time of year.
Signature: ```go func BeginOfYear(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.BeginOfYear(input) fmt.Println(result) // Output: // 2023-01-01 00:00:00 +0000 UTC } ``` ### EndOfMinuteReturn end time minute of day.
Signature: ```go func EndOfMinute(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.EndOfMinute(input) fmt.Println(result) // Output: // 2023-01-08 18:50:59.999999999 +0000 UTC } ``` ### EndOfHourReturn end time hour of day.
Signature: ```go func EndOfHour(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.EndOfHour(input) fmt.Println(result) // Output: // 2023-01-08 18:59:59.999999999 +0000 UTC } ``` ### EndOfDayReturn end time hour of day.
Signature: ```go func EndOfDay(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.EndOfDay(input) fmt.Println(result) // Output: // 2023-01-08 23:59:59.999999999 +0000 UTC } ``` ### EndOfWeekReturn end time of week, week end with Saturday.
Signature: ```go func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.EndOfWeek(input) fmt.Println(result) // Output: // 2023-01-14 23:59:59.999999999 +0000 UTC } ``` ### EndOfMonthReturn end time of month
Signature: ```go func EndOfMonth(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.EndOfMonth(input) fmt.Println(result) // Output: // 2023-01-31 23:59:59.999999999 +0000 UTC } ``` ### EndOfYearReturn beginning time of year.
Signature: ```go func EndOfYear(t time.Time) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) result := datetime.EndOfYear(input) fmt.Println(result) // Output: // 2023-12-31 23:59:59.999999999 +0000 UTC } ``` ### GetNowDateGet current date string, format is yyyy-mm-dd.
Signature: ```go func GetNowDate() string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { currentDate := datetime.GetNowDate() fmt.Println(currentDate) // Output: // 2022-01-28 } ``` ### GetNowTimeGet current time string, format is hh:mm:ss.
Signature: ```go func GetNowTime() string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { currentTime := datetime.GetNowTime() fmt.Println(currentTime) // 15:57:33 // Output: // 15:57:33 } ``` ### GetNowDateTimeGet current date time string, format is yyyy-mm-dd hh:mm:ss.
Signature: ```go func GetNowDateTime() string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { current := datetime.GetNowDateTime() fmt.Println(current) // Output: // 2022-01-28 15:59:33 } ``` ### GetTodayStartTimeReturn the start time of today, format: yyyy-mm-dd 00:00:00.
Signature: ```go func GetTodayStartTime() string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { startTime := datetime.GetTodayStartTime() fmt.Println(startTime) // Output: // 2023-06-29 00:00:00 } ``` ### GetTodayEndTimeReturn the end time of today, format: yyyy-mm-dd 23:59:59.
Signature: ```go func GetTodayEndTime() string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { endTime := datetime.GetTodayEndTime() fmt.Println(endTime) // Output: // 2023-06-29 23:59:59 } ``` ### GetZeroHourTimestampReturn timestamp of zero hour (timestamp of 00:00).
Signature: ```go func GetZeroHourTimestamp() int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { zeroTime := datetime.GetZeroHourTimestamp() fmt.Println(zeroTime) // Output: // 1643299200 } ``` ### GetNightTimestampReturn timestamp of zero hour (timestamp of 23:59).
Signature: ```go func GetNightTimestamp() int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { nightTime := datetime.GetNightTimestamp() fmt.Println(nightTime) // Output: // 1643385599 } ``` ### FormatTimeToStrFormat time to string, `format` param specification see note 1.
Signature: ```go func FormatTimeToStr(t time.Time, format string, timezone ...string) string ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { t, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08") result1 := datetime.FormatTimeToStr(t, "yyyy-mm-dd hh:mm:ss") result2 := datetime.FormatTimeToStr(t, "yyyy-mm-dd") result3 := datetime.FormatTimeToStr(t, "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 } ``` ### FormatStrToTimeFormat string to time, `format` param specification see note 1.
Signature: ```go func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { result1, _ := datetime.FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss") result2, _ := datetime.FormatStrToTime("2021-01-02", "yyyy-mm-dd") result3, _ := datetime.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 } ``` ### NewUnixNowReturn unix timestamp of current time
Signature: ```go type theTime struct { unix int64 } func NewUnixNow() *theTime ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { tm := datetime.NewUnixNow() fmt.Println(tm) // Output: // &{1647597438} } ``` ### NewUnixReturn unix timestamp of specified int64 value.
Signature: ```go type theTime struct { unix int64 } func NewUnix(unix int64) *theTime ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { tm := datetime.NewUnix(1647597438) fmt.Println(tm) // Output: // &{1647597438} } ``` ### NewFormatReturn unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".
Signature: ```go type theTime struct { unix int64 } func NewFormat(t string) (*theTime, error) ``` Example: ```go 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) // Output: // &{1647594245} } ``` ### NewISO8601Return unix timestamp of specified iso8601 time string.
Signature: ```go type theTime struct { unix int64 } func NewISO8601(iso8601 string) (*theTime, error) ``` Example: ```go 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) // Output: // &{1136214245} } ``` ### ToUnixReturn unix timestamp.
Signature: ```go func (t *theTime) ToUnix() int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { tm := datetime.NewUnixNow() fmt.Println(tm.ToUnix()) // Output: // 1647597438 } ``` ### ToFormatReturn time string 'yyyy-mm-dd hh:mm:ss'.
Signature: ```go func (t *theTime) ToFormat() string ``` Example: ```go 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()) // Output: // 2022-03-18 17:04:05 } ``` ### ToFormatForTplReturn the time string which format is specified tpl.
Signature: ```go func (t *theTime) ToFormatForTpl(tpl string) string ``` Example: ```go 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) // Output: // 2022/03/18 17:04:05 } ``` ### ToIso8601Return iso8601 time string.
Signature: ```go func (t *theTime) ToIso8601() string ``` Example: ```go 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) // Output: // 2006-01-02T23:04:05+08:00 } ``` ### IsLeapYearcheck if param `year` is leap year or not.
Signature: ```go func IsLeapYear(year int) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { result1 := datetime.IsLeapYear(2000) result2 := datetime.IsLeapYear(2001) fmt.Println(result1) fmt.Println(result2) // Output: // true // false } ``` ### BetweenSecondsReturn the number of seconds between two times.
Signature: ```go func BetweenSeconds(t1 time.Time, t2 time.Time) int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { today := time.Now() tomorrow := datetime.AddDay(today, 1) yesterday := datetime.AddDay(today, -1) result1 := datetime.BetweenSeconds(today, tomorrow) result2 := datetime.BetweenSeconds(today, yesterday) fmt.Println(result1) fmt.Println(result2) // Output: // 86400 // -86400 } ``` ### DayOfYearReturns which day of the year the parameter date `t` is.
Signature: ```go func DayOfYear(t time.Time) int ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/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 } ``` ### IsWeekendChecks if passed time is weekend or not.
Signature: ```go func IsWeekend(t time.Time) bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/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 } ``` ### NowDateOrTimeReturn current datetime with specific format and timezone.
Signature: ```go func NowDateOrTime(format string, timezone ...string) string ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss") result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST") fmt.Println(result1) fmt.Println(result2) // Output: // 2023-07-26 15:01:30 // 2023-07-26 02:01:30 } ``` ### TimestampReturn current second timestamp.
Signature: ```go func Timestamp(timezone ...string) int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { ts := datetime.Timestamp() fmt.Println(ts) // Output: // 1690363051 } ``` ### TimestampMilliReturn current mill second timestamp.
Signature: ```go func TimestampMilli(timezone ...string) int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { ts := datetime.TimestampMilli() fmt.Println(ts) // Output: // 1690363051331 } ``` ### TimestampMicroReturn current micro second timestamp.
Signature: ```go func TimestampMicro(timezone ...string) int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { ts := datetime.TimestampMicro() fmt.Println(ts) // Output: // 1690363051331784 } ``` ### TimestampNanoReturn current nano second timestamp.
Signature: ```go func TimestampNano(timezone ...string) int64 ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/datetime" ) func main() { ts := datetime.TimestampNano() fmt.Println(ts) // Output: // 1690363051331788000 } ```