2021-07-24 00:10:47 +08:00
|
|
|
|
# Carbon #
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-31 18:33:39 +08:00
|
|
|
|
[![Carbon Release](https://img.shields.io/github/release/golang-module/carbon.svg)](https://github.com/golang-module/carbon/releases)
|
2021-08-09 22:38:26 +08:00
|
|
|
|
[![Go Test](https://github.com/golang-module/carbon/actions/workflows/test.yml/badge.svg)](https://github.com/golang-module/carbon/actions)
|
2021-07-23 11:05:33 +08:00
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/golang-module/carbon)](https://goreportcard.com/report/github.com/golang-module/carbon)
|
2021-08-09 22:38:26 +08:00
|
|
|
|
[![Go Coverage](https://codecov.io/gh/golang-module/carbon/branch/master/graph/badge.svg)](https://codecov.io/gh/golang-module/carbon)
|
2021-07-31 13:08:43 +08:00
|
|
|
|
[![Carbon Doc](https://img.shields.io/badge/go.dev-reference-brightgreen?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/golang-module/carbon)
|
|
|
|
|
![License](https://img.shields.io/github/license/golang-module/carbon)
|
2021-07-23 11:05:33 +08:00
|
|
|
|
|
2021-08-16 09:34:15 +08:00
|
|
|
|
简体中文 | [English](README.md) | [日本語](README.jp.md)
|
2020-09-07 17:10:33 +08:00
|
|
|
|
|
2021-08-05 19:36:51 +08:00
|
|
|
|
一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用
|
2021-08-02 12:26:33 +08:00
|
|
|
|
|
2021-08-02 14:03:48 +08:00
|
|
|
|
Carbon 已被 [awesome-go](https://github.com/avelino/awesome-go#date-and-time "awesome-go") 收录, 如果您觉得不错,请给个 star 吧
|
2020-09-07 17:32:25 +08:00
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
[github.com/golang-module/carbon](https://github.com/golang-module/carbon "github.com/golang-module/carbon")
|
2020-09-07 17:32:25 +08:00
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
[gitee.com/go-package/carbon](https://gitee.com/go-package/carbon "gitee.com/go-package/carbon")
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
#### 安装使用
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 使用 github 库
|
2020-10-07 17:15:46 +08:00
|
|
|
|
go get -u github.com/golang-module/carbon
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/golang-module/carbon"
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 使用 gitee 库
|
2021-08-16 09:34:15 +08:00
|
|
|
|
go get -u gitee.com/go -package/carbon
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"gitee.com/go-package/carbon"
|
2021-07-24 00:10:47 +08:00
|
|
|
|
)
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
#### 用法示例
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
> 默认时区为 Local,即服务器所在时区,假设当前时间为 2020-08-05 13:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 昨天、今天、明天
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天此刻
|
2021-07-19 09:55:54 +08:00
|
|
|
|
fmt.Sprintf("%s", carbon.Now()) // 2020-08-05 13:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Now().ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天日期
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Now().ToDateString() // 2020-08-05
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天时间
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Now().ToTimeString() // 13:14:15
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 指定时区的今天此刻
|
|
|
|
|
carbon.Now(Carbon.NewYork).ToDateTimeString() // 2020-08-05 14:14:15
|
|
|
|
|
carbon.SetTimezone(Carbon.NewYork).Now().ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天秒级时间戳
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Now().Timestamp() // 1596604455
|
|
|
|
|
carbon.Now().TimestampWithSecond() // 1596604455
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天毫秒级时间戳
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Now().TimestampWithMillisecond() // 1596604455000
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天微秒级时间戳
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Now().TimestampWithMicrosecond() // 1596604455000000
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 今天纳秒级时间戳
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Now().TimestampWithNanosecond() // 1596604455000000000
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 昨天此刻
|
2021-07-19 09:55:54 +08:00
|
|
|
|
fmt.Sprintf("%s", carbon.Yesterday()) // 2020-08-04 13:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Yesterday().ToDateTimeString() // 2020-08-04 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 昨天日期
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Yesterday().ToDateString() // 2020-08-04
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 昨天时间
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.Yesterday().ToTimeString() // 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 指定日期的昨天此刻
|
2021-01-29 09:42:51 +08:00
|
|
|
|
carbon.Parse("2021-01-28 13:14:15").Yesterday().ToDateTimeString() // 2021-01-27 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 指定时区的昨天此刻
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Yesterday(Carbon.NewYork).ToDateTimeString() // 2020-08-04 14:14:15
|
|
|
|
|
carbon.SetTimezone(Carbon.NewYork).Yesterday().ToDateTimeString() // 2020-08-04 14:14:15
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 昨天秒级时间戳
|
|
|
|
|
carbon.Yesterday().Timestamp() // 1596518055
|
|
|
|
|
carbon.Yesterday().TimestampWithSecond() // 1596518055
|
|
|
|
|
// 昨天毫秒级时间戳
|
|
|
|
|
carbon.Yesterday().TimestampWithMillisecond() // 1596518055000
|
|
|
|
|
// 昨天微秒级时间戳
|
|
|
|
|
carbon.Yesterday().TimestampWithMicrosecond() // 1596518055000000
|
|
|
|
|
// 昨天纳秒级时间戳
|
|
|
|
|
carbon.Yesterday().TimestampWithNanosecond() // 1596518055000000000
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 明天此刻
|
2021-07-19 09:55:54 +08:00
|
|
|
|
fmt.Sprintf("%s", carbon.Tomorrow()) // 2020-08-06 13:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 明天日期
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Tomorrow().ToDateString() // 2020-08-06
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 明天时间
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.Tomorrow().ToTimeString() // 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 指定日期的明天此刻
|
2021-01-29 09:42:51 +08:00
|
|
|
|
carbon.Parse("2021-01-28 13:14:15").Tomorrow().ToDateTimeString() // 2021-01-29 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 指定时区的明天此刻
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Tomorrow(Carbon.NewYork).ToDateTimeString() // 2020-08-06 14:14:15
|
|
|
|
|
carbon.SetTimezone(Carbon.NewYork).Tomorrow().ToDateTimeString() // 2020-08-06 14:14:15
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 明天秒级时间戳
|
|
|
|
|
carbon.Tomorrow().Timestamp() // 1596690855
|
|
|
|
|
carbon.Tomorrow().TimestampWithSecond() // 1596690855
|
|
|
|
|
// 明天毫秒级时间戳
|
|
|
|
|
carbon.Tomorrow().TimestampWithMillisecond() // 1596690855000
|
|
|
|
|
// 明天微秒级时间戳
|
|
|
|
|
carbon.Tomorrow().TimestampWithMicrosecond() // 1596690855000000
|
|
|
|
|
// 明天纳秒级时间戳
|
|
|
|
|
carbon.Tomorrow().TimestampWithNanosecond() // 1596690855000000000
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-31 13:08:43 +08:00
|
|
|
|
##### 创建 Carbon 实例
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从秒级时间戳创建 Carbon 实例
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromTimestamp(-1).ToDateTimeString() // 1970-01-01 07:59:59
|
|
|
|
|
carbon.CreateFromTimestamp(-1, carbon.Tokyo).ToDateTimeString() // 1970-01-01 08:59:59
|
|
|
|
|
carbon.CreateFromTimestamp(0).ToDateTimeString() // 1970-01-01 08:00:00
|
|
|
|
|
carbon.CreateFromTimestamp(0, carbon.Tokyo).ToDateTimeString() // 1970-01-01 09:00:00
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从毫秒级时间戳创建 Carbon 实例
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455000).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455000, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从微秒级时间戳创建 Carbon 实例
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455000000).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455000000, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从纳级时间戳创建 Carbon 实例
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455000000000).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromTimestamp(1596604455000000000, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2020-11-02 10:35:18 +08:00
|
|
|
|
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从年月日时分秒创建 Carbon 实例
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从年月日创建 Carbon 实例(时分秒默认为当前时分秒)
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.CreateFromDate(2020, 8, 5).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromDate(2020, 8, 5, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 从时分秒创建 Carbon 实例(年月日默认为当前年月日)
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.CreateFromTime(13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.CreateFromTime(13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-01-18 10:34:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-31 13:08:43 +08:00
|
|
|
|
##### 将标准格式时间字符串解析成 Carbon 实例
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
carbon.Parse("").ToDateTimeString() // 空字符串
|
|
|
|
|
carbon.Parse("0").ToDateTimeString() // 空字符串
|
|
|
|
|
carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // 空字符串
|
|
|
|
|
carbon.Parse("0000-00-00").ToDateTimeString() // 空字符串
|
|
|
|
|
carbon.Parse("00:00:00").ToDateTimeString() // 空字符串
|
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.Parse("2020-08-05").ToDateTimeString() // 2020-08-05 00:00:00
|
2020-10-16 09:27:03 +08:00
|
|
|
|
carbon.Parse("20200805131415").ToDateTimeString() // 2020-08-05 13:14:15
|
2020-10-10 16:11:22 +08:00
|
|
|
|
carbon.Parse("20200805").ToDateTimeString() // 2020-08-05 00:00:00
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05T13:14:15+08:00").ToDateTimeString() // 2020-08-05 13:14:15
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-31 13:08:43 +08:00
|
|
|
|
##### 通过格式化字符将字符串解析成 Carbon 实例
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
> 如果使用的字母与格式化字符冲突时,请使用转义符转义该字母
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-04-27 16:01:19 +08:00
|
|
|
|
carbon.ParseByFormat("2020|08|05 13|14|15", "Y|m|d H|i|s").ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-19 09:55:54 +08:00
|
|
|
|
carbon.ParseByFormat("It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").ToDateTimeString() // 2020-08-05 13:14:15
|
2021-04-27 16:01:19 +08:00
|
|
|
|
carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒").ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.ParseByFormat("2020-08-05 13:14:15", "Y-m-d H:i:s", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-02-01 21:29:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-31 13:08:43 +08:00
|
|
|
|
##### 通过布局字符将字符串解析成 Carbon 实例
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
|
```go
|
2021-06-29 18:19:39 +08:00
|
|
|
|
carbon.ParseByLayout("2020|08|05 13|14|15", "2006|01|02 15|04|05").ToDateTimeString() // 2020-08-05 13:14:15
|
|
|
|
|
carbon.ParseByLayout("It is 2020-08-05 13:14:15", "It is 2006-01-02 15:04:05").ToDateTimeString() // 2020-08-05 13:14:15
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 2006年01月02日15时04分05秒").ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.ParseByLayout("2020-08-05 13:14:15", "2006-01-02 15:04:05", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
|
2021-02-01 21:29:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-31 13:08:43 +08:00
|
|
|
|
##### Carbon 和 time.Time 互转
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
|
```go
|
2021-07-31 13:08:43 +08:00
|
|
|
|
// 将 time.Time 转换成 Carbon
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Time2Carbon(time.Now())
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 将 Carbon 转换成 time.Time
|
2021-06-29 18:19:39 +08:00
|
|
|
|
carbon.Now().Carbon2Time()
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 开始时间、结束时间
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本世纪开始时间
|
2021-06-29 18:19:39 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfCentury().ToDateTimeString() // 2000-01-01 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本世纪结束时间
|
2021-06-29 18:19:39 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfCentury().ToDateTimeString() // 2999-12-31 23:59:59
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本年代开始时间
|
2021-07-09 15:13:34 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfDecade().ToDateTimeString() // 2020-01-01 00:00:00
|
|
|
|
|
carbon.Parse("2021-08-05 13:14:15").StartOfDecade().ToDateTimeString() // 2020-01-01 00:00:00
|
|
|
|
|
carbon.Parse("2029-08-05 13:14:15").StartOfDecade().ToDateTimeString() // 2020-01-01 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本年代结束时间
|
2021-07-09 15:13:34 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfDecade().ToDateTimeString() // 2029-12-31 23:59:59
|
|
|
|
|
carbon.Parse("2021-08-05 13:14:15").EndOfDecade().ToDateTimeString() // 2029-12-31 23:59:59
|
|
|
|
|
carbon.Parse("2029-08-05 13:14:15").EndOfDecade().ToDateTimeString() // 2029-12-31 23:59:59
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本年开始时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfYear().ToDateTimeString() // 2020-01-01 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本年结束时间
|
2020-11-02 22:44:35 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfYear().ToDateTimeString() // 2020-12-31 23:59:59
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本季度开始时间
|
2021-06-29 18:19:39 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfQuarter().ToDateTimeString() // 2020-07-01 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本季度结束时间
|
2021-06-29 18:19:39 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfQuarter().ToDateTimeString() // 2020-09-30 23:59:59
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本月开始时间
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfMonth().ToDateTimeString() // 2020-08-01 00:00:00
|
|
|
|
|
// 本月结束时间
|
2020-11-02 22:44:35 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToDateTimeString() // 2020-08-31 23:59:59
|
2020-10-22 09:34:10 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本周开始时间
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfWeek().ToDateTimeString() // 2020-08-02 00:00:00
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetWeekStartsAt(carbon.Sunday).StartOfWeek().ToDateTimeString() // 2020-08-02 00:00:00
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetWeekStartsAt(carbon.Monday).StartOfWeek().ToDateTimeString() // 2020-08-03 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本周结束时间
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfWeek().ToDateTimeString() // 2020-08-08 23:59:59
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetWeekStartsAt(carbon.Sunday).EndOfWeek().ToDateTimeString() // 2020-08-08 23:59:59
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetWeekStartsAt(carbon.Monday).EndOfWeek().ToDateTimeString() // 2020-08-09 23:59:59
|
2020-10-22 09:34:10 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本日开始时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本日结束时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfDay().ToDateTimeString() // 2020-08-05 23:59:59
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本小时开始时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfHour().ToDateTimeString() // 2020-08-05 13:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本小时结束时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfHour().ToDateTimeString() // 2020-08-05 13:59:59
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本分钟开始时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfMinute().ToDateTimeString() // 2020-08-05 13:14:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本分钟结束时间
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfMinute().ToDateTimeString() // 2020-08-05 13:14:59
|
2021-02-18 14:32:31 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本秒开始时间
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfSecond().Format("Y-m-d H:i:s.u") // 2020-08-05 13:14:15.0
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本秒结束时间
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfSecond().Format("Y-m-d H:i:s.u") // 2020-08-05 13:14:15.999
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 时间旅行
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个世纪后
|
2021-01-26 17:08:27 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddCenturies(3).ToDateTimeString() // 2320-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个世纪后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddCenturiesNoOverflow(3).ToDateTimeString() // 2320-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个世纪后
|
2021-01-26 17:08:27 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddCentury().ToDateTimeString() // 2120-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个世纪后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddCenturyNoOverflow().ToDateTimeString() // 2120-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个世纪前
|
2021-01-26 17:08:27 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubCenturies(3).ToDateTimeString() // 1720-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个世纪前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubCenturiesNoOverflow(3).ToDateTimeString() // 1720-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个世纪前
|
2021-01-26 17:08:27 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubCentury().ToDateTimeString() // 1920-02-29 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一世纪前(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubCenturyNoOverflow().ToDateTimeString() // 1920-02-29 13:14:15
|
2021-01-26 17:08:27 +08:00
|
|
|
|
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个年代后
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").Decades(3).ToDateTimeString() // 2050-03-01 13:14:15
|
|
|
|
|
// 三个年代后(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddDecadesNoOverflow(3).ToDateTimeString() // 2050-02-28 13:14:15
|
|
|
|
|
// 一个年代后
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddDecade().ToDateTimeString() // 2030-03-01 13:14:15
|
|
|
|
|
// 一个年代后(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddDecadeNoOverflow().ToDateTimeString() // 2030-02-28 13:14:15
|
|
|
|
|
// 三个年代前
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubDecades(3).ToDateTimeString() // 1990-03-01 13:14:15
|
|
|
|
|
// 三个年代前(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubDecadesNoOverflow(3).ToDateTimeString() // 1990-02-28 13:14:15
|
|
|
|
|
// 一个年代前
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubDecade().ToDateTimeString() // 2010-03-01 13:14:15
|
|
|
|
|
// 一个年代前(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubDecadeNoOverflow().ToDateTimeString() // 2010-02-28 13:14:15
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三年后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddYears(3).ToDateTimeString() // 2023-03-01 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三年后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddYearsNoOverflow(3).ToDateTimeString() // 2023-02-28 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一年后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddYear().ToDateTimeString() // 2021-03-01 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一年后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddYearNoOverflow().ToDateTimeString() // 2021-02-28 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三年前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubYears(3).ToDateTimeString() // 2017-03-01 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三年前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubYearsNoOverflow(3).ToDateTimeString() // 2017-02-28 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一年前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubYear().ToDateTimeString() // 2019-03-01 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一年前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubYearNoOverflow().ToDateTimeString() // 2019-02-28 13:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个季度后
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2019-08-31 13:14:15").AddQuarters(3).ToDateTimeString() // 2019-03-02 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个季度后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2019-08-31 13:14:15").AddQuartersNoOverflow(3).ToDateTimeString() // 2019-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个季度后
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2019-11-30 13:14:15").AddQuarter().ToDateTimeString() // 2020-03-01 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个季度后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2019-11-30 13:14:15").AddQuarterNoOverflow().ToDateTimeString() // 2020-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个季度前
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2019-08-31 13:14:15").SubQuarters(3).ToDateTimeString() // 2019-03-03 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个季度前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2019-08-31 13:14:15").SubQuartersNoOverflow(3).ToDateTimeString() // 2019-02-28 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个季度前
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2020-05-31 13:14:15").SubQuarter().ToDateTimeString() // 2020-03-02 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个季度前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-05-31 13:14:15").SubQuarterNoOverflow().ToDateTimeString() // 2020-02-29 13:14:15
|
2020-11-02 10:35:18 +08:00
|
|
|
|
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个月后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddMonths(3).ToDateTimeString() // 2020-05-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个月后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddMonthsNoOverflow(3).ToDateTimeString() // 2020-05-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个月后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-01-31 13:14:15").AddMonth().ToDateTimeString() // 2020-03-02 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个月后(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-01-31 13:14:15").AddMonthNoOverflow().ToDateTimeString() // 2020-02-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个月前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubMonths(3).ToDateTimeString() // 2019-11-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 三个月前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubMonthsNoOverflow(3).ToDateTimeString() // 2019-11-29 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个月前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-03-31 13:14:15").SubMonth().ToDateTimeString() // 2020-03-02 13:14:15
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 一个月前(月份不溢出)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-03-31 13:14:15").SubMonthNoOverflow().ToDateTimeString() // 2020-02-29 13:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三周后
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddWeeks(3).ToDateTimeString() // 2020-03-21 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一周后
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").AddWeek().ToDateTimeString() // 2020-03-07 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三周前
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubWeeks(3).ToDateTimeString() // 2020-02-08 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一周前
|
2020-11-02 10:35:18 +08:00
|
|
|
|
carbon.Parse("2020-02-29 13:14:15").SubWeek().ToDateTimeString() // 2020-02-22 13:14:15
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三天后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDays(3).ToDateTimeString() // 2020-08-08 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一天后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDay().ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三天前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDays(3).ToDateTimeString() // 2020-08-02 13:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一天前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDay().ToDateTimeString() // 2020-08-04 13:14:15
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三小时后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddHours(3).ToDateTimeString() // 2020-08-05 16:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 二小时半后
|
2020-11-03 07:41:50 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15
|
2021-01-29 09:42:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDuration("2h30m").ToDateTimeString() // 2020-08-05 15:44:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一小时后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 14:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三小时前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubHours(3).ToDateTimeString() // 2020-08-05 10:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 二小时半前
|
2020-11-03 07:41:50 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5h").ToDateTimeString() // 2020-08-05 10:44:15
|
2021-01-29 09:42:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDuration("2h30m").ToDateTimeString() // 2020-08-05 10:44:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一小时前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 12:14:15
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三分钟后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddMinutes(3).ToDateTimeString() // 2020-08-05 13:17:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 二分钟半后
|
2020-11-03 07:41:50 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45
|
2021-01-29 09:42:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDuration("2m30s").ToDateTimeString() // 2020-08-05 13:16:45
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一分钟后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 13:15:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三分钟前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubMinutes(3).ToDateTimeString() // 2020-08-05 13:11:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 二分钟半前
|
2020-11-03 07:41:50 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5m").ToDateTimeString() // 2020-08-05 13:11:45
|
2021-07-24 00:10:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDuration("2m30s").ToDateTimeString() // 2020-08-05 13:11:45
|
|
|
|
|
// 一分钟前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubMinute().ToDateTimeString() // 2020-08-05 13:13:15
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三秒钟后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddSeconds(3).ToDateTimeString() // 2020-08-05 13:14:18
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 二秒钟半后
|
2020-11-03 07:41:50 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:17
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一秒钟后
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").AddSecond().ToDateTimeString() // 2020-08-05 13:14:16
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 三秒钟前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-05 13:14:12
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 二秒钟半前
|
2020-11-03 07:41:50 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 一秒钟前
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
|
2020-10-22 09:34:10 +08:00
|
|
|
|
```
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 时间差
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
|
```go
|
2021-08-06 00:16:08 +08:00
|
|
|
|
// 相差多少年
|
|
|
|
|
carbon.Parse("2021-08-05 13:14:15").DiffInYears(carbon.Parse("2020-08-05 13:14:15")) // -1
|
|
|
|
|
// 相差多少年(绝对值)
|
|
|
|
|
carbon.Parse("2021-08-05 13:14:15").DiffInYearsWithAbs(carbon.Parse("2020-08-05 13:14:15")) // 1
|
|
|
|
|
|
|
|
|
|
// 相差多少月
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInMonths(carbon.Parse("2020-07-05 13:14:15")) // -1
|
|
|
|
|
// 相差多少月(绝对值)
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInMonthsWithAbs(carbon.Parse("2020-07-05 13:14:15")) // 1
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少周
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInWeeks(carbon.Parse("2020-07-28 13:14:15")) // -1
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少周(绝对值)
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInWeeksWithAbs(carbon.Parse("2020-07-28 13:14:15")) // 1
|
2020-10-22 09:34:10 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少天
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInDays(carbon.Parse("2020-08-04 13:14:15")) // -1
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少天(绝对值)
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInDaysWithAbs(carbon.Parse("2020-08-04 13:14:15")) // 1
|
2020-10-22 09:34:10 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少小时
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInHours(carbon.Parse("2020-08-05 12:14:15")) // -1
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少小时(绝对值)
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInHoursWithAbs(carbon.Parse("2020-08-05 12:14:15")) // 1
|
2020-10-22 09:34:10 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少分
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInMinutes(carbon.Parse("2020-08-05 13:13:15")) // -1
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少分(绝对值)
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInMinutesWithAbs(carbon.Parse("2020-08-05 13:13:15")) // 1
|
2020-10-22 09:34:10 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少秒
|
2020-10-22 09:34:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInSeconds(carbon.Parse("2020-08-05 13:14:14")) // -1
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 相差多少秒(绝对值)
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffInSecondsWithAbs(carbon.Parse("2020-08-05 13:14:14")) // 1
|
2021-02-08 09:47:14 +08:00
|
|
|
|
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 对人类友好的可读格式时间差
|
2021-08-16 10:36:41 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffForHumans() // just now
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2019-08-05 13:14:15").DiffForHumans() // 1 year ago
|
|
|
|
|
carbon.Parse("2018-08-05 13:14:15").DiffForHumans() // 2 years ago
|
|
|
|
|
carbon.Parse("2021-08-05 13:14:15").DiffForHumans() // 1 year from now
|
|
|
|
|
carbon.Parse("2022-08-05 13:14:15").DiffForHumans() // 2 years from now
|
2021-07-24 00:10:47 +08:00
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DiffForHumans(carbon.Now()) // 1 year before
|
|
|
|
|
carbon.Parse("2019-08-05 13:14:15").DiffForHumans(carbon.Now()) // 2 years before
|
|
|
|
|
carbon.Parse("2018-08-05 13:14:15").DiffForHumans(carbon.Now()) // 1 year after
|
|
|
|
|
carbon.Parse("2022-08-05 13:14:15").DiffForHumans(carbon.Now()) // 2 years after
|
2020-11-06 09:28:10 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 时间判断
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-11-06 09:28:10 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是零值时间
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("").IsZero() // true
|
|
|
|
|
carbon.Parse("0").IsZero() // true
|
|
|
|
|
carbon.Parse("0000-00-00 00:00:00").IsZero() // true
|
|
|
|
|
carbon.Parse("0000-00-00").IsZero() // true
|
|
|
|
|
carbon.Parse("00:00:00").IsZero() // true
|
|
|
|
|
carbon.Parse("2020-08-05 00:00:00").IsZero() // false
|
|
|
|
|
carbon.Parse("2020-08-05").IsZero() // false
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05").SetTimezone("xxx").IsZero() // false
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是无效时间
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("").IsInvalid() // true
|
|
|
|
|
carbon.Parse("0").IsInvalid() // true
|
|
|
|
|
carbon.Parse("0000-00-00 00:00:00").IsInvalid() // true
|
|
|
|
|
carbon.Parse("0000-00-00").IsInvalid() // true
|
|
|
|
|
carbon.Parse("00:00:00").IsInvalid() // true
|
|
|
|
|
carbon.Parse("2020-08-05 00:00:00").IsInvalid() // false
|
|
|
|
|
carbon.Parse("2020-08-05").IsInvalid() // false
|
|
|
|
|
carbon.Parse("2020-08-05").SetTimezone("xxx").IsInvalid() // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是当前时间
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Now().IsNow() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是未来时间
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Tomorrow().IsFuture() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是过去时间
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Yesterday().IsPast() // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是闰年
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsLeapYear() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是长年
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsLongYear() // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是一月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是二月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是三月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是四月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是五月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是六月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是七月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是八月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是九月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是十月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是十一月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是十二月
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周一
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周二
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周三
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周四
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周五
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周六
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周日
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsSunday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
|
|
|
|
|
// 是否是工作日
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsWeekday() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是周末
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsWeekend() // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是昨天
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-04 13:14:15").IsYesterday() // true
|
|
|
|
|
carbon.Parse("2020-08-04 00:00:00").IsYesterday() // true
|
|
|
|
|
carbon.Parse("2020-08-04").IsYesterday() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是今天
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsToday() // true
|
|
|
|
|
carbon.Parse("2020-08-05 00:00:00").IsToday() // true
|
|
|
|
|
carbon.Parse("2020-08-05").IsToday() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是明天
|
2020-11-06 09:28:10 +08:00
|
|
|
|
carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true
|
|
|
|
|
carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true
|
|
|
|
|
carbon.Parse("2020-08-06").IsTomorrow() // true
|
2021-02-18 14:32:31 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否大于
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-04 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-05 13:14:15")) // false
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare(">", carbon.Parse("2020-08-04 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare(">", carbon.Parse("2020-08-05 13:14:15")) // false
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否小于
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Lt(carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Lt(carbon.Parse("2020-08-05 13:14:15")) // false
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("<", carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("<", carbon.Parse("2020-08-05 13:14:15")) // false
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否等于
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Eq(carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Eq(carbon.Parse("2020-08-05 13:14:00")) // false
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("=", carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("=", carbon.Parse("2020-08-05 13:14:00")) // false
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否不等于
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Ne(carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Ne(carbon.Parse("2020-08-05 13:14:15")) // false
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("!=", carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("<>", carbon.Parse("2020-08-05 13:14:15")) // false
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否大于等于
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Gte(carbon.Parse("2020-08-04 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Gte(carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare(">=", carbon.Parse("2020-08-04 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare(">=", carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否小于等于
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Lte(carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Lte(carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("<=", carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Compare("<=", carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否在两个时间之间(不包括这两个时间)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Between(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // false
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Between(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否在两个时间之间(包括开始时间)
|
2021-07-23 23:03:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedStart(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedStart(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
2021-02-18 14:32:31 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否在两个时间之间(包括结束时间)
|
2021-07-23 23:03:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedEnd(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-05 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedEnd(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
2021-02-18 14:32:31 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否在两个时间之间(包括这两个时间)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedBoth(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedBoth(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-05 13:14:15")) // true
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 12:57:30 +08:00
|
|
|
|
> 关于长年(LongYear)的定义, 请查看 https://en.wikipedia.org/wiki/ISO_8601#Week_dates
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
##### 时间设置
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
```go
|
|
|
|
|
// 设置时区
|
|
|
|
|
carbon.SetTimezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
|
|
|
|
carbon.SetTimezone(carbon.Tokyo).Now().ToDateTimeString() // 2020-08-05 14:14:15
|
|
|
|
|
carbon.SetTimezone(carbon.Tokyo).Now().SetTimezone(carbon.PRC).ToDateTimeString() // 2020-08-05 12:14:15
|
|
|
|
|
|
|
|
|
|
// 设置区域
|
|
|
|
|
carbon.Parse("2020-07-05 13:14:15").SetLocale("en").DiffForHumans() // 1 month ago
|
|
|
|
|
carbon.Parse("2020-07-05 13:14:15").SetLocale("zh-CN").DiffForHumans() // 1 月前
|
|
|
|
|
|
|
|
|
|
// 设置年份
|
|
|
|
|
carbon.Parse("2020-02-29").SetYear(2021).ToDateString() // 2021-03-01
|
|
|
|
|
// 设置年份(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-02-29").SetYearNoOverflow(2021).ToDateString() // 2021-02-28
|
|
|
|
|
|
|
|
|
|
// 设置月份
|
|
|
|
|
carbon.Parse("2020-01-31").SetMonth(2).ToDateString() // 2020-03-02
|
|
|
|
|
// 设置月份(月份不溢出)
|
|
|
|
|
carbon.Parse("2020-01-31").SetMonthNoOverflow(2).ToDateString() // 2020-02-29
|
|
|
|
|
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 设置一周的开始日期
|
|
|
|
|
carbon.Parse("2020-08-02").SetWeekStartsAt(carbon.Monday).Week() // 6
|
|
|
|
|
carbon.Parse("2020-08-02").SetWeekStartsAt(carbon.Sunday).Week() // 0
|
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
// 设置日期
|
|
|
|
|
carbon.Parse("2019-08-05").SetDay(31).ToDateString() // 2020-08-31
|
|
|
|
|
carbon.Parse("2020-02-01").SetDay(31).ToDateString() // 2020-03-02
|
|
|
|
|
|
|
|
|
|
// 设置小时
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetHour(10).ToDateTimeString() // 2020-08-05 10:14:15
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetHour(24).ToDateTimeString() // 2020-08-06 00:14:15
|
|
|
|
|
|
|
|
|
|
// 设置分钟
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetMinute(10).ToDateTimeString() // 2020-08-05 13:10:15
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetMinute(60).ToDateTimeString() // 2020-08-05 14:00:15
|
|
|
|
|
|
|
|
|
|
// 设置秒
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetSecond(10).ToDateTimeString() // 2020-08-05 13:14:10
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetSecond(60).ToDateTimeString() // 2020-08-05 13:15:00
|
|
|
|
|
|
|
|
|
|
// 设置毫秒
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetMillisecond(100).Millisecond() // 100
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetMillisecond(999).Millisecond() // 999
|
|
|
|
|
|
|
|
|
|
// 设置微妙
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetMicrosecond(100000).Microsecond() // 100000
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetMicrosecond(999999).Microsecond() // 999999
|
|
|
|
|
|
|
|
|
|
// 设置纳秒
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetNanosecond(100000000).Nanosecond() // 100000000
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").SetNanosecond(999999999).Nanosecond() // 999999999
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
##### 时间获取
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
```go
|
|
|
|
|
// 获取本年总天数
|
|
|
|
|
carbon.Parse("2019-08-05 13:14:15").DaysInYear() // 365
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DaysInYear() // 366
|
|
|
|
|
// 获取本月总天数
|
|
|
|
|
carbon.Parse("2020-02-01 13:14:15").DaysInMonth() // 29
|
|
|
|
|
carbon.Parse("2020-04-01 13:14:15").DaysInMonth() // 30
|
|
|
|
|
carbon.Parse("2020-08-01 13:14:15").DaysInMonth() // 31
|
|
|
|
|
|
|
|
|
|
// 获取本年第几天
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DayOfYear() // 218
|
|
|
|
|
// 获取本年第几周
|
|
|
|
|
carbon.Parse("2019-12-31 13:14:15").WeekOfYear() // 1
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").WeekOfYear() // 32
|
|
|
|
|
// 获取本月第几天
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DayOfMonth() // 5
|
|
|
|
|
// 获取本月第几周
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").WeekOfMonth() // 1
|
|
|
|
|
// 获取本周第几天
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
|
|
|
|
|
|
|
|
|
|
// 获取当前世纪
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Century() // 21
|
|
|
|
|
// 获取当前年代
|
|
|
|
|
carbon.Parse("2019-08-05 13:14:15").Decade() // 10
|
|
|
|
|
carbon.Parse("2021-08-05 13:14:15").Decade() // 20
|
|
|
|
|
// 获取当前年份
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
|
|
|
|
|
// 获取当前季度
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Quarter() // 3
|
|
|
|
|
// 获取当前月份
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Month() // 8
|
|
|
|
|
// 获取当前周(从0开始)
|
2021-08-16 09:34:15 +08:00
|
|
|
|
carbon.Parse("2020-08-02 13:14:15").Week() // 0
|
|
|
|
|
carbon.Parse("2020-08-02").SetWeekStartsAt(carbon.Sunday).Week() // 0
|
|
|
|
|
carbon.Parse("2020-08-02").SetWeekStartsAt(carbon.Monday).Week() // 6
|
2021-08-09 16:47:20 +08:00
|
|
|
|
// 获取当前天数
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Day() // 5
|
|
|
|
|
// 获取当前小时
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
|
|
|
|
|
// 获取当前分钟
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
|
|
|
|
|
// 获取当前秒钟
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Second() // 15
|
|
|
|
|
// 获取当前毫秒
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Millisecond() // 1596604455000
|
|
|
|
|
// 获取当前微秒
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Microsecond() // 1596604455000000
|
|
|
|
|
// 获取当前纳秒
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Nanosecond() // 1596604455000000000
|
|
|
|
|
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 获取秒级时间戳, Timestamp() 是TimestampWithSecond()的简写
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Timestamp() // 1596604455
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").TimestampWithSecond() // 1596604455
|
|
|
|
|
// 获取毫秒级时间戳
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").TimestampWithMillisecond() // 1596604455000
|
|
|
|
|
// 获取微秒级时间戳
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").TimestampWithMicrosecond() // 1596604455000000
|
|
|
|
|
// 获取纳秒级时间戳
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").TimestampWithNanosecond() // 1596604455000000000
|
|
|
|
|
|
2021-08-09 16:47:20 +08:00
|
|
|
|
// 获取时区
|
|
|
|
|
carbon.SetTimezone(carbon.PRC).Timezone() // CST
|
|
|
|
|
carbon.SetTimezone(carbon.Tokyo).Timezone() // JST
|
|
|
|
|
|
|
|
|
|
// 获取位置
|
|
|
|
|
carbon.SetTimezone(carbon.PRC).Location() // PRC
|
|
|
|
|
carbon.SetTimezone(carbon.Tokyo).Location() // Asia/Tokyo
|
|
|
|
|
|
|
|
|
|
// 获取距离UTC时区的偏移量,单位秒
|
|
|
|
|
carbon.SetTimezone(carbon.PRC).Offset() // 28800
|
|
|
|
|
carbon.SetTimezone(carbon.Tokyo).Offset() // 32400
|
|
|
|
|
|
|
|
|
|
// 获取当前区域
|
|
|
|
|
carbon.Now().Locale() // en
|
|
|
|
|
carbon.Now().SetLocale("zh-CN").Locale() // zh-CN
|
|
|
|
|
|
|
|
|
|
// 获取当前星座
|
|
|
|
|
carbon.Now().Constellation() // Leo
|
|
|
|
|
carbon.Now().SetLocale("en").Constellation() // Leo
|
|
|
|
|
carbon.Now().SetLocale("zh-CN").Constellation() // 狮子座
|
|
|
|
|
|
|
|
|
|
// 获取当前季节
|
|
|
|
|
carbon.Now().Season() // Summer
|
|
|
|
|
carbon.Now().SetLocale("en").Season() // Summer
|
|
|
|
|
carbon.Now().SetLocale("zh-CN").Season() // 夏季
|
|
|
|
|
|
|
|
|
|
// 获取年龄
|
|
|
|
|
carbon.Parse("2002-01-01 13:14:15").Age() // 17
|
|
|
|
|
carbon.Parse("2002-12-31 13:14:15").Age() // 18
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 时间输出
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-08-16 09:34:15 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出日期时间字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString(carbon.Tokyo) // 2020-08-05 14:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出简写日期时间字符串
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToShortDateTimeString() // 20200805131415
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToShortDateTimeString(carbon.Tokyo) // 20200805141415
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出日期字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDateString() // 2020-08-05
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDateString(carbon.Tokyo) // 2020-08-05
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出简写日期字符串
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToShortDateString() // 20200805
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToShortDateString(carbon.Tokyo) // 20200805
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出时间字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToTimeString(carbon.Tokyo) // 14:14:15
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出简写时间字符串
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString(carbon.Tokyo) // 141415
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 Ansic 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToAnsicString(carbon.Tokyo) // Wed Aug 5 14:14:15 2020
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 Atom 格式字符串
|
2021-06-04 16:17:52 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToAtomString(carbon.Tokyo) // 2020-08-05T14:14:15+08:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 UnixDate 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString() // Wed Aug 5 13:14:15 CST 2020
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString(carbon.Tokyo) // Wed Aug 5 14:14:15 JST 2020
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RubyDate 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString() // Wed Aug 05 13:14:15 +0800 2020
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString(carbon.Tokyo) // Wed Aug 05 14:14:15 +0900 2020
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 Kitchen 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToKitchenString() // 1:14PM
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToKitchenString(carbon.Tokyo) // 2:14PM
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 Cookie 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToCookieString() // Wednesday, 05-Aug-2020 13:14:15 CST
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToCookieString(carbon.Tokyo) // Wednesday, 05-Aug-2020 14:14:15 JST
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 DayDateTime 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString() // Wed, Aug 5, 2020 1:14 PM
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString(carbon.Tokyo) // Wed, Aug 5, 2020 2:14 PM
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RSS 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +0800
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRssString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 W3C 格式字符串
|
2020-10-07 17:15:46 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToW3cString(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 ISO8601 格式字符串
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToIso8601String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC822 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc822String(carbon.Tokyo) // 05 Aug 20 14:14 JST
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC822Z 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString() // 05 Aug 20 13:14 +0800
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString(carbon.Tokyo) // 05 Aug 20 14:14 +0900
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC850 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc850String() // Wednesday, 05-Aug-20 13:14:15 CST
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc850String(carbon.Tokyo) // Wednesday, 05-Aug-20 14:14:15 JST
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC1036 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String() // Wed, 05 Aug 20 13:14:15 +0800
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String(carbon.Tokyo) // Wed, 05 Aug 20 14:14:15 +0900
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC1123 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String() // Wed, 05 Aug 2020 13:14:15 CST
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 JST
|
2021-07-28 09:18:51 +08:00
|
|
|
|
// 输出 RFC1123Z 格式字符串
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString() // Wed, 05 Aug 2020 13:14:15 +0800
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 0800
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC2822 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String() // Wed, 05 Aug 2020 13:14:15 +0800
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC3339 格式字符串
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc3339String() // 2020-08-05T13:14:15+08:00
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc3339String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出 RFC7231 格式字符串
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String() // Wed, 05 Aug 2020 13:14:15 GMT
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 GMT
|
2021-02-01 21:29:21 +08:00
|
|
|
|
|
2021-07-28 09:18:51 +08:00
|
|
|
|
// 输出日期时间字符串
|
|
|
|
|
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15
|
|
|
|
|
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15", carbon.Tokyo)) // 2020-08-05 13:14:15
|
|
|
|
|
|
|
|
|
|
// 输出"2006-01-02 15:04:05.999999999 -0700 MST"格式字符串
|
2021-04-06 15:00:40 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").ToString(carbon.Tokyo) // 2020-08-05 14:14:15 +0900 JST
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出指定布局的字符串,Layout()是ToLayoutString()的简写
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒
|
2021-07-24 12:39:07 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Layout("2006-01-02 15:04:05", carbon.Tokyo) // 2020-08-05 14:14:15
|
2021-07-23 11:05:33 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出指定格式的字符串,Format()是ToFormatString()的简写(如果使用的字母与格式化字符冲突时,请使用\符号转义该字符)
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Format("YmdHis") // 20200805131415
|
2021-07-23 11:05:33 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Format("Y年m月d日 H时i分s秒") // 2020年08月05日 13时14分15秒
|
2021-02-01 21:29:21 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Format("l jS \\o\\f F Y h:i:s A") // Wednesday 5th of August 2020 01:14:15 PM
|
2021-07-24 12:39:07 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Format("\\I\\t \\i\\s Y-m-d H:i:s") // It is 2020-08-05 13:14:15
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Format("Y-m-d H:i:s", carbon.Tokyo) // 2020-08-05 14:14:15
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
|
|
|
|
> 更多格式化输出符号请查看附录 <a href="#format-sign-table">格式化符号表</a>
|
2021-02-18 14:32:31 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 星座
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取星座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Constellation() // Leo
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是白羊座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsAries() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是金牛座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsTaurus() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是双子座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsGemini() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是巨蟹座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsCancer() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是狮子座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsLeo() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是处女座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsVirgo() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是天秤座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsLibra() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是天蝎座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsScorpio() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是射手座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsSagittarius() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是摩羯座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsCapricorn() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是水瓶座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsAquarius() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是双鱼座
|
2021-02-23 09:32:55 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsPisces() // false
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 季节
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
> 按照气象划分,即3-5月为春季,6-8月为夏季,9-11月为秋季,12-2月为冬季
|
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取季节
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").Season() // Summer
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本季节开始时间
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").StartOfSeason().ToDateTimeString() // 2020-06-01 00:00:00
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 本季节结束时间
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").EndOfSeason().ToDateTimeString() // 2020-08-31 23:59:59
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是春季
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsSpring() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是夏季
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsSummer() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是秋季
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsAutumn() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是冬季
|
2021-07-12 16:20:47 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-08-16 09:34:15 +08:00
|
|
|
|
##### 农历
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
> 目前仅支持公元`1900`年至`2100`年的`200`年时间跨度
|
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```go
|
2021-08-05 19:36:51 +08:00
|
|
|
|
// 获取农历生肖
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().Animal() // 鼠
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历节日
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2021-02-12 13:14:15", carbon.PRC).Lunar().Festival() // 春节
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历年年份
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().Year() // 2020
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历月月份
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().Month() // 6
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历闰月月份
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().LeapMonth() // 4
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历日日期
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().Day() // 16
|
|
|
|
|
// 获取农历 YYYY-MM-DD 格式字符串
|
|
|
|
|
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar()) // 2020-06-16
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历年字符串
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().ToYearString() // 二零二零
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 获取农历月字符串
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().ToMonthString() // 六
|
|
|
|
|
// 获取农历天字符串
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().ToDayString() // 十六
|
|
|
|
|
// 获取农历日期字符串
|
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().ToDateString() // 二零二零年六月十六
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是农历闰年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsLeapYear() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是农历闰月
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsLeapMonth() // false
|
2020-10-07 17:15:46 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是鼠年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsRatYear() // true
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是牛年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsOxYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是虎年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsTigerYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是兔年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsRabbitYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是龙年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsDragonYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是蛇年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsSnakeYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是马年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsHorseYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是羊年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsGoatYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是猴年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsMonkeyYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是鸡年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsRoosterYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是狗年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsDogYear() // false
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 是否是猪年
|
2021-08-05 19:36:51 +08:00
|
|
|
|
carbon.Parse("2020-08-05 13:14:15", carbon.PRC).Lunar().IsPigYear() // false
|
2020-10-07 17:15:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### JSON 支持
|
2021-07-19 09:55:54 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 定义模型
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-19 09:55:54 +08:00
|
|
|
|
```go
|
|
|
|
|
type Person struct {
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Age int `json:"age"`
|
2021-08-16 09:34:15 +08:00
|
|
|
|
Birthday carbon.DateTime `json:"birthday"`
|
|
|
|
|
GraduatedAt carbon.Date `json:"graduated_at"`
|
|
|
|
|
CreatedAt carbon.Time `json:"created_at"`
|
|
|
|
|
UpdatedAt carbon.Timestamp `json:"updated_at"`
|
|
|
|
|
DateTime1 carbon.TimestampWithSecond `json:"date_time1"`
|
|
|
|
|
DateTime2 carbon.TimestampWithMillisecond `json:"date_time2"`
|
|
|
|
|
DateTime3 carbon.TimestampWithMicrosecond `json:"date_time3"`
|
|
|
|
|
DateTime4 carbon.TimestampWithNanosecond `json:"date_time4"`
|
2021-07-19 09:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 实例化模型
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-19 09:55:54 +08:00
|
|
|
|
```go
|
|
|
|
|
person := Person {
|
2021-08-09 16:47:20 +08:00
|
|
|
|
ID: 1,
|
|
|
|
|
Name: "gouguoyin",
|
|
|
|
|
Age: 18,
|
2021-08-16 09:34:15 +08:00
|
|
|
|
Birthday: carbon.DateTime{carbon.Now().SubYears(18)},
|
|
|
|
|
GraduatedAt: carbon.Date{carbon.Parse("2020-08-05 13:14:15")},
|
|
|
|
|
CreatedAt: carbon.Time{carbon.Parse("2021-08-05 13:14:15")},
|
|
|
|
|
UpdatedAt: carbon.Timestamp{carbon.Parse("2022-08-05 13:14:15")},
|
|
|
|
|
DateTime1: carbon.TimestampWithSecond{carbon.Parse("2023-08-05 13:14:15")},
|
|
|
|
|
DateTime2: carbon.TimestampWithMillisecond{carbon.Parse("2024-08-05 13:14:15")},
|
|
|
|
|
DateTime3: carbon.TimestampWithMicrosecond{carbon.Parse("2025-08-05 13:14:15")},
|
|
|
|
|
DateTime4: carbon.TimestampWithNanosecond{carbon.Parse("2025-08-05 13:14:15")},
|
2021-07-19 09:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### JSON 编码
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-19 09:55:54 +08:00
|
|
|
|
```go
|
|
|
|
|
data, err := json.Marshal(&person)
|
|
|
|
|
if err != nil {
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 错误处理
|
|
|
|
|
log.Fatal(err)
|
2021-07-19 09:55:54 +08:00
|
|
|
|
}
|
2021-08-16 09:34:15 +08:00
|
|
|
|
fmt.Printf("%s", data)
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出
|
2021-07-19 09:55:54 +08:00
|
|
|
|
{
|
2021-08-16 09:34:15 +08:00
|
|
|
|
"id": 1,
|
|
|
|
|
"name": "gouguoyin",
|
|
|
|
|
"age": 18,
|
|
|
|
|
"birthday": "2003-07-16 16:22:02",
|
|
|
|
|
"graduated_at": "2020-08-05",
|
|
|
|
|
"created_at": "13:14:15",
|
|
|
|
|
"updated_at": 1659676455,
|
|
|
|
|
"date_time1": 1691212455,
|
|
|
|
|
"date_time2": 1722834855000,
|
|
|
|
|
"date_time3": 1754370855000000,
|
|
|
|
|
"date_time4": 1754370855000000000
|
2021-07-19 09:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### JSON 解码
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-19 09:55:54 +08:00
|
|
|
|
```go
|
2021-08-09 16:47:20 +08:00
|
|
|
|
jsonString := `{
|
2021-08-16 09:34:15 +08:00
|
|
|
|
"id": 1,
|
|
|
|
|
"name": "gouguoyin",
|
|
|
|
|
"age": 18,
|
|
|
|
|
"birthday": "2003-07-16 16:22:02",
|
|
|
|
|
"graduated_at": "2020-08-05",
|
|
|
|
|
"updated_at": 1659676455,
|
|
|
|
|
"date_time1": 1691212455,
|
|
|
|
|
"date_time2": 1722834855000,
|
|
|
|
|
"date_time3": 1754370855000000,
|
|
|
|
|
"date_time4": 1754370855000000000
|
2021-07-19 09:55:54 +08:00
|
|
|
|
}`
|
|
|
|
|
person := new(Person)
|
2021-08-09 16:47:20 +08:00
|
|
|
|
err := json.Unmarshal([]byte(jsonString), &person)
|
2021-07-19 09:55:54 +08:00
|
|
|
|
if err != nil {
|
2021-08-16 09:34:15 +08:00
|
|
|
|
// 错误处理
|
|
|
|
|
log.Fatal(err)
|
2021-07-19 09:55:54 +08:00
|
|
|
|
}
|
2021-08-16 09:34:15 +08:00
|
|
|
|
fmt.Printf("%v", *person)
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出
|
2021-08-09 16:47:20 +08:00
|
|
|
|
{ID:1 Name:gouguoyin Age:18 Birthday:2003-07-16 16:22:02 GraduatedAt:2020-08-05 00:00:00 UpdatedAt:2022-08-05 13:14:15 DTime1:2023-08-05 13:14:15 DateTime2:2024-08-05 13:14:15 DateTime3:2025-08-05 13:14:15 DateTime4:2025-08-05 13:14:15}
|
2021-07-19 09:55:54 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 国际化支持
|
|
|
|
|
|
|
|
|
|
目前支持的语言有
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-08-06 14:22:07 +08:00
|
|
|
|
* [简体中文(zh-CN)](./lang/zh-CN.json "简体中文")
|
|
|
|
|
* [繁体中文(zh-TW)](./lang/zh-TW.json "繁体中文")
|
2021-08-06 14:38:23 +08:00
|
|
|
|
* [英语(en)](./lang/en.json "英语")
|
2021-07-24 00:10:47 +08:00
|
|
|
|
* [日语(jp)](./lang/jp.json "日语")
|
|
|
|
|
* [韩语(kr)](./lang/kr.json "韩语")
|
2021-08-05 19:36:51 +08:00
|
|
|
|
* [西班牙语(es)](./lang/es.json "西班牙语"): 由 [hgisinger](https://github.com/hgisinger "hgisinger") 翻译
|
|
|
|
|
* [德语(de)](./lang/de.json "德语"): 由 [benzammour](https://github.com/benzammour "benzammour") 翻译
|
2021-08-09 16:47:20 +08:00
|
|
|
|
* [土耳其语(tr)](./lang/tr.json "土耳其语"): 由 [emresenyuva](https://github.com/emresenyuva "emresenyuva") 翻译
|
2021-08-16 09:34:15 +08:00
|
|
|
|
* [葡萄牙语(pt)](./lang/pt.json "葡萄牙语"): 由 [felipear89](https://github.com/felipear89 "felipear89") 翻译
|
2021-07-24 00:10:47 +08:00
|
|
|
|
|
|
|
|
|
目前支持的方法有
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-08-05 19:36:51 +08:00
|
|
|
|
* `Constellation()`:获取星座
|
|
|
|
|
* `Season()`:获取季节
|
|
|
|
|
* `DiffForHumans()`:获取对人类友好的可读格式时间差
|
2021-07-24 00:10:47 +08:00
|
|
|
|
* `ToMonthString()`:输出完整月份字符串
|
|
|
|
|
* `ToShortMonthString()`:输出缩写月份字符串
|
|
|
|
|
* `ToWeekString()`:输出完整星期字符串
|
|
|
|
|
* `ToShortWeekString()`:输出缩写星期字符串
|
|
|
|
|
|
|
|
|
|
###### 设置区域
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
```go
|
|
|
|
|
lang := NewLanguage()
|
2021-08-16 09:34:15 +08:00
|
|
|
|
if err := lang.SetLocale("zh-CN"); err != nil {
|
|
|
|
|
// 错误处理
|
2021-02-23 09:32:55 +08:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2021-07-08 09:49:00 +08:00
|
|
|
|
|
|
|
|
|
c := carbon.SetLanguage(lang)
|
|
|
|
|
c.Now().AddHours(1).DiffForHumans() // 1 小时后
|
|
|
|
|
c.Now().AddHours(1).ToMonthString() // 八月
|
|
|
|
|
c.Now().AddHours(1).ToShortMonthString() // 8月
|
|
|
|
|
c.Now().AddHours(1).ToWeekString() // 星期二
|
|
|
|
|
c.Now().AddHours(1).ToShortWeekString() // 周二
|
|
|
|
|
c.Now().AddHours(1).Constellation() // 狮子座
|
2021-07-12 16:20:47 +08:00
|
|
|
|
c.Now().AddHours(1).Season() // 夏季
|
2021-02-23 09:32:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 重写部分翻译资源(其余仍然按照指定的 `locale` 文件内容翻译)
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
```go
|
|
|
|
|
lang := NewLanguage()
|
|
|
|
|
|
2021-08-16 09:34:15 +08:00
|
|
|
|
if err := lang.SetLocale("en"); err != nil {
|
|
|
|
|
// 错误处理
|
2021-02-23 09:32:55 +08:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resources := map[string]string {
|
2021-08-16 09:34:15 +08:00
|
|
|
|
"hour": "%dh",
|
2021-02-23 09:32:55 +08:00
|
|
|
|
}
|
|
|
|
|
lang.SetResources(resources)
|
|
|
|
|
|
2021-07-08 09:49:00 +08:00
|
|
|
|
c := carbon.SetLanguage(lang)
|
|
|
|
|
c.Now().AddYears(1).DiffForHumans() // 1 year from now
|
|
|
|
|
c.Now().AddHours(1).DiffForHumans() // 1h from now
|
|
|
|
|
c.Now().ToMonthString() // August
|
|
|
|
|
c.Now().ToShortMonthString() // Aug
|
|
|
|
|
c.Now().ToWeekString() // Tuesday
|
|
|
|
|
c.Now().ToShortWeekString() // Tue
|
|
|
|
|
c.Now().Constellation() // Leo
|
2021-07-12 16:20:47 +08:00
|
|
|
|
c.Now().Season() // Summer
|
2021-02-23 09:32:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 重写全部翻译资源(无需指定 `locale`)
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
```go
|
|
|
|
|
lang := NewLanguage()
|
|
|
|
|
resources := map[string]string {
|
2021-08-16 09:34:15 +08:00
|
|
|
|
"months": "january|february|march|april|may|june|july|august|september|october|november|december",
|
|
|
|
|
"short_months": "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec",
|
|
|
|
|
"weeks": "sunday|monday|tuesday|wednesday|thursday|friday|saturday",
|
|
|
|
|
"short_weeks": "sun|mon|tue|wed|thu|fri|sat",
|
|
|
|
|
"seasons": "spring|summer|autumn|winter",
|
|
|
|
|
"constellations": "aries|taurus|gemini|cancer|leo|virgo|libra|scorpio|sagittarius|capricornus|aquarius|pisce",
|
|
|
|
|
"year": "1 yr|%d yrs",
|
|
|
|
|
"month": "1 mo|%d mos",
|
|
|
|
|
"week": "%dw",
|
|
|
|
|
"day": "%dd",
|
|
|
|
|
"hour": "%dh",
|
|
|
|
|
"minute": "%dm",
|
|
|
|
|
"second": "%ds",
|
2021-02-23 09:32:55 +08:00
|
|
|
|
"now": "just now",
|
2021-08-16 09:34:15 +08:00
|
|
|
|
"ago": "%s ago",
|
|
|
|
|
"from_now": "in %s",
|
|
|
|
|
"before": "%s before",
|
|
|
|
|
"after": "%s after",
|
2021-02-23 09:32:55 +08:00
|
|
|
|
}
|
|
|
|
|
lang.SetResources(resources)
|
|
|
|
|
|
2021-07-08 09:49:00 +08:00
|
|
|
|
c := carbon.SetLanguage(lang)
|
|
|
|
|
c.Now().AddYears(1).DiffForHumans() // in 1 yr
|
|
|
|
|
c.Now().AddHours(1).DiffForHumans() // in 1h
|
2021-08-16 09:34:15 +08:00
|
|
|
|
c.Now().ToMonthString() // august
|
|
|
|
|
c.Now().ToShortMonthString() // aug
|
|
|
|
|
c.Now().ToWeekString() // tuesday
|
|
|
|
|
c.Now().ToShortWeekString() // tue
|
|
|
|
|
c.Now().Constellation() // leo
|
|
|
|
|
c.Now().Season() // summer
|
2021-07-12 16:20:47 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### 错误处理
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
> 如果有多个错误发生,只返回第一个错误,前一个错误排除后才返回下一个错误
|
2021-01-26 17:08:27 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 场景一
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
|
```go
|
2021-07-23 11:05:33 +08:00
|
|
|
|
c := carbon.SetTimezone(PRC).Parse("xxx")
|
2021-01-26 17:08:27 +08:00
|
|
|
|
if c.Error != nil {
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 错误处理...
|
2021-02-23 09:32:55 +08:00
|
|
|
|
log.Fatal(c.Error)
|
2021-01-26 17:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(c.ToDateTimeString())
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出
|
2021-08-09 16:47:20 +08:00
|
|
|
|
cannot parse "xxx" as carbon, please make sure the value is valid
|
2021-01-26 17:08:27 +08:00
|
|
|
|
```
|
2021-07-19 09:55:54 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 场景二
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
|
```go
|
2021-07-23 11:05:33 +08:00
|
|
|
|
c := carbon.SetTimezone("xxx").Parse("2020-08-05")
|
2021-01-26 17:08:27 +08:00
|
|
|
|
if c.Error != nil {
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 错误处理...
|
2021-02-23 09:32:55 +08:00
|
|
|
|
log.Fatal(c.Error)
|
2021-01-26 17:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(c.ToDateTimeString())
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出
|
2021-07-23 11:05:33 +08:00
|
|
|
|
invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for all valid timezones
|
2021-01-26 17:08:27 +08:00
|
|
|
|
```
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
###### 场景三
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
|
```go
|
2021-07-23 11:05:33 +08:00
|
|
|
|
c := carbon.SetTimezone("xxx").Parse("12345678")
|
2021-01-26 17:08:27 +08:00
|
|
|
|
if c.Error != nil {
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 错误处理...
|
2021-02-23 09:32:55 +08:00
|
|
|
|
log.Fatal(c.Error)
|
2021-01-26 17:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(c.ToDateTimeString())
|
2021-07-24 00:10:47 +08:00
|
|
|
|
// 输出
|
|
|
|
|
invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for all valid timezone
|
2021-01-26 17:08:27 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
#### 附录
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
##### <a id="format-sign-table">格式化符号表</a>
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2021-07-24 00:10:47 +08:00
|
|
|
|
| 符号 | 描述 | 长度 | 范围 | 示例 |
|
2021-01-29 09:42:51 +08:00
|
|
|
|
| :------------: | :------------: | :------------: | :------------: | :------------: |
|
2021-08-16 09:34:15 +08:00
|
|
|
|
| d | 月份中的第几天,有前导零 | 2 | 01-31 | 02 |
|
|
|
|
|
| D | 缩写单词表示的周几 | 3 | Mon-Sun | Mon |
|
|
|
|
|
| j | 月份中的第几天,没有前导零 | - |1-31 | 2 |
|
2021-07-24 00:10:47 +08:00
|
|
|
|
| S | 第几天的英文缩写后缀,一般和j配合使用 | 2 | st/nd/rd/th | th |
|
2021-08-16 09:34:15 +08:00
|
|
|
|
| l | 完整单词表示的周几 | - | Monday-Sunday | Monday |
|
|
|
|
|
| F | 完整单词表示的月份 | - | January-December | January |
|
|
|
|
|
| m | 数字表示的月份,有前导零 | 2 | 01-12 | 01 |
|
|
|
|
|
| M | 缩写单词表示的月份 | 3 | Jan-Dec | Jan |
|
|
|
|
|
| n | 数字表示的月份,没有前导零 | - | 1-12 | 1 |
|
|
|
|
|
| Y | 4 位数字完整表示的年份 | 4 | 0000-9999 | 2006 |
|
|
|
|
|
| y | 2 位数字表示的年份 | 2 | 00-99 | 06 |
|
2021-07-25 07:59:41 +08:00
|
|
|
|
| a | 小写的上午和下午标识 | 2 | am/pm | pm |
|
|
|
|
|
| A | 大写的上午和下午标识 | 2 | AM/PM | PM |
|
2021-08-16 09:34:15 +08:00
|
|
|
|
| g | 小时,12 小时格式 | - | 1-12 | 3 |
|
2021-07-25 07:59:41 +08:00
|
|
|
|
| G | 小时,24 小时格式 | - | 0-23 | 15 |
|
|
|
|
|
| h | 小时,12 小时格式 | 2 | 00-11 | 03 |
|
|
|
|
|
| H | 小时,24 小时格式 | 2 | 00-23 | 15 |
|
2021-08-16 09:34:15 +08:00
|
|
|
|
| i | 分钟 | 2 | 01-59 | 04 |
|
|
|
|
|
| s | 秒数 | 2 | 01-59 | 05 |
|
|
|
|
|
| c | ISO8601 格式日期 | - | - | 2006-01-02T15:04:05-07:00 |
|
|
|
|
|
| r | RFC2822 格式日期 | - | - | Mon, 02 Jan 2006 15:04:05 -0700 |
|
|
|
|
|
| O | 与格林威治时间相差的小时数 | - | - | -0700 |
|
|
|
|
|
| P | 与格林威治时间相差的小时数,小时和分钟之间有冒号分隔 | - | - | +07:00 |
|
|
|
|
|
| T | 时区缩写 | - | - | MST |
|
|
|
|
|
| W | ISO8601 格式数字表示的年份中的第几周 | - | 1-52 | 1 |
|
|
|
|
|
| N | ISO8601 格式数字表示的星期中的第几天 | 1 | 1-7 | 1 |
|
|
|
|
|
| L | 是否为闰年,如果是闰年为 1,否则为 0 | 1 | 0-1 | 0 |
|
2021-07-24 00:10:47 +08:00
|
|
|
|
| U | 秒级时间戳 | 10 | - | 1611818268 |
|
2021-07-25 07:59:41 +08:00
|
|
|
|
| u | 毫秒 | 3 | 000-999 | 999 |
|
2021-08-16 09:34:15 +08:00
|
|
|
|
| w | 数字表示的周几 | 1 | 0-6 | 1 |
|
|
|
|
|
| t | 月份中的总天数 | 2 | 28-31 | 31 |
|
|
|
|
|
| z | 年份中的第几天 | - | 0-365 | 2 |
|
2021-07-24 00:10:47 +08:00
|
|
|
|
| e | 当前位置 | - | - | America/New_York |
|
|
|
|
|
| Q | 当前季节 | 1 | 1-4 | 1 |
|
|
|
|
|
| C | 当前世纪数 | - | 0-99 | 21 |
|
|
|
|
|
|
|
|
|
|
#### 参考项目
|
2021-08-16 09:34:15 +08:00
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
|
* [briannesbitt/carbon](https://github.com/briannesbitt/Carbon)
|
2021-07-24 00:10:47 +08:00
|
|
|
|
* [jinzhu/now](https://github.com/jinzhu/now)
|
2021-01-29 09:42:51 +08:00
|
|
|
|
* [goframe/gtime](https://github.com/gogf/gf/tree/master/os/gtime)
|
2021-02-23 09:32:55 +08:00
|
|
|
|
* [arrow-py/arrow](https://github.com/arrow-py/arrow)
|
2021-07-12 16:20:47 +08:00
|
|
|
|
* [moment/moment](https://github.com/moment/moment)
|
2021-08-05 19:36:51 +08:00
|
|
|
|
* [iamkun/dayjs](https://github.com/iamkun/dayjs)
|