Now
Now is a time toolkit for golang
Install
go get -u github.com/jinzhu/now
Usage
Calculating time based on current time
import "github.com/jinzhu/now"
time.Now()
now.BeginningOfMinute()
now.BeginningOfHour()
now.BeginningOfDay()
now.BeginningOfWeek()
now.BeginningOfMonth()
now.BeginningOfQuarter()
now.BeginningOfYear()
now.EndOfMinute()
now.EndOfHour()
now.EndOfDay()
now.EndOfWeek()
now.EndOfMonth()
now.EndOfQuarter()
now.EndOfYear()
now.WeekStartDay = time.Monday
now.EndOfWeek()
Calculating time based on another time
t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
now.With(t).EndOfMonth()
Calculating time based on configuration
location, err := time.LoadLocation("Asia/Shanghai")
myConfig := &now.Config{
WeekStartDay: time.Monday,
TimeLocation: location,
TimeFormats: []string{"2006-01-02 15:04:05"},
}
t := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location())
myConfig.With(t).BeginningOfWeek()
myConfig.Parse("2002-10-12 22:14:01")
myConfig.Parse("2002-10-12 22:14")
Monday/Sunday
Don't be bothered with the WeekStartDay
setting, you can use Monday
, Sunday
now.Monday()
now.Monday("17:44")
now.Sunday()
now.Sunday("18:19:24")
now.EndOfSunday()
t := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.Now().Location())
now.With(t).Monday()
now.With(t).Monday("17:44")
now.With(t).Sunday()
now.With(t).Sunday("18:19:24")
now.With(t).EndOfSunday()
Parse String to Time
time.Now()
t, err := now.Parse("2017")
t, err := now.Parse("2017-10")
t, err := now.Parse("2017-10-13")
t, err := now.Parse("1999-12-12 12")
t, err := now.Parse("1999-12-12 12:20")
t, err := now.Parse("1999-12-12 12:20:21")
t, err := now.Parse("10-13")
t, err := now.Parse("12:20")
t, err := now.Parse("12:20:13")
t, err := now.Parse("14")
t, err := now.Parse("99:99")
now.MustParse("2013-01-13")
now.MustParse("02-17")
now.MustParse("2-17")
now.MustParse("8")
now.MustParse("2002-10-12 22:14")
now.MustParse("99:99")
Extend now
to support more formats is quite easy, just update now.TimeFormats
with other time layouts, e.g:
now.TimeFormats = append(now.TimeFormats, "02 Jan 2006 15:04")
Please send me pull requests if you want a format to be supported officially
Contributing
You can help to make the project better, check out http://gorm.io/contribute.html for things you can do.
Author
jinzhu
License
Released under the MIT License.