2020-09-07 17:10:33 +08:00
|
|
|
|
package carbon
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-30 23:11:51 +08:00
|
|
|
|
"strconv"
|
2020-09-07 17:10:33 +08:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-12 08:32:15 +08:00
|
|
|
|
type Carbon struct {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
Time time.Time
|
|
|
|
|
loc *time.Location
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New 初始化,设置默认时区
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func New() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
loc, _ := time.LoadLocation(Local)
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return &Carbon{loc: loc}
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 07:24:56 +08:00
|
|
|
|
// Timezone 设置时区,必须在New()之后,其他方法之前调用
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Timezone(name string) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
loc, _ := time.LoadLocation(name)
|
|
|
|
|
c.loc = loc
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 23:11:51 +08:00
|
|
|
|
// CreateFromTimestamp 将时间戳转成时间实例
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) CreateFromTimestamp(timestamp int64) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = time.Unix(timestamp, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 23:11:51 +08:00
|
|
|
|
// CreateFromDateTime 将年月日时分秒转成时间实例
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) CreateFromDateTime(year int, month time.Month, day int, hour int, minute int, second int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = time.Date(year, month, day, hour, minute, second, 0, c.loc)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 23:11:51 +08:00
|
|
|
|
// CreateFromDate 将年月日转成时间实例
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) CreateFromDate(year int, month time.Month, day int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = time.Date(year, month, day, 0, 0, 0, 0, c.loc)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 14:35:56 +08:00
|
|
|
|
// CreateFromTime 将时分秒转成时间实例
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) CreateFromTime(hour int, minute int, second int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), hour, minute, second, 0, c.loc)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now 当前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Now() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = time.Now()
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddYears N年后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddYears(years int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(years, 0, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddYear 1年后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddYear() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(1, 0, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubYears N年前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubYears(years int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(-years, 0, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubYear 1年前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubYear() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(-1, 0, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddMonths N月后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddMonths(months int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, months, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddMonth 1月后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddMonth() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 1, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubMonths N月前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubMonths(months int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, -months, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 09:09:28 +08:00
|
|
|
|
// SubMonth 1月前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubMonth() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, -1, 0)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 08:53:59 +08:00
|
|
|
|
// AddWeeks N周后
|
|
|
|
|
func (c *Carbon) AddWeeks(weeks int) *Carbon {
|
2020-09-12 23:56:25 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, weeks*DaysPerWeek)
|
2020-09-12 08:53:59 +08:00
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddWeek 1周后
|
|
|
|
|
func (c *Carbon) AddWeek() *Carbon {
|
2020-09-12 23:56:25 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, DaysPerWeek)
|
2020-09-12 08:53:59 +08:00
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SubWeeks N周前
|
|
|
|
|
func (c *Carbon) SubWeeks(weeks int) *Carbon {
|
2020-09-12 23:56:25 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, -weeks*DaysPerWeek)
|
2020-09-12 08:53:59 +08:00
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 14:35:56 +08:00
|
|
|
|
// SubWeek 1周前
|
2020-09-12 08:53:59 +08:00
|
|
|
|
func (c *Carbon) SubWeek() *Carbon {
|
2020-09-12 23:56:25 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, -DaysPerWeek)
|
2020-09-12 08:53:59 +08:00
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddDays N天后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddDays(days int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, days)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddDay 1天后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddDay() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, 1)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubDays N天前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubDays(days int) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, -days)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubDay 1天前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubDay() *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
c.Time = c.Time.AddDate(0, 0, -1)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddHours N小时后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddHours(hours int) *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := strconv.Itoa(hours) + "h"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddHour 1小时后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddHour() *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "1h"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddHours N小时前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubHours(hours int) *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "-" + strconv.Itoa(hours) + "h"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddHour 1小时前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubHour() *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "-1h"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddMinutes N分钟后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddMinutes(minutes int) *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := strconv.Itoa(minutes) + "m"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddMinute 1分钟后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddMinute() *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "1m"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubMinutes N分钟前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubMinutes(minutes int) *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "-" + strconv.Itoa(minutes) + "m"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubMinute 1分钟前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubMinute() *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "-1m"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddSeconds N秒钟后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddSeconds(second int) *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := strconv.Itoa(second) + "s"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// AddMinute 1秒钟后
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) AddSecond() *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "1s"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubMinutes N秒钟前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubSeconds(second int) *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "-" + strconv.Itoa(second) + "s"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:01:01 +08:00
|
|
|
|
// SubMinute 1秒钟前
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) SubSecond() *Carbon {
|
2020-09-30 23:11:51 +08:00
|
|
|
|
duration := "-1s"
|
2020-10-01 14:35:56 +08:00
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = c.Time.Add(d)
|
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse 解析标准时间格式
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Parse(value string) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
|
layout := "2006-01-02 15:04:05"
|
|
|
|
|
|
|
|
|
|
if len(value) == 10 && strings.Contains(value, "-") {
|
|
|
|
|
layout = "2006-01-02"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(value) == 10 && strings.Contains(value, "/") {
|
|
|
|
|
layout = "2006/01/02"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(value) == 19 && strings.Contains(value, "/") {
|
|
|
|
|
layout = "2006/01/02 15:04:05"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(value) == 14 {
|
|
|
|
|
layout = "20060102150405"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(value) == 8 {
|
|
|
|
|
layout = "20060102"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t, _ := time.ParseInLocation(layout, value, c.loc)
|
|
|
|
|
c.Time = t
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 14:35:56 +08:00
|
|
|
|
// ParseByFormat 解析自定义时间格式
|
|
|
|
|
func (c *Carbon) ParseByFormat(value string, format string) *Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
|
layout := format2layout(format)
|
|
|
|
|
t, _ := time.ParseInLocation(layout, value, c.loc)
|
|
|
|
|
c.Time = t
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 14:35:56 +08:00
|
|
|
|
// ParseByDuration 解析相对时间字符串(相对于今天)
|
|
|
|
|
func (c *Carbon) ParseByDuration(duration string) *Carbon {
|
|
|
|
|
d, _ := time.ParseDuration(duration)
|
|
|
|
|
c.Time = time.Now().Add(d)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:10:33 +08:00
|
|
|
|
// Format 格式化时间
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Format(format string) string {
|
2020-09-12 09:17:14 +08:00
|
|
|
|
if c.Time.IsZero() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2020-09-07 17:10:33 +08:00
|
|
|
|
return c.Time.In(c.loc).Format(format2layout(format))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Today 今天
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Today() string {
|
2020-09-13 12:44:17 +08:00
|
|
|
|
now := time.Now()
|
|
|
|
|
return now.In(c.loc).Format("2006-01-02")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StartOfToday 今天开始时间
|
|
|
|
|
func (c *Carbon) StartOfToday() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.Today() + " 00:00:00"
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EndOfToday 今天结束时间
|
|
|
|
|
func (c *Carbon) EndOfToday() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.Today() + " 23:59:59"
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tomorrow 明天
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Tomorrow() string {
|
2020-09-13 12:44:17 +08:00
|
|
|
|
tomorrow := time.Now().AddDate(0, 0, 1)
|
|
|
|
|
return tomorrow.In(c.loc).Format("2006-01-02")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StartOfTomorrow 明天开始时间
|
|
|
|
|
func (c *Carbon) StartOfTomorrow() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.Tomorrow() + " 00:00:00"
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EndOfTomorrow 明天结束时间
|
|
|
|
|
func (c *Carbon) EndOfTomorrow() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.Tomorrow() + " 23:59:59"
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Yesterday 昨天
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) Yesterday() string {
|
2020-09-13 15:08:23 +08:00
|
|
|
|
yesterday := time.Now().AddDate(0, 0, -1)
|
|
|
|
|
return yesterday.In(c.loc).Format("2006-01-02")
|
2020-09-08 16:01:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 12:44:17 +08:00
|
|
|
|
// StartOfYesterday 昨天开始时间
|
|
|
|
|
func (c *Carbon) StartOfYesterday() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.Yesterday() + " 00:00:00"
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EndOfYesterday 昨天结束时间
|
|
|
|
|
func (c *Carbon) EndOfYesterday() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.Yesterday() + " 23:59:59"
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 03:32:03 +08:00
|
|
|
|
// FirstDayInYear 当年第一天
|
2020-09-13 07:24:56 +08:00
|
|
|
|
func (c *Carbon) FirstOfYear() string {
|
2020-09-13 15:08:23 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), 01, 01).ToDateStartString()
|
2020-09-08 16:01:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 03:32:03 +08:00
|
|
|
|
// LastDayInYear 当年最后一天
|
2020-09-13 07:24:56 +08:00
|
|
|
|
func (c *Carbon) LastOfYear() string {
|
2020-09-13 15:08:23 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), 12, 31).ToDateStartString()
|
2020-09-10 09:24:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 03:32:03 +08:00
|
|
|
|
// FirstDayInMonth 当月第一天
|
2020-09-13 07:24:56 +08:00
|
|
|
|
func (c *Carbon) FirstOfMonth() string {
|
2020-09-13 15:08:23 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), 01).ToDateStartString()
|
2020-09-10 09:24:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 03:32:03 +08:00
|
|
|
|
// LastDayInMonth 当月最后一天
|
2020-09-13 07:24:56 +08:00
|
|
|
|
func (c *Carbon) LastOfMonth() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), c.getDays()).ToDateStartString()
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 12:44:17 +08:00
|
|
|
|
// StartOfYear 当年开始时间
|
|
|
|
|
func (c *Carbon) StartOfYear() string {
|
|
|
|
|
return c.CreateFromDate(c.Time.Year(), 01, 01).Format("2006-01-02 00:00:00")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EndOfYear 当年结束时间
|
|
|
|
|
func (c *Carbon) EndOfYear() string {
|
|
|
|
|
return c.CreateFromDateTime(c.Time.Year(), 12, 31, HoursPerDay-1, MinutesPerHour-1, SecondsPerMinute-1).Format("2006-01-02 15:04:05")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StartOfMonth 当月开始时间
|
|
|
|
|
func (c *Carbon) StartOfMonth() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), 01).ToDateStartString()
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EndOfMonth 当月结束时间
|
|
|
|
|
func (c *Carbon) EndOfMonth() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), c.getDays()).ToDateEndString()
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 14:49:32 +08:00
|
|
|
|
// StartOfDay 当天开始时间
|
2020-09-13 12:44:17 +08:00
|
|
|
|
func (c *Carbon) StartOfDay() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), c.Time.Day()).ToDateStartString()
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 14:49:32 +08:00
|
|
|
|
// EndOfDay 当天结束时间
|
2020-09-13 12:44:17 +08:00
|
|
|
|
func (c *Carbon) EndOfDay() string {
|
2020-10-01 03:32:03 +08:00
|
|
|
|
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), c.Time.Day()).ToDateEndString()
|
2020-09-13 12:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:10:33 +08:00
|
|
|
|
// ToDateTimeString 转日期时间字符串
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) ToDateTimeString() string {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
return c.Time.In(c.loc).Format("2006-01-02 15:04:05")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToDateString 转日期字符串
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) ToDateString() string {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
return c.Time.In(c.loc).Format("2006-01-02")
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 13:21:47 +08:00
|
|
|
|
// ToDateStartString 转日期开始时间字符串
|
|
|
|
|
func (c *Carbon) ToDateStartString() string {
|
|
|
|
|
return c.Time.In(c.loc).Format("2006-01-02 00:00:00")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToDateEndString 转日期结束时间字符串
|
|
|
|
|
func (c *Carbon) ToDateEndString() string {
|
|
|
|
|
return c.CreateFromDateTime(c.Time.Year(), c.Time.Month(), c.Time.Day(), HoursPerDay-1, MinutesPerHour-1, SecondsPerMinute-1).Format("2006-01-02 15:04:05")
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:10:33 +08:00
|
|
|
|
// ToTimeString 转时间字符串
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) ToTimeString() string {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
return c.Time.In(c.loc).Format("15:04:05")
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 19:04:34 +08:00
|
|
|
|
// ToTimeStartString 转开始时间字符串
|
|
|
|
|
func (c *Carbon) ToTimeStartString() string {
|
|
|
|
|
return c.Time.In(c.loc).Format("15:00:00")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToDateEndString 转结束时间字符串
|
|
|
|
|
func (c *Carbon) ToTimeEndString() string {
|
|
|
|
|
return c.CreateFromDateTime(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), MinutesPerHour-1, SecondsPerMinute-1).Format("15:04:05")
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:10:33 +08:00
|
|
|
|
// ToTimestamp 转时间戳
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) ToTimestamp() int64 {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
return c.Time.Unix()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsLeapYear 是否是闰年
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsLeapYear() bool {
|
2020-09-07 17:10:33 +08:00
|
|
|
|
year := c.Time.Year()
|
|
|
|
|
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-09 20:14:23 +08:00
|
|
|
|
// IsJanuary 是否是一月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsJanuary() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.January
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsMonday 是否是二月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsFebruary() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.February
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsMarch 是否是三月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsMarch() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.March
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsApril 是否是四月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsApril() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.April
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsMay 是否是五月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsMay() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.May
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsJune 是否是六月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsJune() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.June
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsJuly 是否是七月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsJuly() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.July
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsAugust 是否是八月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsAugust() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.August
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsSeptember 是否是九月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsSeptember() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.September
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsOctober 是否是十月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsOctober() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.October
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsNovember 是否是十一月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsNovember() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.November
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsDecember 是否是十二月
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsDecember() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Month() == time.December
|
2020-09-09 20:14:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:10:33 +08:00
|
|
|
|
// IsMonday 是否是周一
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsMonday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Monday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsTuesday 是否是周二
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsTuesday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Tuesday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsWednesday 是否是周三
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsWednesday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Wednesday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsThursday 是否是周四
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsThursday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Thursday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsFriday 是否是周五
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsFriday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Friday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsSaturday 是否是周六
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsSaturday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Saturday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsSunday 是否是周日
|
2020-09-12 08:32:15 +08:00
|
|
|
|
func (c *Carbon) IsSunday() bool {
|
2020-09-12 22:09:59 +08:00
|
|
|
|
return c.Time.In(c.loc).Weekday() == time.Sunday
|
2020-09-07 17:10:33 +08:00
|
|
|
|
}
|
2020-09-08 16:01:01 +08:00
|
|
|
|
|
2020-09-13 15:08:23 +08:00
|
|
|
|
// IsToday 是否是今天
|
|
|
|
|
func (c *Carbon) IsToday() bool {
|
|
|
|
|
return c.ToDateString() == c.Today()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsYesterday 是否是昨天
|
|
|
|
|
func (c *Carbon) IsYesterday() bool {
|
|
|
|
|
return c.SubDay().ToDateString() == c.Yesterday()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsTomorrow 是否是明天
|
|
|
|
|
func (c *Carbon) IsTomorrow() bool {
|
|
|
|
|
return c.AddDay().ToDateString() == c.Tomorrow()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 07:24:56 +08:00
|
|
|
|
// IsFirstOfYear 是否年初
|
|
|
|
|
func (c *Carbon) IsFirstOfYear() bool {
|
2020-09-10 09:24:02 +08:00
|
|
|
|
_, month, day := c.Time.Date()
|
2020-09-12 22:09:59 +08:00
|
|
|
|
if month == time.January && day == 1 {
|
2020-09-10 09:24:02 +08:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 07:24:56 +08:00
|
|
|
|
// IsLastOfYear 是否是年末
|
|
|
|
|
func (c *Carbon) IsLastOfYear() bool {
|
2020-09-10 09:24:02 +08:00
|
|
|
|
_, month, day := c.Time.Date()
|
2020-09-12 22:09:59 +08:00
|
|
|
|
if month == time.December && day == 31 {
|
2020-09-10 09:24:02 +08:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 07:24:56 +08:00
|
|
|
|
// IsFirstOfMonth 是否月初
|
|
|
|
|
func (c *Carbon) IsFirstOfMonth() bool {
|
2020-09-13 12:44:17 +08:00
|
|
|
|
return c.ToDateTimeString() == c.FirstOfMonth()
|
2020-09-08 16:01:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 07:24:56 +08:00
|
|
|
|
// IsLastOfMonth 是否是月末
|
|
|
|
|
func (c *Carbon) IsLastOfMonth() bool {
|
2020-09-13 12:44:17 +08:00
|
|
|
|
return c.ToDateTimeString() == c.LastOfMonth()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsStartOfYear 是否当年开始时间
|
|
|
|
|
func (c *Carbon) IsStartOfYear() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.StartOfYear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEndOfYear 是否当年结束时间
|
|
|
|
|
func (c *Carbon) IsEndOfYear() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.EndOfYear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsStartOfMonth 是否当月开始时间
|
|
|
|
|
func (c *Carbon) IsStartOfMonth() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.StartOfMonth()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEndOfMonth 是否当月结束时间
|
|
|
|
|
func (c *Carbon) IsEndOfMonth() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.EndOfMonth()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsStartOfDay 是否当天开始时间
|
|
|
|
|
func (c *Carbon) IsStartOfDay() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.StartOfDay()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEndOfDay 是否当天结束时间
|
|
|
|
|
func (c *Carbon) IsEndOfDay() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.EndOfDay()
|
2020-09-08 16:01:01 +08:00
|
|
|
|
}
|
2020-09-13 15:08:23 +08:00
|
|
|
|
|
|
|
|
|
// IsStartOfToday 是否今天开始时间
|
|
|
|
|
func (c *Carbon) IsStartOfToday() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.StartOfToday()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEndOfToday 是否今天结束时间
|
|
|
|
|
func (c *Carbon) IsEndOfToday() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.EndOfToday()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsStartOfTomorrow 是否明天开始时间
|
|
|
|
|
func (c *Carbon) IsStartOfTomorrow() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.StartOfTomorrow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEndOfTomorrow 是否明天结束时间
|
|
|
|
|
func (c *Carbon) IsEndOfTomorrow() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.EndOfTomorrow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsStartOfYesterday 是否昨天开始时间
|
|
|
|
|
func (c *Carbon) IsStartOfYesterday() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.StartOfYesterday()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEndOfYesterday 是否昨天结束时间
|
|
|
|
|
func (c *Carbon) IsEndOfYesterday() bool {
|
|
|
|
|
return c.ToDateTimeString() == c.EndOfYesterday()
|
|
|
|
|
}
|