Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork241
Now is a time toolkit for golang
License
jinzhu/now
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Now is a time toolkit for golang
go get -u github.com/jinzhu/nowCalculating time based on current time
import"github.com/jinzhu/now"time.Now()// 2013-11-18 17:51:49.123456789 Monnow.BeginningOfMinute()// 2013-11-18 17:51:00 Monnow.BeginningOfHour()// 2013-11-18 17:00:00 Monnow.BeginningOfDay()// 2013-11-18 00:00:00 Monnow.BeginningOfWeek()// 2013-11-17 00:00:00 Sunnow.BeginningOfMonth()// 2013-11-01 00:00:00 Frinow.BeginningOfQuarter()// 2013-10-01 00:00:00 Tuenow.BeginningOfYear()// 2013-01-01 00:00:00 Tuenow.EndOfMinute()// 2013-11-18 17:51:59.999999999 Monnow.EndOfHour()// 2013-11-18 17:59:59.999999999 Monnow.EndOfDay()// 2013-11-18 23:59:59.999999999 Monnow.EndOfWeek()// 2013-11-23 23:59:59.999999999 Satnow.EndOfMonth()// 2013-11-30 23:59:59.999999999 Satnow.EndOfQuarter()// 2013-12-31 23:59:59.999999999 Tuenow.EndOfYear()// 2013-12-31 23:59:59.999999999 Tuenow.WeekStartDay=time.Monday// Set Monday as first day, default is Sundaynow.EndOfWeek()// 2013-11-24 23:59:59.999999999 Sun
Calculating time based on another time
t:=time.Date(2013,02,18,17,51,49,123456789,time.Now().Location())now.With(t).EndOfMonth()// 2013-02-28 23:59:59.999999999 Thu
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())// // 2013-11-18 17:51:49.123456789 MonmyConfig.With(t).BeginningOfWeek()// 2013-11-18 00:00:00 MonmyConfig.Parse("2002-10-12 22:14:01")// 2002-10-12 22:14:01myConfig.Parse("2002-10-12 22:14")// returns error 'can't parse string as time: 2002-10-12 22:14'
Don't be bothered with theWeekStartDay setting, you can useMonday,Sunday
now.Monday()// 2013-11-18 00:00:00 Monnow.Monday("17:44")// 2013-11-18 17:44:00 Monnow.Sunday()// 2013-11-24 00:00:00 Sun (Next Sunday)now.Sunday("18:19:24")// 2013-11-24 18:19:24 Sun (Next Sunday)now.EndOfSunday()// 2013-11-24 23:59:59.999999999 Sun (End of next Sunday)t:=time.Date(2013,11,24,17,51,49,123456789,time.Now().Location())// 2013-11-24 17:51:49.123456789 Sunnow.With(t).Monday()// 2013-11-18 00:00:00 Mon (Last Monday if today is Sunday)now.With(t).Monday("17:44")// 2013-11-18 17:44:00 Mon (Last Monday if today is Sunday)now.With(t).Sunday()// 2013-11-24 00:00:00 Sun (Beginning Of Today if today is Sunday)now.With(t).Sunday("18:19:24")// 2013-11-24 18:19:24 Sun (Beginning Of Today if today is Sunday)now.With(t).EndOfSunday()// 2013-11-24 23:59:59.999999999 Sun (End of Today if today is Sunday)
time.Now()// 2013-11-18 17:51:49.123456789 Mon// Parse(string) (time.Time, error)t,err:=now.Parse("2017")// 2017-01-01 00:00:00, nilt,err:=now.Parse("2017-10")// 2017-10-01 00:00:00, nilt,err:=now.Parse("2017-10-13")// 2017-10-13 00:00:00, nilt,err:=now.Parse("1999-12-12 12")// 1999-12-12 12:00:00, nilt,err:=now.Parse("1999-12-12 12:20")// 1999-12-12 12:20:00, nilt,err:=now.Parse("1999-12-12 12:20:21")// 1999-12-12 12:20:21, nilt,err:=now.Parse("10-13")// 2013-10-13 00:00:00, nilt,err:=now.Parse("12:20")// 2013-11-18 12:20:00, nilt,err:=now.Parse("12:20:13")// 2013-11-18 12:20:13, nilt,err:=now.Parse("14")// 2013-11-18 14:00:00, nilt,err:=now.Parse("99:99")// 2013-11-18 12:20:00, Can't parse string as time: 99:99// MustParse must parse string to time or it will panicnow.MustParse("2013-01-13")// 2013-01-13 00:00:00now.MustParse("02-17")// 2013-02-17 00:00:00now.MustParse("2-17")// 2013-02-17 00:00:00now.MustParse("8")// 2013-11-18 08:00:00now.MustParse("2002-10-12 22:14")// 2002-10-12 22:14:00now.MustParse("99:99")// panic: Can't parse string as time: 99:99
Extendnow to support more formats is quite easy, just updatenow.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
You can help to make the project better, check outhttp://gorm.io/contribute.html for things you can do.
jinzhu
Released under theMIT License.
About
Now is a time toolkit for golang
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.