添加N周前后方法

This commit is contained in:
gouguoyin 2020-09-12 08:53:59 +08:00
parent b3e47c6b64
commit 944620d9d7
2 changed files with 33 additions and 0 deletions

View File

@ -164,6 +164,15 @@ c.Now().SubMonths(3).ToDateTimeString() // 2020-06-08 13:00:00
// 一月前
c.Now().SubMonth().ToDateTimeString() // 2020-08-08 13:00:00
// 三周后
c.Now().AddWeeks(3).ToDateTimeString() // 2020-09-29 13:00:00
// 一周后
c.Now().AddWeek().ToDateTimeString() // 2020-09-15 13:00:00
// 三周前
c.Now().SubWeeks(3).ToDateTimeString() // 2020-08-17 13:00:00
// 一周前
c.Now().SubWeek().ToDateTimeString() // 2020-09-01 13:00:00
// 三天后
c.Now().AddDays(3).ToDateTimeString() // 2020-09-11 13:00:00
// 一天后

View File

@ -120,6 +120,30 @@ func (c *Carbon) SubMonth() *Carbon {
return c
}
// AddWeeks N周后
func (c *Carbon) AddWeeks(weeks int) *Carbon {
c.Time = c.Time.AddDate(0, 0, weeks*7)
return c
}
// AddWeek 1周后
func (c *Carbon) AddWeek() *Carbon {
c.Time = c.Time.AddDate(0, 0, 7)
return c
}
// SubWeeks N周前
func (c *Carbon) SubWeeks(weeks int) *Carbon {
c.Time = c.Time.AddDate(0, 0, -weeks*7)
return c
}
// SubMonth 减少1月
func (c *Carbon) SubWeek() *Carbon {
c.Time = c.Time.AddDate(0, 0, -7)
return c
}
// AddDays N天后
func (c *Carbon) AddDays(days int) *Carbon {
c.Time = c.Time.AddDate(0, 0, days)