mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
1、私有方法提取到独立文件private.go里
2、新增StartOf和EndOf类方法
This commit is contained in:
parent
4a3a42417d
commit
12a9bd8ae9
19
README.md
19
README.md
@ -22,7 +22,7 @@ c := carbon.New().Timezone(carbon.PRC)
|
||||
>更多时区常量请查看[timezone.go](https://gitee.com/go-package/carbon/blob/master/timezone.go "timezone.go")文件
|
||||
|
||||
#### 通用用法
|
||||
###### 获取当前时间字符串
|
||||
###### 获取当前时间
|
||||
```go
|
||||
c.Now().Format("Y-m-d H:i:s") // 2020-09-08 13:00:00
|
||||
c.Now().Format("y-m-d h:i:s") // 20-09-08 01:00:00
|
||||
@ -37,14 +37,21 @@ c.Now().ToTimeString() // 13:00:00
|
||||
c.Now().ToTimestamp() // 1599272433
|
||||
```
|
||||
|
||||
###### 获取昨天、今天、明天时间字符串
|
||||
###### 获取昨天、今天、明天
|
||||
```go
|
||||
c.Yesterday() // 2020-09-07 00:00:00
|
||||
c.Today() // 2020-09-08 00:00:00
|
||||
c.Tomorrow() // 2020-09-09 00:00:00
|
||||
c.Yesterday() // 2020-09-07
|
||||
c.Today() // 2020-09-08
|
||||
c.Tomorrow() // 2020-09-09
|
||||
|
||||
c.StartOfYesterday() // 2020-09-07 00:00:00
|
||||
c.EndOfYesterday() // 2020-09-07 23:59:59
|
||||
c.StartOfToday() // 2020-09-08 00:00:00
|
||||
c.EndOfToday() // 2020-09-08 23:59:59
|
||||
c.StartOfTomorrow() // 2020-09-09 00:00:00
|
||||
c.EndOfTomorrow() // 2020-09-09 23:59:59
|
||||
```
|
||||
|
||||
###### 获取第一天、最后一天时间字符串
|
||||
###### 获取第一天、最后一天
|
||||
|
||||
```go
|
||||
// 年初
|
||||
|
24
carbon.go
24
carbon.go
@ -277,13 +277,13 @@ func (c *Carbon) Today() string {
|
||||
// StartOfToday 今天开始时间
|
||||
func (c *Carbon) StartOfToday() string {
|
||||
now := time.Now()
|
||||
return c.getStartDay(now)
|
||||
return c.getStartDay(now).ToDateTimeString()
|
||||
}
|
||||
|
||||
// EndOfToday 今天结束时间
|
||||
func (c *Carbon) EndOfToday() string {
|
||||
now := time.Now()
|
||||
return c.getEndDay(now)
|
||||
return c.getEndDay(now).ToDateTimeString()
|
||||
}
|
||||
|
||||
// Tomorrow 明天
|
||||
@ -295,13 +295,13 @@ func (c *Carbon) Tomorrow() string {
|
||||
// StartOfTomorrow 明天开始时间
|
||||
func (c *Carbon) StartOfTomorrow() string {
|
||||
tomorrow := time.Now().AddDate(0, 0, 1)
|
||||
return c.getStartDay(tomorrow)
|
||||
return c.getStartDay(tomorrow).ToDateTimeString()
|
||||
}
|
||||
|
||||
// EndOfTomorrow 明天结束时间
|
||||
func (c *Carbon) EndOfTomorrow() string {
|
||||
tomorrow := time.Now().AddDate(0, 0, 1)
|
||||
return c.getEndDay(tomorrow)
|
||||
return c.getEndDay(tomorrow).ToDateTimeString()
|
||||
}
|
||||
|
||||
// Yesterday 昨天
|
||||
@ -312,13 +312,13 @@ func (c *Carbon) Yesterday() string {
|
||||
// StartOfYesterday 昨天开始时间
|
||||
func (c *Carbon) StartOfYesterday() string {
|
||||
yesterday := time.Now().AddDate(0, 0, -1)
|
||||
return c.getStartDay(yesterday)
|
||||
return c.getStartDay(yesterday).ToDateTimeString()
|
||||
}
|
||||
|
||||
// EndOfYesterday 昨天结束时间
|
||||
func (c *Carbon) EndOfYesterday() string {
|
||||
yesterday := time.Now().AddDate(0, 0, -1)
|
||||
return c.getEndDay(yesterday)
|
||||
return c.getEndDay(yesterday).ToDateTimeString()
|
||||
}
|
||||
|
||||
// FirstDayInYear 年初第一天
|
||||
@ -333,12 +333,12 @@ func (c *Carbon) LastOfYear() string {
|
||||
|
||||
// FirstDayInMonth 月初第一天
|
||||
func (c *Carbon) FirstOfMonth() string {
|
||||
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), 1).ToDateTimeString()
|
||||
return c.getStartDay(c.Time).Format("2006-01-02 00:00:00")
|
||||
}
|
||||
|
||||
// LastDayInMonth 月末最后一天
|
||||
func (c *Carbon) LastOfMonth() string {
|
||||
return c.CreateFromDate(c.Time.Year(), c.Time.Month(), 1).Time.AddDate(0, 1, -1).Format("2006-01-02 00:00:00")
|
||||
return c.getEndDay(c.Time).Format("2006-01-02 00:00:00")
|
||||
}
|
||||
|
||||
// StartOfYear 当年开始时间
|
||||
@ -354,23 +354,23 @@ func (c *Carbon) EndOfYear() string {
|
||||
// StartOfMonth 当月开始时间
|
||||
func (c *Carbon) StartOfMonth() string {
|
||||
t := c.CreateFromDate(c.Time.Year(), c.Time.Month(), c.Time.Day()).Time
|
||||
return c.getStartDay(t)
|
||||
return c.getStartDay(t).ToDateTimeString()
|
||||
}
|
||||
|
||||
// EndOfMonth 当月结束时间
|
||||
func (c *Carbon) EndOfMonth() string {
|
||||
t := c.CreateFromDate(c.Time.Year(), c.Time.Month(), c.Time.Day()).Time
|
||||
return c.getEndDay(t)
|
||||
return c.getEndDay(t).ToDateTimeString()
|
||||
}
|
||||
|
||||
// FirstDayInMonth 当天开始时间
|
||||
func (c *Carbon) StartOfDay() string {
|
||||
return c.getStartDay(c.Time)
|
||||
return c.getStartDay(c.Time).ToDateTimeString()
|
||||
}
|
||||
|
||||
// LastDayInMonth 当天结束时间
|
||||
func (c *Carbon) EndOfDay() string {
|
||||
return c.getEndDay(c.Time)
|
||||
return c.getEndDay(c.Time).ToDateTimeString()
|
||||
}
|
||||
|
||||
// ToDateTimeString 转日期时间字符串
|
||||
|
@ -261,6 +261,6 @@ func TestCarbon_IsSunday(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCarbon_StartOfDay(t *testing.T) {
|
||||
a := New().Now().EndOfDay()
|
||||
a := New().Now().FirstOfMonth()
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
@ -23,12 +23,12 @@ func format2layout(format string) string {
|
||||
}
|
||||
|
||||
// getStartDay 获取开始日期
|
||||
func (c *Carbon) getStartDay(t time.Time) string {
|
||||
return c.CreateFromDate(t.Year(), t.Month(), 01).Format("2006-01-02 15:04:05")
|
||||
func (c *Carbon) getStartDay(t time.Time) *Carbon {
|
||||
return c.CreateFromDate(t.Year(), t.Month(), 01)
|
||||
}
|
||||
|
||||
// getEndDay 获取结束日期
|
||||
func (c *Carbon) getEndDay(t time.Time) string {
|
||||
func (c *Carbon) getEndDay(t time.Time) *Carbon {
|
||||
days := 30
|
||||
if c.Time.Month() == time.February {
|
||||
days = 28
|
||||
@ -36,5 +36,5 @@ func (c *Carbon) getEndDay(t time.Time) string {
|
||||
if c.IsLeapYear() {
|
||||
days = days + 1
|
||||
}
|
||||
return c.CreateFromDateTime(t.Year(), t.Month(), days-1, HoursPerDay-1, MinutesPerHour-1, SecondsPerMinute-1).Format("2006-01-02 15:04:05")
|
||||
return c.CreateFromDateTime(t.Year(), t.Month(), days-1, HoursPerDay-1, MinutesPerHour-1, SecondsPerMinute-1)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user