A Go library for handling date-time is inspired by Dayjs
daygo.Now().Add(1, daygo.Month).EndOf(daygo.Day).Format("YYYY-MM-DD HH:mm:ss")
- 😀Immutable
- 🔥Chainable
go get -u github.com/siam-ese/daygo
Get a day.D by now time
daygo.Now()
Get a day.D by time.Time
daygo.New(time.Time)
Get a day.D by ISO format string
daygo.Parse(`2021-08-02T05:53:12`)
daygo.Parse(`20210802T055312`)
Get a day.D by int list, default values are used for unset items
// [year is required, month = 1, day = 1, hour = 0, minute = 0, second = 0]
daygo.List([2021, 8]) // 2021-8-1 00:00:00
daygo.List([2021, 8, 2, 19, 13, 54])
Get a day.D by millisecond
daygo.Unix(1627542959340)
Set translator to daygo
import daygo
import daygo/locale
daygo.Locale(locale.EN) // default
daygo.Locale(locale.ZH_CN)
// create your translator
customTranslator = locale.Translator{
WeekMap: [7]string{"monday"}
MonthMap: [12]string{"january"}
}
daygo.Locale(customTranslator)
package daygo
type D struct {
Year int
Month time.Month
Day int
Hour int
Minute int
Second int
Unix int64 // millisecond
UnixNano int64
Weekday time.Weekday
}
D.Time() // return time.Time from D
return a change time new daygo.D
D.Add(1, daygo.Year) // add 1 year
D.Add(1, daygo.Month)
D.Add(1, daygo.Day)
D.Add(1, daygo.Hour)
D.Add(1, daygo.Minute)
D.Add(1, daygo.Second)
D.Add(1, daygo.Weekday) // Monday -> Tuesday, if Sunday Add 1 to Monday
D.Subtract(1, daygo.Year)
D.Subtract(1, daygo.Month)
D.Subtract(1, daygo.Day)
D.Subtract(1, daygo.Hour)
D.Subtract(1, daygo.Minute)
D.Subtract(1, daygo.Second)
D.Subtract(1, daygo.Weekday)
D.Set(2020, daygo.Year) // set year to 2020
D.Set(1, daygo.Month)
D.Set(1, daygo.Day)
D.Set(1, daygo.Hour)
D.Set(1, daygo.Minute)
D.Set(1, daygo.Second)
D.Set(1, daygo.Weekday)
// set method alias
D.SetYear(2020) // set year to 2020
D.SetMonth(1)
D.SetDay(1)
D.SetMinute(1)
D.SetHour(1)
D.SetSecond(1)
D.SetWeekDay(1)
// time 2021-8-3 10:02:55
D.Format("YYYY年MM月DD日,HH时mm分ss秒") // return 2021年08月03日,10时02分55秒
template
YYYY // 2021 Four-digit year
YY // 21 Two-digit year
M // The month, beginning at 1
MM // The month, 2-digits
MMMM // The month name by translator, here is August
D // The day of the month
DD // The day of the month, 2-digits
h // The hour, 12-hour clock
hh // The hour, 12-hour clock, 2-digits
H // The hour 24-hour
HH // The hour, 24-hour 2-digits
m // The minute
mm // The minute, 2-digits
s // The second
ss // The second, 2-digits
SSS // The millisecond, 3-digits 000-999
d // The number of Weekday 1-7
dd // The Weekday name by translator, here is Tuesday
Support: Year, Month, Day, Hour, Minute, Second
// time 2021-8-3 10:02:55
D.StartOf(daygo.Year) // 2021-1-1 00:00:00 Nanosecond is 0e9
D.EndOf(daygo.Year) // 2021-12-31 23:59:59 Nanosecond is nine bits 9
// Of second, is only set millisecond or nanosecond
D.StartOf(daygo.Second)
D.EndOf(daygo.Second)
Get days by day.D current Month
// time 2021-08
D.DaysInMonth() // 31
// time 2021-02
D.DaysInMonth() // 28
return a time.Duration by current day.D sub b-day.D
a := daygo.Now()
b := daygo.Now().Subtract(1, daygo.Minute)
a.From(b) // a - b, return time.Duration
Get a new day.D by UTC or Local
D.UTC()
D.Local()