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)
2023-09-07 11:29:47 +08:00
[![Go Report Card ](https://goreportcard.com/badge/github.com/golang-module/carbon/v2 )](https://goreportcard.com/report/github.com/golang-module/carbon/v2)
2023-12-22 16:01:00 +08:00
[![Go Coverage ](https://codecov.io/gh/golang-module/carbon/branch/master/graph/badge.svg )](https://codecov.io/gh/golang-module/carbon)
2023-11-10 14:35:09 +08:00
[![Goproxy.cn ](https://goproxy.cn/stats/github.com/golang-module/carbon/badges/download-count.svg )](https://goproxy.cn)
2023-12-22 16:01:00 +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)
2023-12-22 17:45:14 +08:00
[![License ](https://img.shields.io/github/license/golang-module/carbon )](https://github.com/golang-module/carbon/blob/master/LICENSE)
2021-07-23 11:05:33 +08:00
2021-08-16 09:34:15 +08:00
English | [简体中文 ](README.cn.md ) | [日本語 ](README.jp.md )
2020-10-07 17:15:46 +08:00
2021-07-24 00:10:47 +08:00
#### Introduction
2021-08-16 09:34:15 +08:00
2023-11-06 20:17:40 +08:00
A simple, semantic and developer-friendly golang package for datetime, has been included
2023-01-07 21:58:06 +08:00
by [awesome-go ](https://github.com/avelino/awesome-go#date-and-time "awesome-go" )
2020-09-14 10:39:20 +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:16:34 +08:00
2022-05-05 01:06:12 +08:00
[gitee.com/golang-module/carbon ](https://gitee.com/golang-module/carbon "gitee.com/golang-module/carbon" )
2020-09-07 17:10:33 +08:00
2021-07-24 00:10:47 +08:00
#### Installation
2021-08-16 09:34:15 +08:00
2022-06-09 07:43:25 +08:00
##### Go version >= 1.16 (recommend)
2021-09-06 11:13:43 +08:00
2020-09-07 17:47:49 +08:00
```go
2021-07-24 00:10:47 +08:00
// By github
2022-05-07 09:16:36 +08:00
go get -u github.com/golang-module/carbon/v2
2020-10-07 17:15:46 +08:00
2023-08-30 09:30:37 +08:00
import "github.com/golang-module/carbon/v2"
2020-09-07 17:10:33 +08:00
2021-07-24 00:10:47 +08:00
// By gitee
2022-05-07 09:16:36 +08:00
go get -u gitee.com/golang-module/carbon/v2
2020-09-13 13:33:45 +08:00
2023-08-30 09:30:37 +08:00
import "gitee.com/golang-module/carbon/v2"
2020-09-10 09:24:02 +08:00
```
2022-06-09 07:43:25 +08:00
##### Go version < 1.16 (must)
2021-09-10 16:30:35 +08:00
```go
// By github
2022-05-07 09:16:36 +08:00
go get -u github.com/golang-module/carbon
2021-09-10 16:30:35 +08:00
2023-08-30 09:35:31 +08:00
import "github.com/golang-module/carbon"
2021-09-10 16:30:35 +08:00
// By gitee
2022-05-07 09:16:36 +08:00
go get -u gitee.com/golang-module/carbon
2021-09-10 16:30:35 +08:00
2023-08-30 09:35:31 +08:00
import "gitee.com/golang-module/carbon"
2021-09-10 16:30:35 +08:00
```
2023-08-30 09:35:31 +08:00
2023-10-27 11:16:23 +08:00
> Please refer to <a href="#faq">FAQ</a> for the difference between v1 and v2, v1 is no longer updated, only maintained.
2021-09-10 16:30:35 +08:00
2021-07-24 00:10:47 +08:00
#### Usage and example
2021-08-16 09:34:15 +08:00
2023-01-07 09:12:38 +08:00
> The default timezone is Local, assuming the current time is 2020-08-05 13:14:15.999999999 +0800 CST
2020-09-10 09:24:02 +08:00
2021-07-24 00:10:47 +08:00
##### Yesterday, today and tomorrow
2021-08-16 09:34:15 +08:00
2020-09-08 16:01:01 +08:00
```go
2021-07-31 13:08:43 +08:00
// Return datetime of today
2021-07-19 09:55:54 +08:00
fmt.Sprintf("%s", carbon.Now()) // 2020-08-05 13:14:15
2023-08-26 22:30:29 +08:00
carbon.Now().String() // 2020-08-05 13:14:15
2022-05-05 01:25:19 +08:00
carbon.Now().ToString() // 2020-08-05 13:14:15 +0800 CST
2020-10-07 17:15:46 +08:00
carbon.Now().ToDateTimeString() // 2020-08-05 13:14:15
2021-07-31 13:08:43 +08:00
// Return date of today
2020-10-07 17:15:46 +08:00
carbon.Now().ToDateString() // 2020-08-05
2021-07-31 13:08:43 +08:00
// Return time of today
2020-10-07 17:15:46 +08:00
carbon.Now().ToTimeString() // 13:14:15
2021-08-16 09:34:15 +08:00
// Return datetime of today in a given timezone
carbon.Now(Carbon.NewYork).ToDateTimeString() // 2020-08-05 14:14:15
2021-07-31 13:08:43 +08:00
// Return timestamp with second of today
2021-08-16 09:34:15 +08:00
carbon.Now().Timestamp() // 1596604455
2021-07-31 13:08:43 +08:00
// Return timestamp with millisecond of today
2023-01-07 09:12:38 +08:00
carbon.Now().TimestampMilli() // 1596604455999
2021-07-31 13:08:43 +08:00
// Return timestamp with microsecond of today
2023-01-07 09:12:38 +08:00
carbon.Now().TimestampMicro() // 1596604455999999
2021-07-31 13:08:43 +08:00
// Return timestamp with nanosecond of today
2023-01-07 09:12:38 +08:00
carbon.Now().TimestampNano() // 1596604455999999999
2020-11-06 09:28:10 +08:00
2022-05-24 07:04:44 +08:00
// Return datetime of yesterday
2021-07-19 09:55:54 +08:00
fmt.Sprintf("%s", carbon.Yesterday()) // 2020-08-04 13:14:15
2023-08-26 22:30:29 +08:00
carbon.Yesterday().String() // 2020-08-04 13:14:15
2022-05-05 01:25:19 +08:00
carbon.Yesterday().ToString() // 2020-08-04 13:14:15 +0800 CST
2020-10-07 17:15:46 +08:00
carbon.Yesterday().ToDateTimeString() // 2020-08-04 13:14:15
2021-07-31 13:08:43 +08:00
// Return date of yesterday
2020-10-07 17:15:46 +08:00
carbon.Yesterday().ToDateString() // 2020-08-04
2021-07-31 13:08:43 +08:00
// Return time of yesterday
2020-10-10 16:11:22 +08:00
carbon.Yesterday().ToTimeString() // 13:14:15
2021-07-31 13:08:43 +08:00
// Return datetime of yesterday on a given day
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-31 13:08:43 +08:00
// Return datetime of yesterday in a given timezone
2021-07-23 11:05:33 +08:00
carbon.Yesterday(Carbon.NewYork).ToDateTimeString() // 2020-08-04 14:14:15
2021-08-16 09:34:15 +08:00
// Return timestamp with second of yesterday
carbon.Yesterday().Timestamp() // 1596518055
// Return timestamp with millisecond of yesterday
2023-01-07 09:12:38 +08:00
carbon.Yesterday().TimestampMilli() // 1596518055999
2021-08-16 09:34:15 +08:00
// Return timestamp with microsecond of yesterday
2023-01-07 09:12:38 +08:00
carbon.Yesterday().TimestampMicro() // 1596518055999999
2021-08-16 09:34:15 +08:00
// Return timestamp with nanosecond of yesterday
2023-01-07 09:12:38 +08:00
carbon.Yesterday().TimestampNano() // 1596518055999999999
2020-11-06 09:28:10 +08:00
2021-07-31 13:08:43 +08:00
// Return datetime of tomorrow
2021-07-19 09:55:54 +08:00
fmt.Sprintf("%s", carbon.Tomorrow()) // 2020-08-06 13:14:15
2023-08-26 22:30:29 +08:00
carbon.Tomorrow().String() // 2020-08-06 13:14:15
2022-05-05 01:25:19 +08:00
carbon.Tomorrow().ToString() // 2020-08-06 13:14:15 +0800 CST
2020-10-07 17:15:46 +08:00
carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15
2021-07-31 13:08:43 +08:00
// Return date of tomorrow
2020-10-07 17:15:46 +08:00
carbon.Tomorrow().ToDateString() // 2020-08-06
2021-07-31 13:08:43 +08:00
// Return time of tomorrow
2020-10-10 16:11:22 +08:00
carbon.Tomorrow().ToTimeString() // 13:14:15
2021-07-31 13:08:43 +08:00
// Return datetime of tomorrow on a given day
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-31 13:08:43 +08:00
// Return datetime of tomorrow in a given timezone
2021-07-23 11:05:33 +08:00
carbon.Tomorrow(Carbon.NewYork).ToDateTimeString() // 2020-08-06 14:14:15
2021-08-16 09:34:15 +08:00
// Return timestamp with second of tomorrow
carbon.Tomorrow().Timestamp() // 1596690855
// Return timestamp with millisecond of tomorrow
2023-01-07 09:12:38 +08:00
carbon.Tomorrow().TimestampMilli() // 1596690855999
2021-08-16 09:34:15 +08:00
// Return timestamp with microsecond of tomorrow
2023-01-07 09:12:38 +08:00
carbon.Tomorrow().TimestampMicro() // 1596690855999999
2021-08-16 09:34:15 +08:00
// Return timestamp with nanosecond of tomorrow
2023-01-07 09:12:38 +08:00
carbon.Tomorrow().TimestampNano() // 1596690855999999999
2020-10-01 14:49:32 +08:00
```
2021-07-31 13:08:43 +08:00
##### Create a Carbon instance
2021-08-16 09:34:15 +08:00
2020-09-07 17:10:33 +08:00
```go
2021-07-31 13:08:43 +08:00
// Create a Carbon instance from a given timestamp with second
2022-04-12 17:40:58 +08:00
carbon.CreateFromTimestamp(-1).ToString() // 1970-01-01 07:59:59 +0800 CST
carbon.CreateFromTimestamp(0).ToString() // 1970-01-01 08:00:00 +0800 CST
carbon.CreateFromTimestamp(1).ToString() // 1970-01-01 08:00:01 +0800 CST
carbon.CreateFromTimestamp(1649735755).ToString() // 2022-04-12 11:55:55 +0800 CST
2021-07-31 13:08:43 +08:00
// Create a Carbon instance from a given timestamp with millisecond
2022-04-12 17:40:58 +08:00
carbon.CreateFromTimestampMilli(1649735755981).ToString() // 2022-04-12 11:55:55.981 +0800 CST
2021-07-31 13:08:43 +08:00
// Create a Carbon instance from a given timestamp with microsecond
2022-04-12 17:40:58 +08:00
carbon.CreateFromTimestampMicro(1649735755981566).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
2021-07-31 13:08:43 +08:00
// Create a Carbon instance from a given timestamp with nanosecond
2022-04-12 17:40:58 +08:00
carbon.CreateFromTimestampNano(1649735755981566000).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
2020-11-02 10:32:46 +08:00
2022-05-04 23:55:39 +08:00
// Create a Carbon instance from a given date and time
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15).ToString() // 2020-08-05 13:14:15 +0800 CST
// Create a Carbon instance from a given date and time with millisecond
carbon.CreateFromDateTimeMilli(2020, 8, 5, 13, 14, 15, 999).ToString() // 2020-08-05 13:14:15.999 +0800 CST
// Create a Carbon instance from a given date and time with microsecond
carbon.CreateFromDateTimeMicro(2020, 8, 5, 13, 14, 15, 999999).ToString() // 2020-08-05 13:14:15.999999 +0800 CST
// Create a Carbon instance from a given date and time with nanosecond
carbon.CreateFromDateTimeNano(2020, 8, 5, 13, 14, 15, 999999999).ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2022-04-12 23:24:23 +08:00
2021-07-31 13:08:43 +08:00
// Create a Carbon instance from a given year, month and day
2023-12-26 22:00:05 +08:00
carbon.CreateFromDate(2020, 8, 5).ToString() // 2020-08-05 00:00:00 +0800 CST
2022-05-04 23:55:39 +08:00
// Create a Carbon instance from a given year, month and day with millisecond
2023-12-24 12:38:44 +08:00
carbon.CreateFromDateMilli(2020, 8, 5, 999).ToString() // 2020-08-05 00:00:00.999 +0800 CST
2022-05-04 23:55:39 +08:00
// Create a Carbon instance from a given year, month and day with microsecond
2023-12-24 12:38:44 +08:00
carbon.CreateFromDateMicro(2020, 8, 5, 999999).ToString() // 2020-08-05 00:00:00.999999 +0800 CST
2022-05-04 23:55:39 +08:00
// Create a Carbon instance from a given year, month and day with nanosecond
2023-12-24 12:38:44 +08:00
carbon.CreateFromDateNano(2020, 8, 5, 999999999).ToString() // 2020-08-05 00:00:00.999999999 +0800 CST
2022-05-04 23:55:39 +08:00
2021-07-31 13:08:43 +08:00
// Create a Carbon instance from a given hour, minute and second
2022-05-04 23:55:39 +08:00
carbon.CreateFromTime(13, 14, 15).ToString() // 2020-08-05 13:14:15 +0800 CST
// Create a Carbon instance from a given hour, minute and second with millisecond
carbon.CreateFromTimeMilli(13, 14, 15, 999).ToString() // 2020-08-05 13:14:15.999 +0800 CST
// Create a Carbon instance from a given hour, minute and second with microsecond
carbon.CreateFromTimeMicro(13, 14, 15, 999999).ToString() // 2020-08-05 13:14:15.999999 +0800 CST
// Create a Carbon instance from a given hour, minute and second with nanosecond
carbon.CreateFromTimeNano(13, 14, 15, 999999999).ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2020-09-07 17:10:33 +08:00
```
2022-11-02 15:48:04 +08:00
##### Parse a time string as a Carbon instance
2021-08-16 09:34:15 +08:00
2020-09-07 17:10:33 +08:00
```go
2021-07-24 00:10:47 +08:00
carbon.Parse("").ToDateTimeString() // empty string
2021-07-25 08:51:49 +08:00
carbon.Parse("0").ToDateTimeString() // empty string
2022-04-17 21:50:43 +08:00
carbon.Parse("00:00:00").ToDateTimeString() // empty string
2022-11-02 15:48:04 +08:00
carbon.Parse("0000-00-00").ToDateTimeString() // empty string
carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // empty string
2022-04-17 21:50:43 +08:00
2022-11-04 10:57:02 +08:00
carbon.Parse("now").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("yesterday").ToString() // 2020-08-04 13:14:15 +0800 CST
carbon.Parse("tomorrow").ToString() // 2020-08-06 13:14:15 +0800 CST
2022-10-27 00:38:32 +08:00
carbon.Parse("2020").ToString() // 2020-01-01 00:00:00 +0800 CST
carbon.Parse("2020-8").ToString() // 2020-08-01 00:00:00 +0800 CST
carbon.Parse("2020-08").ToString() // 2020-08-01 00:00:00 +0800 CST
carbon.Parse("2020-8-5").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("2020-8-05").ToString() // 2020-08-05 00:00:00 +0800 CST
2022-04-17 22:45:10 +08:00
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
2022-11-02 15:48:04 +08:00
carbon.Parse("2020-08-05.999").ToString() // 2020-08-05 00:00:00.999 +0800 CST
carbon.Parse("2020-08-05.999999").ToString() // 2020-08-05 00:00:00.999999 +0800 CST
carbon.Parse("2020-08-05.999999999").ToString() // 2020-08-05 00:00:00.999999999 +0800 CST
2022-10-27 00:38:32 +08:00
carbon.Parse("2020-8-5 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-8-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-08-5 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
2022-04-17 21:50:43 +08:00
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2022-04-17 23:47:50 +08:00
2022-10-27 00:38:32 +08:00
carbon.Parse("2020-8-5T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-8-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +0800 CST
2022-04-17 21:50:43 +08:00
carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +0800 CST
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2022-04-17 23:47:50 +08:00
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
2022-04-17 21:50:43 +08:00
carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST
2022-04-17 22:45:10 +08:00
carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2022-10-29 14:32:37 +08:00
carbon.Parse("20200805131415.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("20200805131415.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("20200805131415.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2020-09-07 17:10:33 +08:00
```
2022-10-27 00:38:32 +08:00
##### Parse a time string as a Carbon instance by format
2021-08-16 09:34:15 +08:00
2020-10-01 14:54:04 +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-02-01 21:29:21 +08:00
```
2022-10-27 00:38:32 +08:00
##### Parse a time string as a Carbon instance by layout
2021-08-16 09:34:15 +08:00
2021-02-01 21:29:21 +08:00
```go
2021-04-27 16:01:19 +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
```
2022-04-12 17:40:58 +08:00
##### Convert between Carbon and 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
// Convert Time.time into Carbon
2023-08-22 12:54:27 +08:00
carbon.CreateFromStdTime(time.Now())
2021-07-31 13:08:43 +08:00
// Convert Carbon into Time.time
2023-01-07 21:58:06 +08:00
carbon.Now().ToStdTime()
2020-10-01 14:54:04 +08:00
```
2023-12-26 22:00:05 +08:00
##### Boundary
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
// Start of the century
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
// End of the century
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
// Start of the decade
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
// End of the decade
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
// Start of the year
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
// End of the year
2020-10-22 09:34:10 +08:00
carbon.Parse("2020-08-05 13:14:15").EndOfYear().ToDateTimeString() // 2020-12-31 23:59:59
2021-07-24 00:10:47 +08:00
// Start of the quarter
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
// End of the quarter
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
// Start of the month
2022-08-01 12:24:44 +08:00
carbon.Parse("2020-08-05 13:14:15").StartOfMonth().ToDateTimeString() // 2020-08-01 00:00:00
2021-07-24 00:10:47 +08:00
// End of the month
2020-10-22 09:34:10 +08:00
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToDateTimeString() // 2020-08-31 23:59:59
2020-10-07 17:15:46 +08:00
2021-07-24 00:10:47 +08:00
// Start of the week
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
// End of the week
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
// Start of the day
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
// End of the day
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
// Start of the hour
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
// End of the hour
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
// Start of the minute
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
// End of the minute
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
// Start of the second
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").StartOfSecond().ToString() // 2020-08-05 13:14:15 +0800 CST
2021-07-24 00:10:47 +08:00
// End of the second
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").EndOfSecond().ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
2020-10-01 14:35:56 +08:00
```
2023-12-26 22:00:05 +08:00
##### Traveler
2021-08-16 09:34:15 +08:00
2020-09-07 17:10:33 +08:00
```go
2021-07-24 00:10:47 +08:00
// Add three centuries
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
// Add three centuries without overflowing month
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-07-24 00:10:47 +08:00
// Add one century
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
// Add one century without overflowing month
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-07-24 00:10:47 +08:00
// Subtract three centuries
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
// Subtract three centuries without overflowing month
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-07-24 00:10:47 +08:00
// Subtract one century
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-08-05 19:36:51 +08:00
// Subtract one century without overflowing month
2021-07-24 00:10:47 +08:00
carbon.Parse("2020-02-29 13:14:15").SubCenturyNoOverflow().ToDateTimeString() // 1920-02-20 13:14:15
2021-01-26 17:08:27 +08:00
2021-08-05 19:36:51 +08:00
// Add three decades
2022-04-14 11:32:46 +08:00
carbon.Parse("2020-02-29 13:14:15").AddDecades(3).ToDateTimeString() // 2050-03-01 13:14:15
2021-08-05 19:36:51 +08:00
// Add three decades without overflowing month
carbon.Parse("2020-02-29 13:14:15").AddDecadesNoOverflow(3).ToDateTimeString() // 2050-02-28 13:14:15
// Add one decade
carbon.Parse("2020-02-29 13:14:15").AddDecade().ToDateTimeString() // 2030-03-01 13:14:15
// Add one decade without overflowing month
carbon.Parse("2020-02-29 13:14:15").AddDecadeNoOverflow().ToDateTimeString() // 2030-02-28 13:14:15
// Subtract three decades
carbon.Parse("2020-02-29 13:14:15").SubDecades(3).ToDateTimeString() // 1990-03-01 13:14:15
// Subtract three decades without overflowing month
carbon.Parse("2020-02-29 13:14:15").SubDecadesNoOverflow(3).ToDateTimeString() // 1990-02-28 13:14:15
// Subtract one decade
carbon.Parse("2020-02-29 13:14:15").SubDecade().ToDateTimeString() // 2010-03-01 13:14:15
// Subtract one decade without overflowing month
carbon.Parse("2020-02-29 13:14:15").SubDecadeNoOverflow().ToDateTimeString() // 2010-02-28 13:14:15
2021-07-24 00:10:47 +08:00
// Add three years
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-08-05 19:36:51 +08:00
// Add three years without overflowing month
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
// Add one year
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-08-05 19:36:51 +08:00
// Add one year without overflowing month
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
// Subtract three years
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-08-05 19:36:51 +08:00
// Subtract three years without overflowing month
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
// Subtract one year
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-08-05 19:36:51 +08:00
// Subtract one year without overflowing month
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-09-08 12:18:07 +08:00
2021-07-24 00:10:47 +08:00
// Add three quarters
2020-11-02 10:32:46 +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
// Add three quarters without overflowing month
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-07-24 00:10:47 +08:00
// Add one quarter
2020-11-02 10:32:46 +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
// Add one quarter without overflowing month
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-07-24 00:10:47 +08:00
// Subtract three quarters
2020-11-02 10:32:46 +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
// Subtract three quarters without overflowing month
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-07-24 00:10:47 +08:00
// Subtract one quarter
2020-11-02 10:32:46 +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
// Subtract one quarter without overflowing month
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:32:46 +08:00
2021-07-24 00:10:47 +08:00
// Add three months
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
// Add three months without overflowing month
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-07-24 00:10:47 +08:00
// Add one month
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
// Add one month without overflowing month
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-07-24 00:10:47 +08:00
// Subtract three months
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
// Subtract three months without overflowing month
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-07-24 00:10:47 +08:00
// Subtract one month
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
// Subtract one month without overflowing month
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-09-12 08:53:59 +08:00
2021-07-24 00:10:47 +08:00
// Add three weeks
2020-11-02 10:32:46 +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
// Add one week
2020-11-02 10:32:46 +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
// Subtract three weeks
2020-11-02 10:32:46 +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
// Subtract three week
2020-11-02 10:32:46 +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
// Add three days
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
// Add one day
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
// Subtract three days
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
// Subtract one day
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").SubDay().ToDateTimeString() // 2020-08-04 13:14:15
2020-09-08 12:18:07 +08:00
2021-07-24 00:10:47 +08:00
// Add three hours
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
// Add two and a half hours
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
// Add one hour
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
// Subtract three hours
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
// Subtract two and a half hours
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
// Subtract one hour
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 12:14:15
2020-09-08 12:18:07 +08:00
2021-07-24 00:10:47 +08:00
// Add three minutes
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
// Add two and a half minutes
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
// Add one minute
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
// Subtract three minutes
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
// Subtract two and a half minutes
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
// Subtract one minute
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").SubMinute().ToDateTimeString() // 2020-08-05 13:13:15
2020-09-08 12:18:07 +08:00
2021-07-24 00:10:47 +08:00
// Add three seconds
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
// Add two and a half seconds
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
// Add one second
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
// Subtract three seconds
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
// Subtract two and a half seconds
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
// Subtract one second
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
2022-05-01 09:09:59 +08:00
// Add three milliseconds
carbon.Parse("2020-08-05 13:14:15.222222222").AddMilliseconds(3).ToString() // 2020-08-05 13:14:15.225222222 +0800 CST
// Add one millisecond
carbon.Parse("2020-08-05 13:14:15.222222222").AddMillisecond().ToString() // 2020-08-05 13:14:15.223222222 +0800 CST
// Subtract three milliseconds
carbon.Parse("2020-08-05 13:14:15.222222222").SubMilliseconds(3).ToString() // 2020-08-05 13:14:15.219222222 +0800 CST
// Subtract one millisecond
carbon.Parse("2020-08-05 13:14:15.222222222").SubMillisecond().ToString() // 2020-08-05 13:14:15.221222222 +0800 CST
// Add three microseconds
carbon.Parse("2020-08-05 13:14:15.222222222").AddMicroseconds(3).ToString() // 2020-08-05 13:14:15.222225222 +0800 CST
// Add one microsecond
carbon.Parse("2020-08-05 13:14:15.222222222").AddMicrosecond().ToString() // 2020-08-05 13:14:15.222223222 +0800 CST
// Subtract three microseconds
carbon.Parse("2020-08-05 13:14:15.222222222").SubMicroseconds(3).ToString() // 2020-08-05 13:14:15.222219222 +0800 CST
// Subtract one microsecond
carbon.Parse("2020-08-05 13:14:15.222222222").SubMicrosecond().ToString() // 2020-08-05 13:14:15.222221222 +0800 CST
// Add three nanoseconds
carbon.Parse("2020-08-05 13:14:15.222222222").AddNanoseconds(3).ToString() // 2020-08-05 13:14:15.222222225 +0800 CST
// Add one nanosecond
carbon.Parse("2020-08-05 13:14:15.222222222").AddNanossecond().ToString() // 2020-08-05 13:14:15.222222223 +0800 CST
// Subtract three nanoseconds
carbon.Parse("2020-08-05 13:14:15.222222222").SubNanosseconds(3).ToString() // 2020-08-05 13:14:15.222222219 +0800 CST
// Subtract one nanosecond
carbon.Parse("2020-08-05 13:14:15.222222222").SubNanossecond().ToString() // 2020-08-05 13:14:15.222222221 +0800 CST
2020-10-22 09:34:10 +08:00
```
2020-10-07 17:15:46 +08:00
2021-07-24 00:10:47 +08:00
##### Difference
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
// Difference in years
carbon.Parse("2021-08-05 13:14:15").DiffInYears(carbon.Parse("2020-08-05 13:14:15")) // -1
// Difference in years with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2021-08-05 13:14:15").DiffAbsInYears(carbon.Parse("2020-08-05 13:14:15")) // 1
2021-08-06 00:16:08 +08:00
// Difference in months
carbon.Parse("2020-08-05 13:14:15").DiffInMonths(carbon.Parse("2020-07-05 13:14:15")) // -1
// Difference in months with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").DiffAbsInMonths(carbon.Parse("2020-07-05 13:14:15")) // 1
2021-08-06 00:16:08 +08:00
2021-07-24 00:10:47 +08:00
// Difference in weeks
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
// Difference in weeks with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").DiffAbsInWeeks(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
// Difference in days
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
// Difference in days with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").DiffAbsInDays(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
// Difference in hours
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
// Difference in hours with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").DiffAbsInHours(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
// Difference in minutes
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
// Difference in minutes with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").DiffAbsInMinutes(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
// Difference in seconds
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
// Difference in seconds with absolute value
2022-04-17 19:52:02 +08:00
carbon.Parse("2020-08-05 13:14:15").DiffAbsInSeconds(carbon.Parse("2020-08-05 13:14:14")) // 1
2021-02-08 09:47:14 +08:00
2021-09-10 14:06:53 +08:00
// Difference in string
carbon.Now().DiffInString() // just now
carbon.Now().AddYearsNoOverflow(1).DiffInString() // -1 year
carbon.Now().SubYearsNoOverflow(1).DiffInString() // 1 year
// Difference in string with absolute value
2022-04-17 19:52:02 +08:00
carbon.Now().DiffAbsInString(carbon.Now()) // just now
carbon.Now().AddYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year
carbon.Now().SubYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year
2021-09-10 14:06:53 +08:00
2021-09-16 11:45:24 +08:00
// Difference in a human-readable format
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-09-16 11:45:24 +08:00
// Difference in a human-readable format from now time
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
```
2023-12-26 22:00:05 +08:00
##### Extremum
```go
c := carbon.Parse("2023-04-01")
c0 := carbon.Parse("xxx")
c1 := carbon.Parse("2023-03-28")
c2 := carbon.Parse("2023-04-16")
// Return the closest Carbon instance
c.Closest(c0, c1) // c1
c.Closest(c0, c2) // c2
c.Closest(c1, c2) // c1
// Return the farthest Carbon instance
c.Farthest(c0, c1) // c1
c.Farthest(c0, c2) // c2
c.Farthest(c1, c2) // c2
```
2021-07-24 00:10:47 +08:00
##### Comparison
2021-08-16 09:34:15 +08:00
2020-11-06 09:28:10 +08:00
```go
2023-12-26 22:00:05 +08:00
// Whether is daylight saving time
2023-11-06 20:17:40 +08:00
carbon.Parse("").IsDST() // false
carbon.Parse("0").IsDST() // false
carbon.Parse("0000-00-00 00:00:00").IsDST() // false
carbon.Parse("0000-00-00").IsDST() // false
carbon.Parse("00:00:00").IsDST() // false
carbon.Parse("2023-01-01", "Australia/Brisbane").IsDST() // false
carbon.Parse("2023-01-01", "Australia/Sydney").IsDST() // true
2021-07-24 00:10:47 +08:00
// Whether is zero time
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
2022-04-21 15:08:24 +08:00
// Whether is valid time
carbon.Parse("").IsValid() // false
carbon.Parse("0").IsValid() // false
carbon.Parse("0000-00-00 00:00:00").IsValid() // false
carbon.Parse("0000-00-00").IsValid() // false
carbon.Parse("00:00:00").IsValid() // false
carbon.Parse("2020-08-05 00:00:00").IsValid() // true
carbon.Parse("2020-08-05").IsValid() // true
carbon.Parse("2020-08-05").SetTimezone("xxx").IsValid() // false
2021-07-24 00:10:47 +08:00
// Whether is invalid time
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
2020-11-06 09:28:10 +08:00
2021-07-24 00:10:47 +08:00
// Whether is now time
2020-11-06 09:28:10 +08:00
carbon.Now().IsNow() // true
2021-07-24 00:10:47 +08:00
// Whether is future time
2020-11-06 09:28:10 +08:00
carbon.Tomorrow().IsFuture() // true
2021-07-24 00:10:47 +08:00
// Whether is pass time
2020-11-06 09:28:10 +08:00
carbon.Yesterday().IsPast() // true
2021-07-31 13:08:43 +08:00
// Whether is a leap year
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsLeapYear() // true
2021-07-31 13:08:43 +08:00
// Whether is a long year
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsLongYear() // true
2022-05-24 07:04:44 +08:00
// Whether is January
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
2021-07-31 13:08:43 +08:00
// Whether is February
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
2021-07-31 13:08:43 +08:00
// Whether is March
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
2021-07-31 13:08:43 +08:00
// Whether is April
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
2021-07-31 13:08:43 +08:00
// Whether is May
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
2021-07-31 13:08:43 +08:00
// Whether is June
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
2021-07-31 13:08:43 +08:00
// Whether is July
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
2021-07-31 13:08:43 +08:00
// Whether is August
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
2021-07-31 13:08:43 +08:00
// Whether is September
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
2021-07-31 13:08:43 +08:00
// Whether is October
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
2021-07-31 13:08:43 +08:00
// Whether is November
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
2021-07-31 13:08:43 +08:00
// Whether is December
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
2021-07-31 13:08:43 +08:00
// Whether is Monday
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
2021-07-31 13:08:43 +08:00
// Whether is Tuesday
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
2021-07-31 13:08:43 +08:00
// Whether is Wednesday
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
2021-07-31 13:08:43 +08:00
// Whether is Thursday
2021-08-16 09:34:15 +08:00
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
2021-07-31 13:08:43 +08:00
// Whether is Friday
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
2021-07-31 13:08:43 +08:00
// Whether is Saturday
2020-11-06 09:28:10 +08:00
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
2021-07-31 13:08:43 +08:00
// Whether is Sunday
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
// Whether is weekday
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
// Whether is weekend
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
// Whether is yesterday
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
// Whether is today
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
// Whether is tomorrow
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
2022-05-20 00:02:30 +08:00
// Whether is same century
carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("3020-08-05 13:14:15")) // false
carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("2099-08-05 13:14:15")) // true
// Whether is same decade
carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2030-08-05 13:14:15")) // false
carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2120-08-05 13:14:15")) // true
// Whether is same year
carbon.Parse("2020-08-05 00:00:00").IsSameYear(carbon.Parse("2021-08-05 13:14:15")) // false
carbon.Parse("2020-01-01 00:00:00").IsSameYear(carbon.Parse("2020-12-31 13:14:15")) // true
// Whether is same quarter
carbon.Parse("2020-08-05 00:00:00").IsSameQuarter(carbon.Parse("2020-09-05 13:14:15")) // false
carbon.Parse("2020-01-01 00:00:00").IsSameQuarter(carbon.Parse("2021-01-31 13:14:15")) // true
// Whether is same month
carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2021-01-31 13:14:15")) // false
carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2020-01-31 13:14:15")) // true
// Whether is same day
carbon.Parse("2020-08-05 13:14:15").IsSameDay(carbon.Parse("2021-08-05 13:14:15")) // false
carbon.Parse("2020-08-05 00:00:00").IsSameDay(carbon.Parse("2020-08-05 13:14:15")) // true
// Whether is same hour
carbon.Parse("2020-08-05 13:14:15").IsSameHour(carbon.Parse("2021-08-05 13:14:15")) // false
carbon.Parse("2020-08-05 13:00:00").IsSameHour(carbon.Parse("2020-08-05 13:14:15")) // true
// Whether is same minute
carbon.Parse("2020-08-05 13:14:15").IsSameMinute(carbon.Parse("2021-08-05 13:14:15")) // false
carbon.Parse("2020-08-05 13:14:00").IsSameMinute(carbon.Parse("2020-08-05 13:14:15")) // true
// Whether is same second
carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2021-08-05 13:14:15")) // false
carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2020-08-05 13:14:15")) // true
2021-07-24 00:10:47 +08:00
// Whether greater than
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
// Whether less than
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
// Whether equal
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-26 10:12:23 +08:00
// Whether not equal
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
// Whether greater than or equal
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
// Whether less than or equal
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-31 13:08:43 +08:00
// Whether between two Carbon instances, excluded the start and end Carbon instance
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-31 13:08:43 +08:00
// Whether between two Carbon instances, included the start Carbon instance
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-31 13:08:43 +08:00
// Whether between two Carbon instances, included the end Carbon instance
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-31 13:08:43 +08:00
// Whether between two Carbon instances, included the start and end Carbon instance
2021-07-23 23:03:46 +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-08-09 16:47:20 +08:00
> For the definition of long year, please see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
##### Setter
2021-08-16 09:34:15 +08:00
2021-08-09 16:47:20 +08:00
```go
// Set timezone
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
2022-08-01 12:24:44 +08:00
// Set location
utc, _ := time.LoadLocation(carbon.UTC)
carbon.SetLocation(utc).Now().ToDateTimeString() // 2022-06-28 09:25:38
tokyo, _ := time.LoadLocation(carbon.Tokyo)
carbon.SetLocation(tokyo).Now().ToDateTimeString() // 2022-06-28 18:25:38
2021-08-09 16:47:20 +08:00
// Set locale
carbon.Parse("2020-07-05 13:14:15").SetLocale("en").DiffForHumans() // 1 month before
carbon.Parse("2020-07-05 13:14:15").SetLocale("zh-CN").DiffForHumans() // 1 月前
2022-05-07 09:16:36 +08:00
// Set year, month, day, hour, minute and second
2022-04-18 00:13:21 +08:00
carbon.Parse("2020-01-01").SetDateTime(2019, 2, 2, 13, 14, 15).ToString() // 2019-02-02 13:14:15 +0800 CST
carbon.Parse("2020-01-01").SetDateTime(2019, 2, 31, 13, 14, 15).ToString() // 2019-03-03 13:14:15 +0800 CST
2022-05-07 09:16:36 +08:00
// Set year, month, day, hour, minute, second and millisecond
2022-04-18 00:13:21 +08:00
carbon.Parse("2020-01-01").SetDateTimeMilli(2019, 2, 2, 13, 14, 15, 999).ToString() // 2019-02-02 13:14:15.999 +0800 CST
carbon.Parse("2020-01-01").SetDateTimeMilli(2019, 2, 31, 13, 14, 15, 999).ToString() // 2019-03-03 13:14:15.999 +0800 CST
2022-05-07 09:16:36 +08:00
// Set year, month, day, hour, minute, second and microsecond
2022-04-18 00:13:21 +08:00
carbon.Parse("2020-01-01").SetDateTimeMicro(2019, 2, 2, 13, 14, 15, 999999).ToString() // 2019-02-02 13:14:15.999999 +0800 CST
carbon.Parse("2020-01-01").SetDateTimeMicro(2019, 2, 31, 13, 14, 15, 999999).ToString() // 2019-03-03 13:14:15.999999 +0800 CST
2022-05-07 09:16:36 +08:00
// Set year, month, day, hour, minute, second and nanosecond
2022-04-18 00:13:21 +08:00
carbon.Parse("2020-01-01").SetDateTimeNano(2019, 2, 2, 13, 14, 15, 999999999).ToString() // 2019-02-02 13:14:15.999999999 +0800 CST
carbon.Parse("2020-01-01").SetDateTimeNano(2019, 2, 31, 13, 14, 15, 999999999).ToString() // 2019-03-03 13:14:15.999999999 +0800 CST
2022-04-12 17:40:58 +08:00
2022-05-07 09:16:36 +08:00
// Set year, month and day
carbon.Parse("2020-01-01").SetDate(2019, 2, 2).ToString() // 2019-02-02 00:00:00 +0800 CST
carbon.Parse("2020-01-01").SetDate(2019, 2, 31).ToString() // 2019-03-03 00:00:00 +0800 CST
// Set year, month, day and millisecond
carbon.Parse("2020-01-01").SetDateMilli(2019, 2, 2, 999).ToString() // 2019-02-02 00:00:00.999 +0800 CST
carbon.Parse("2020-01-01").SetDateMilli(2019, 2, 31, 999).ToString() // 2019-03-03 00:00:00.999 +0800 CST
// Set year, month, day and microsecond
carbon.Parse("2020-01-01").SetDateMicro(2019, 2, 2, 999999).ToString() // 2019-02-02 00:00:00.999999 +0800 CST
carbon.Parse("2020-01-01").SetDateMicro(2019, 2, 31, 999999).ToString() // 2019-03-03 00:00:00.999999 +0800 CST
// Set year, month, day and nanosecond
carbon.Parse("2020-01-01").SetDateNano(2019, 2, 2, 999999999).ToString() // 2019-02-02 00:00:00.999999999 +0800 CST
carbon.Parse("2020-01-01").SetDateNano(2019, 2, 31, 999999999).ToString() // 2019-03-03 00:00:00.999999999 +0800 CST
// Set hour, minute and second
carbon.Parse("2020-01-01").SetTime(13, 14, 15).ToString() // 2020-01-01 13:14:15 +0800 CST
carbon.Parse("2020-01-01").SetTime(13, 14, 90).ToString() // 2020-01-01 13:15:30 +0800 CST
// Set hour, minute, second and millisecond
carbon.Parse("2020-01-01").SetTimeMilli(13, 14, 15, 999).ToString() // 2020-01-01 13:14:15.999 +0800 CST
carbon.Parse("2020-01-01").SetTimeMilli(13, 14, 90, 999).ToString() // 2020-01-01 13:15:30.999 +0800 CST
// Set hour, minute, second and microsecond
carbon.Parse("2020-01-01").SetTimeMicro(13, 14, 15, 999999).ToString() // 2020-01-01 13:14:15.999999 +0800 CST
carbon.Parse("2020-01-01").SetTimeMicro(13, 14, 90, 999999).ToString() // 2020-01-01 13:15:30.999999 +0800 CST
// Set hour, minute, second and nanosecond
carbon.Parse("2020-01-01").SetTimeNano(13, 14, 15, 999999999).ToString() // 2020-01-01 13:14:15.999999999 +0800 CST
carbon.Parse("2020-01-01").SetTimeNano(13, 14, 90, 999999999).ToString() // 2020-01-01 13:15:30.999999999 +0800 CST
2022-04-12 17:40:58 +08:00
2021-08-09 16:47:20 +08:00
// Set year
carbon.Parse("2020-02-29").SetYear(2021).ToDateString() // 2021-03-01
// Set year without overflowing month
carbon.Parse("2020-02-29").SetYearNoOverflow(2021).ToDateString() // 2021-02-28
// Set month
carbon.Parse("2020-01-31").SetMonth(2).ToDateString() // 2020-03-02
// Set month without overflowing month
carbon.Parse("2020-01-31").SetMonthNoOverflow(2).ToDateString() // 2020-02-29
2021-09-16 11:45:24 +08:00
// Set start day of the week
2021-08-16 09:34:15 +08:00
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
// Set day
carbon.Parse("2019-08-05").SetDay(31).ToDateString() // 2020-08-31
carbon.Parse("2020-02-01").SetDay(31).ToDateString() // 2020-03-02
// Set hour
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
// Set minute
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
// Set second
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
2021-09-16 11:45:24 +08:00
// Set millisecond
2021-08-09 16:47:20 +08: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
2021-09-16 11:45:24 +08:00
// Set microsecond
2021-08-09 16:47:20 +08:00
carbon.Parse("2020-08-05 13:14:15").SetMicrosecond(100000).Microsecond() // 100000
carbon.Parse("2020-08-05 13:14:15").SetMicrosecond(999999).Microsecond() // 999999
2021-09-16 11:45:24 +08:00
// Set nanosecond
2021-08-09 16:47:20 +08:00
carbon.Parse("2020-08-05 13:14:15").SetNanosecond(100000000).Nanosecond() // 100000000
carbon.Parse("2020-08-05 13:14:15").SetNanosecond(999999999).Nanosecond() // 999999999
```
##### Getter
2021-08-16 09:34:15 +08:00
2021-08-09 16:47:20 +08:00
```go
// Get total days of the year
carbon.Parse("2019-08-05 13:14:15").DaysInYear() // 365
carbon.Parse("2020-08-05 13:14:15").DaysInYear() // 366
// Get total days of the month
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
// Get day of the year
carbon.Parse("2020-08-05 13:14:15").DayOfYear() // 218
// Get week of the year
carbon.Parse("2020-08-05 13:14:15").WeekOfYear() // 32
// Get day of the month
carbon.Parse("2020-08-05 13:14:15").DayOfMonth() // 5
// Get week of the month
carbon.Parse("2020-08-05 13:14:15").WeekOfMonth() // 1
// Get day of the week
carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
2022-04-12 17:40:58 +08:00
// Get current year, month, day, hour, minute and second
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15").DateTime() // 2020, 8, 5, 13, 14, 15
2022-04-14 11:32:46 +08:00
// Get current year, month, day, hour, minute, second and millisecond
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15").DateTimeMilli() // 2020, 8, 5, 13, 14, 15, 999
2022-04-14 11:32:46 +08:00
// Get current year, month, day, hour, minute, second and microsecond
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15").DateTimeMicro() // 2020, 8, 5, 13, 14, 15, 999999
2022-04-21 15:08:24 +08:00
// Get current year, month, day, hour, minute, second and nanosecond
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15").DateTimeNano() // 2020, 8, 5, 13, 14, 15, 999999999
2022-04-14 11:32:46 +08:00
2022-04-12 17:40:58 +08:00
// Get current year, month and day
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").Date() // 2020, 8, 5
2022-05-01 09:09:59 +08:00
// Get current year, month, day and millisecond
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").DateMilli() // 2020, 8, 5, 999
2022-05-01 09:09:59 +08:00
// Get current year, month, day and microsecond
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").DateMicro() // 2020, 8, 5, 999999
2022-05-01 09:09:59 +08:00
// Get current year, month, day and nanosecond
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").DateNano() // 2020, 8, 5, 999999999
2022-05-01 09:09:59 +08:00
2022-04-12 17:40:58 +08:00
// Get current hour, minute and second
2023-01-07 08:48:51 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").Time() // 13, 14, 15
// Get current hour, minute, second and millisecond
carbon.Parse("2020-08-05 13:14:15.999999999").TimeMilli() // 13, 14, 15, 999
// Get current hour, minute, second and microsecond
carbon.Parse("2020-08-05 13:14:15.999999999").TimeMicro() // 13, 14, 15, 999999
// Get current hour, minute, second and nanosecond
carbon.Parse("2020-08-05 13:14:15.999999999").TimeNano() // 13, 14, 15, 999999999
2022-04-12 17:40:58 +08:00
2021-08-09 16:47:20 +08:00
// Get current century
carbon.Parse("2020-08-05 13:14:15").Century() // 21
// Get current decade
carbon.Parse("2019-08-05 13:14:15").Decade() // 10
carbon.Parse("2021-08-05 13:14:15").Decade() // 20
// Get current year
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
// Get current quarter
carbon.Parse("2020-08-05 13:14:15").Quarter() // 3
// Get current month
carbon.Parse("2020-08-05 13:14:15").Month() // 8
2022-04-14 11:32:46 +08:00
// Get current week(start from 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
// Get current day
carbon.Parse("2020-08-05 13:14:15").Day() // 5
// Get current hour
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
// Get current minute
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
// Get current second
carbon.Parse("2020-08-05 13:14:15").Second() // 15
// Get current millisecond
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05 13:14:15.999").Millisecond() // 999
2021-08-09 16:47:20 +08:00
// Get current microsecond
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05 13:14:15.999").Microsecond() // 999000
2021-08-09 16:47:20 +08:00
// Get current nanosecond
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05 13:14:15.999").Nanosecond() // 999000000
2021-08-09 16:47:20 +08:00
2022-04-12 23:24:23 +08:00
// Get timestamp with second
2021-08-16 09:34:15 +08:00
carbon.Parse("2020-08-05 13:14:15").Timestamp() // 1596604455
// Get timestamp with millisecond
2022-04-12 23:24:23 +08:00
carbon.Parse("2020-08-05 13:14:15").TimestampMilli() // 1596604455000
2021-08-16 09:34:15 +08:00
// Get timestamp with microsecond
2022-04-12 23:24:23 +08:00
carbon.Parse("2020-08-05 13:14:15").TimestampMicro() // 1596604455000000
2021-08-16 09:34:15 +08:00
// Get timestamp with nanosecond
2022-04-12 23:24:23 +08:00
carbon.Parse("2020-08-05 13:14:15").TimestampNano() // 1596604455000000000
2021-08-16 09:34:15 +08:00
2021-08-09 16:47:20 +08:00
// Get timezone name
carbon.SetTimezone(carbon.PRC).Timezone() // CST
carbon.SetTimezone(carbon.Tokyo).Timezone() // JST
// Get location name
carbon.SetTimezone(carbon.PRC).Location() // PRC
carbon.SetTimezone(carbon.Tokyo).Location() // Asia/Tokyo
// Get offset seconds from the UTC timezone
carbon.SetTimezone(carbon.PRC).Offset() // 28800
carbon.SetTimezone(carbon.Tokyo).Offset() // 32400
// Get locale name
carbon.Now().SetLocale("en").Locale() // en
carbon.Now().SetLocale("zh-CN").Locale() // zh-CN
// Get constellation name
carbon.Now().Constellation() // Leo
carbon.Now().SetLocale("en").Constellation() // Leo
carbon.Now().SetLocale("zh-CN").Constellation() // 狮子座
2022-04-21 15:08:24 +08:00
// Get season name
2021-08-09 16:47:20 +08:00
carbon.Now().Season() // Summer
carbon.Now().SetLocale("en").Season() // Summer
carbon.Now().SetLocale("zh-CN").Season() // 夏季
// Get current age
carbon.Parse("2002-01-01 13:14:15").Age() // 17
carbon.Parse("2002-12-31 13:14:15").Age() // 18
```
2020-10-07 17:15:46 +08:00
2021-08-16 09:34:15 +08:00
##### Output
2020-10-07 17:15:46 +08:00
2021-08-16 09:34:15 +08:00
```go
2023-01-07 09:12:38 +08:00
// Output datetime format string
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
2023-01-07 09:12:38 +08:00
// Output datetime with millisecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString() // 2020-08-05 13:14:15.999
2023-01-07 09:12:38 +08:00
// Output datetime with microsecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString() // 2020-08-05 13:14:15.999999
2023-01-07 09:12:38 +08:00
// Output datetime with nanosecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString() // 2020-08-05 13:14:15.999999999
2023-01-07 09:12:38 +08:00
// Output short datetime format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString() // 20200805131415
2023-01-07 09:12:38 +08:00
// Output short datetime with millisecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString() // 20200805131415.999
2023-01-07 09:12:38 +08:00
// Output short datetime with microsecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString() // 20200805131415.999999
2023-01-07 09:12:38 +08:00
// Output short datetime with nanosecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString() // 20200805131415.999999999
2021-07-23 11:05:33 +08:00
2023-01-07 08:48:51 +08:00
// Output date format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToDateString() // 2020-08-05
2023-01-07 08:48:51 +08:00
// Output date with millisecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToDateMilliString() // 2020-08-05.999
2023-01-07 08:48:51 +08:00
// Output date with microsecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToDateMicroString() // 2020-08-05.999999
2023-01-07 08:48:51 +08:00
// Output date with nanosecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToDateNanoString() // 2020-08-05.999999999
2023-01-07 08:48:51 +08:00
// Output short date format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortDateString() // 20200805
2023-01-07 08:48:51 +08:00
// Output short date with millisecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortDateMilliString() // 20200805.999
2023-01-07 08:48:51 +08:00
// Output short date with microsecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortDateMicroString() // 20200805.999999
2023-01-07 08:48:51 +08:00
// Output short date with nanosecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortDateNanoString() // 20200805.999999999
2021-07-23 11:05:33 +08:00
2023-01-07 08:48:51 +08:00
// Output time format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToTimeString() // 13:14:15
2023-01-07 08:48:51 +08:00
// Output time with millisecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToTimeMilliString() // 13:14:15.999
2023-01-07 08:48:51 +08:00
// Output time with microsecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToTimeMicroString() // 13:14:15.999999
2023-01-07 08:48:51 +08:00
// Output time with nanosecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToTimeNanoString() // 13:14:15.999999999
2023-01-07 08:48:51 +08:00
// Output short time format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortTimeString() // 131415
2023-01-07 08:48:51 +08:00
// Output short time with millisecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortTimeMilliString() // 131415.999
2023-01-07 08:48:51 +08:00
// Output short time with microsecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortTimeMicroString() // 131415.999999
2023-01-07 08:48:51 +08:00
// Output short time with nanosecond format string
2022-05-01 09:09:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToShortTimeNanoString() // 131415.999999999
2020-10-07 17:15:46 +08:00
2023-12-24 18:35:29 +08:00
// Output Ansic format string
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
2023-01-07 08:48:51 +08:00
// Output Atom format string
2021-06-04 16:17:52 +08:00
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
2023-01-07 08:48:51 +08:00
// Output Unix date format string
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
2023-01-07 08:48:51 +08:00
// Output Ruby date format string
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
2023-01-07 08:48:51 +08:00
// Output Kitchen format string
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").ToKitchenString() // 1:14PM
2023-01-07 08:48:51 +08:00
// Output Cookie format string
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
2023-01-07 08:48:51 +08:00
// Output day, date and time format string
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString() // Wed, Aug 5, 2020 1:14 PM
2023-01-07 08:48:51 +08:00
// Output RSS format string
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
2023-01-07 08:48:51 +08:00
// Output W3C format string
2020-10-07 17:15:46 +08:00
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00
2023-01-07 08:48:51 +08:00
// Output ISO8601 format string
2022-04-14 22:43:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601String() // 2020-08-05T13:14:15+08:00
2023-01-07 08:48:51 +08:00
// Output ISO8601 with millisecond format string
2022-04-14 22:43:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MilliString() // 2020-08-05T13:14:15.999+08:00
2023-01-07 08:48:51 +08:00
// Output ISO8601 with microsecond format string
2022-04-14 22:43:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MicroString() // 2020-08-05T13:14:15.999999+08:00
2023-01-07 08:48:51 +08:00
// Output ISO8601 with nanosecond format string
2022-04-14 22:43:59 +08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601NanoString() // 2020-08-05T13:14:15.999999999+08:00
2023-01-07 08:48:51 +08:00
// Output RFC822 format string
2021-02-01 21:29:21 +08:00
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
2023-01-07 08:48:51 +08:00
// Output RFC822Z format string
2021-02-01 21:29:21 +08:00
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString() // 05 Aug 20 13:14 +0800
2023-01-07 08:48:51 +08:00
// Output RFC850 format string
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
2023-01-07 08:48:51 +08:00
// Output RFC1036 format string
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
2023-01-07 08:48:51 +08:00
// Output RFC1123 format string
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
2023-01-07 08:48:51 +08:00
// Output RFC1123Z format string
2021-07-28 09:18:51 +08:00
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString() // Wed, 05 Aug 2020 13:14:15 +0800
2023-01-07 08:48:51 +08:00
// Output RFC2822 format string
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
2023-01-07 08:48:51 +08:00
// Output RFC7231 format string
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
2021-02-01 21:29:21 +08:00
2023-01-07 08:48:51 +08:00
// Output RFC3339 format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String() // 2020-08-05T13:14:15+08:00
2023-01-07 08:48:51 +08:00
// Output RFC3339 with millisecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString() // 2020-08-05T13:14:15.999+08:00
2023-01-07 08:48:51 +08:00
// Output RFC3339 with microsecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString() // 2020-08-05T13:14:15.999999+08:00
2023-01-07 08:48:51 +08:00
// Output RFC3339 with nanosecond format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString() // 2020-08-05T13:14:15.999999999+08:00
2023-01-07 09:12:38 +08:00
// Output datetime format string
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
2023-01-07 08:48:51 +08:00
// Output "2006-01-02 15:04:05.999999999 -0700 MST" format string
2022-04-12 17:40:58 +08:00
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
2021-07-23 11:05:33 +08:00
2022-05-24 07:04:44 +08:00
// Output string by layout, Layout() is shorthand for ToLayoutString()
2022-04-24 22:15:44 +08:00
carbon.Parse("2020-08-05 13:14:15").Layout(carbon.ISO8601Layout) // 2020-08-05T13:14:15+08:00
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
2021-07-23 11:05:33 +08:00
2022-05-24 07:04:44 +08:00
// Output string by format, Format() is shorthand for 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
2020-10-07 17:15:46 +08:00
```
2021-08-16 09:34:15 +08:00
2021-07-24 00:10:47 +08:00
> For more supported format signs, please see the <a href="#format-sign-table">Format sign table</a>
2021-02-18 14:32:31 +08:00
2021-07-24 00:10:47 +08:00
##### Constellation
2021-08-16 09:34:15 +08:00
2021-07-12 16:20:47 +08:00
```go
2021-07-24 00:10:47 +08:00
// Get constellation name
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").Constellation() // Leo
2021-07-31 13:08:43 +08:00
// Whether is Aries
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsAries() // false
2021-07-31 13:08:43 +08:00
// Whether is Taurus
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsTaurus() // false
2021-07-31 13:08:43 +08:00
// Whether is Gemini
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsGemini() // false
2021-07-31 13:08:43 +08:00
// Whether is Cancer
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsCancer() // false
2021-07-31 13:08:43 +08:00
// Whether is Leo
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsLeo() // true
2021-07-31 13:08:43 +08:00
// Whether is Virgo
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsVirgo() // false
2021-07-31 13:08:43 +08:00
// Whether is Libra
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsLibra() // false
2021-07-31 13:08:43 +08:00
// Whether is Scorpio
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsScorpio() // false
2021-07-31 13:08:43 +08:00
// Whether is Sagittarius
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsSagittarius() // false
2021-07-31 13:08:43 +08:00
// Whether is Capricorn
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsCapricorn() // false
2021-07-31 13:08:43 +08:00
// Whether is Aquarius
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsAquarius() // false
2021-07-31 13:08:43 +08:00
// Whether is Pisces
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsPisces() // false
```
2021-07-24 00:10:47 +08:00
##### Season
2021-08-16 09:34:15 +08:00
2021-08-02 10:17:47 +08:00
> According to the meteorological division method, March to May is spring, June to August is summer, September to November is autumn, and December to February is winter
2021-08-16 09:34:15 +08:00
2021-07-12 16:20:47 +08:00
```go
2021-07-24 00:10:47 +08:00
// Get season name
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
// Start of the season
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
// End of the season
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
// Whether is spring
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
// Whether is summer
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
// Whether is autumn
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
// Whether is winter
2021-07-12 16:20:47 +08:00
carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
```
2020-09-07 17:10:33 +08:00
2023-12-26 22:00:05 +08:00
##### Lunar
2021-08-16 09:34:15 +08:00
2021-07-24 00:10:47 +08:00
> Currently only `200` years from `1900` to `2100` are supported
2021-08-16 09:34:15 +08:00
2020-10-01 14:35:56 +08:00
```go
2021-08-05 19:36:51 +08:00
// Get Chinese Lunar year of animal
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().Animal() // 鼠
2021-08-05 19:36:51 +08:00
// Get Chinese lunar festival
2022-04-21 15:08:24 +08:00
carbon.Parse("2021-02-12 13:14:15").Lunar().Festival() // 春节
// Get Chinese lunar year, month, day, hour, minute and second
carbon.Parse("2020-08-05 13:14:15").Lunar().DateTime() // 2020, 6, 16, 13, 14, 15
// Get Chinese lunar year, month and day
carbon.Parse("2020-08-05 13:14:15").Lunar().Date() // 2020, 6, 16
// Get Chinese lunar hour, minute and second
carbon.Parse("2020-08-05 13:14:15").Lunar().Time() // 13, 14, 15
2021-08-05 19:36:51 +08:00
// Get Chinese lunar year
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().Year() // 2020
2021-08-05 19:36:51 +08:00
// Get Chinese lunar month
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().Month() // 6
2021-08-05 19:36:51 +08:00
// Get Chinese lunar leap month
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().LeapMonth() // 4
2021-08-05 19:36:51 +08:00
// Get Chinese lunar day
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().Day() // 16
2023-01-07 08:48:51 +08:00
// Get Chinese lunar date as YYYY-MM-DD HH::ii::ss format string
2022-04-21 15:08:24 +08:00
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15").Lunar()) // 2020-06-16 13:14:15
2021-08-05 19:36:51 +08:00
// Get Chinese lunar year as string
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().ToYearString() // 二零二零
2021-08-05 19:36:51 +08:00
// Get Chinese lunar month as string
2022-04-22 10:12:28 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().ToMonthString() // 六月
2021-08-05 19:36:51 +08:00
// Get Chinese lunar day as string
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().ToDayString() // 十六
2021-08-05 19:36:51 +08:00
// Get Chinese lunar date as string
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().ToDateString() // 二零二零年六月十六
2021-07-08 09:49:00 +08:00
2021-07-31 13:08:43 +08:00
// Whether is a leap year
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsLeapYear() // true
2021-07-31 13:08:43 +08:00
// Whether is a leap month
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsLeapMonth() // false
2020-10-01 14:35:56 +08:00
2021-07-31 13:08:43 +08:00
// Whether is a year of the rat
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsRatYear() // true
2021-07-31 13:08:43 +08:00
// Whether is a year of the ox
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsOxYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the tiger
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsTigerYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the rabbit
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsRabbitYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the dragon
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsDragonYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the snake
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsSnakeYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the horse
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsHorseYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the goat
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsGoatYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the monkey
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsMonkeyYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the rooster
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsRoosterYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the dog
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsDogYear() // false
2021-07-31 13:08:43 +08:00
// Whether is a year of the dig
2022-04-21 15:08:24 +08:00
carbon.Parse("2020-08-05 13:14:15").Lunar().IsPigYear() // false
// Get Chinese double-hour
carbon.Parse("2020-02-05 21:00:00").Lunar().DoubleHour() // 亥时
// Whether is FirstDoubleHour
carbon.Parse("2020-03-21 00:00:00").Lunar().IsFirstDoubleHour() // true
// Whether is SecondDoubleHour
carbon.Parse("2020-03-21 01:00:00").Lunar().IsSecondDoubleHour() // true
// Whether is ThirdDoubleHour
carbon.Parse("2020-03-21 03:00:00").Lunar().IsThirdDoubleHour() // true
// Whether is FourthDoubleHour
carbon.Parse("2020-03-21 05:00:00").Lunar().IsFourthDoubleHour() // true
// Whether is FifthDoubleHour
carbon.Parse("2020-03-21 07:00:00").Lunar().IsFifthDoubleHour() // true
// Whether is SixthDoubleHour
carbon.Parse("2020-03-21 09:00:00").Lunar().IsSixthDoubleHour() // true
// Whether is SeventhDoubleHour
carbon.Parse("2020-03-21 11:00:00").Lunar().IsSeventhDoubleHour() // true
// Whether is EighthDoubleHour
carbon.Parse("2020-03-21 13:00:00").Lunar().IsEighthDoubleHour() // true
// Whether is NinthDoubleHour
carbon.Parse("2020-03-21 15:00:00").Lunar().IsNinthDoubleHour() // true
// Whether is TenthDoubleHour
carbon.Parse("2020-03-21 17:00:00").Lunar().IsTenthDoubleHour() // true
// Whether is EleventhDoubleHour
carbon.Parse("2020-03-21 19:00:00").Lunar().IsEleventhDoubleHour() // true
// Whether is TwelfthDoubleHour
carbon.Parse("2020-03-21 21:00:00").Lunar().IsTwelfthDoubleHour() // true
2020-10-01 14:35:56 +08:00
```
2023-12-26 22:00:05 +08:00
##### JSON
2021-07-19 09:55:54 +08:00
2021-07-24 00:10:47 +08:00
###### Define model
2021-08-16 09:34:15 +08:00
2023-12-26 22:00:05 +08:00
```go
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 Carbon `json:"birthday0"`
Birthday1 Carbon `json:"birthday1" carbon:"date"`
Birthday2 Carbon `json:"birthday2" carbon:"time"`
Birthday3 Carbon `json:"birthday3" carbon:"dateTime"`
Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"`
Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"`
Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"`
}
```
or
2021-07-19 09:55:54 +08:00
```go
type Person struct {
2023-12-22 16:01:00 +08:00
Name string `json:"name"`
2023-12-26 22:00:05 +08:00
Age int `json:"age"`
Birthday0 Carbon `json:"birthday0"`
Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"`
Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"`
Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05"`
Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"`
Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"`
Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"`
2021-07-19 09:55:54 +08:00
}
```
2023-12-22 16:01:00 +08:00
or
2023-09-26 22:08:43 +08:00
```go
2023-12-22 16:01:00 +08:00
type Person struct {
Name string `json:"name"`
2023-12-26 22:00:05 +08:00
Age int `json:"age"`
Birthday0 Carbon `json:"birthday0"`
Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"`
Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"`
Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"`
Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"`
Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"`
Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s" tz:"PRC"`
2023-09-26 22:08:43 +08:00
}
```
2023-12-26 22:00:05 +08:00
> If the `carbon` tag is not set, the default is `layout:2006-01-02 15:04:05`, if the `tz` tag is not set, the default is `Local`
2023-12-22 16:01:00 +08:00
###### Instantiate model
2021-07-19 09:55:54 +08:00
```go
2023-12-22 16:01:00 +08:00
now := Parse("2020-08-05 13:14:15", PRC)
2021-07-19 09:55:54 +08:00
person := Person {
2023-12-26 22:00:05 +08:00
Name: "gouguoyin",
Age: 18,
Birthday0: now,
Birthday1: now,
Birthday2: now,
Birthday3: now,
Birthday4: now,
Birthday5: now,
Birthday6: now,
2021-07-19 09:55:54 +08:00
}
```
2021-07-24 00:10:47 +08:00
###### JSON encode
2021-08-16 09:34:15 +08:00
2021-07-19 09:55:54 +08:00
```go
2023-12-26 22:00:05 +08:00
loadErr := carbon.LoadTag(& person)
if loadErr != nil {
2023-12-22 16:01:00 +08:00
// Error handle...
2023-12-26 22:00:05 +08:00
log.Fatal(loadErr)
2023-12-22 16:01:00 +08:00
}
2023-12-26 22:00:05 +08:00
data, marshalErr := json.Marshal(person)
if marshalErr != nil {
2023-12-22 16:01:00 +08:00
// Error handle...
2023-12-26 22:00:05 +08:00
log.Fatal(marshalErr)
2021-07-19 09:55:54 +08:00
}
2021-08-16 09:34:15 +08:00
fmt.Printf("%s", data)
2021-09-10 20:44:55 +08:00
// Output
2021-07-19 09:55:54 +08:00
{
2023-12-22 16:01:00 +08:00
"name": "gouguoyin",
"age": 18,
2023-12-26 22:00:05 +08:00
"birthday0": "2020-08-05 13:14:15",
"birthday1": "2020-08-05",
"birthday2": "13:14:15",
"birthday3": "2020-08-05 13:14:15",
"birthday4": "2020-08-05",
"birthday5": "213:14:15",
"birthday6": "2020-08-05 13:14:15"
2021-07-19 09:55:54 +08:00
}
```
2021-07-24 00:10:47 +08:00
###### JSON decode
2021-08-16 09:34:15 +08:00
2021-07-19 09:55:54 +08:00
```go
2022-04-17 19:52:02 +08:00
str := `{
2023-12-22 16:01:00 +08:00
"name": "gouguoyin",
"age": 18,
2023-12-26 22:00:05 +08:00
"birthday0": "2020-08-05 13:14:15",
"birthday1": "2020-08-05",
"birthday2": "13:14:15",
"birthday3": "2020-08-05 13:14:15",
"birthday4": "2020-08-05",
"birthday5": "213:14:15",
"birthday6": "2020-08-05 13:14:15"
2021-07-19 09:55:54 +08:00
}`
2023-12-22 16:01:00 +08:00
var person Person
2023-12-26 22:00:05 +08:00
loadErr := carbon.LoadTag(& person)
if loadErr != nil {
2023-12-22 16:01:00 +08:00
// Error handle...
2023-12-26 22:00:05 +08:00
log.Fatal(loadErr)
2023-12-22 16:01:00 +08:00
}
2023-12-26 22:00:05 +08:00
unmarshalErr := json.Unmarshal([]byte(str), & person)
if unmarshalErr != nil {
2023-12-22 16:01:00 +08:00
// Error handle...
2023-12-26 22:00:05 +08:00
log.Fatal(unmarshalErr)
2021-07-19 09:55:54 +08:00
}
2023-09-12 22:11:43 +08:00
2023-12-26 22:00:05 +08:00
fmt.Sprintf("%s", person.Birthday0) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday1) // 2020-08-05
fmt.Sprintf("%s", person.Birthday2) // 13:14:15
fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday4) // 2002-08-05
fmt.Sprintf("%s", person.Birthday5) // 13:14:15
fmt.Sprintf("%s", person.Birthday6) // 2002-08-05 13:14:15
2021-07-19 09:55:54 +08:00
```
2021-07-24 00:10:47 +08:00
##### I18n
The following languages are supported
2021-08-16 09:34:15 +08:00
2022-11-03 21:44:59 +08:00
* [Simplified Chinese(zh-CN) ](./lang/zh-CN.json "Simplified Chinese" ): translated
by [gouguoyin ](https://github.com/gouguoyin "gouguoyin" )
* [Traditional Chinese(zh-TW) ](./lang/zh-TW.json "Traditional Chinese" ): translated
by [gouguoyin ](https://github.com/gouguoyin "gouguoyin" )
2022-11-02 15:48:04 +08:00
* [English(en) ](./lang/en.json "English" ): translated
by [gouguoyin ](https://github.com/gouguoyin "gouguoyin" )
* [Japanese(jp) ](./lang/jp.json "Japanese" ): translated
by [gouguoyin ](https://github.com/gouguoyin "gouguoyin" )
* [Korean(kr) ](./lang/kr.json "Korean" ): translated by [nannul ](https://github.com/nannul "nannul" )
2021-08-05 19:36:51 +08:00
* [German(de) ](./lang/de.json "German" ): translated by [benzammour ](https://github.com/benzammour "benzammour" )
2022-04-17 19:52:02 +08:00
* [Spanish(es) ](./lang/es.json "Spanish" ): translated by [hgisinger ](https://github.com/hgisinger "hgisinger" )
2021-08-09 16:47:20 +08:00
* [Turkish(tr) ](./lang/tr.json "Turkish" ): translated by [emresenyuva ](https://github.com/emresenyuva "emresenyuva" )
2021-08-16 09:34:15 +08:00
* [Portuguese(pt) ](./lang/pt.json "Portuguese" ): translated by [felipear89 ](https://github.com/felipear89 "felipear89" )
2021-08-23 11:32:03 +08:00
* [Russian(ru) ](./lang/ru.json "Russian" ): translated by [zemlyak ](https://github.com/zemlyak "zemlyak" )
2022-04-18 11:57:39 +08:00
* [Ukrainian(uk) ](./lang/uk.json "Ukrainian" ): translated by [open-git ](https://github.com/open-git "open-git" )
2022-05-20 00:02:30 +08:00
* [Romanian(ro) ](./lang/ro.json "Romanian" ): translated by [DrOctavius ](https://github.com/DrOctavius "DrOctavius" )
2022-06-15 12:34:01 +08:00
* [Indonesian(id) ](./lang/id.json "Indonesian" ): translated by [justpoypoy ](https://github.com/justpoypoy "justpoypoy" )
2022-11-06 22:59:15 +08:00
* [Bahasa Malaysia(ms-MY) ](./lang/ms-MY.json "Bahasa Malaysia" ): translated
by [hollowaykeanho ](https://github.com/hollowaykeanho "hollowaykeanho" )
* [French(fr) ](./lang/fr.json "French" ): translated
by [hollowaykeanho ](https://github.com/hollowaykeanho "hollowaykeanho" )
2022-11-02 15:48:04 +08:00
* [Thailand(th) ](./lang/th.json "Thailand" ): translated by [izcream ](https://github.com/izcream "izcream" )
2023-09-07 01:34:35 +08:00
* [Swedish(se) ](./lang/se.json "Swedish" ): translated by [jwanglof ](https://github.com/jwanglof "jwanglof" )
* [Iranian(fa) ](./lang/fa.json "Iranian" ): translated by [erfanMomeniii ](https://github.com/erfanMomeniii "erfanMomeniii" )
2023-10-27 10:31:39 +08:00
* [Dutch(nl) ](./lang/nl.json "Dutch" ): translated by [RemcoE33 ](https://github.com/RemcoE33 "RemcoE33" )
2021-07-24 00:10:47 +08:00
The following methods are supported
2021-08-16 09:34:15 +08:00
2021-07-24 00:10:47 +08:00
* `Constellation()` : get constellation name
* `Season()` : get season name
2022-05-24 07:04:44 +08:00
* `DiffForHumans()` : get the difference with human-readable format
2023-01-07 08:48:51 +08:00
* `ToMonthString()` : output month format string
* `ToShortMonthString()` : output short month format string
* `ToWeekString()` : output week format string
* `ToShortWeekString()` : output short week format string
2021-07-24 00:10:47 +08:00
###### Set locale
2021-08-16 09:34:15 +08:00
2021-02-23 09:32:55 +08:00
```go
2021-09-10 20:44:55 +08:00
lang := carbon.NewLanguage()
2022-04-21 15:08:24 +08:00
lang.SetLocale("en")
2021-02-23 09:32:55 +08:00
2021-07-12 16:20:47 +08:00
c := carbon.SetLanguage(lang)
2022-05-24 07:04:44 +08:00
if c.Error != nil {
2022-06-15 12:34:01 +08:00
// Error handle...
log.Fatal(err)
2021-09-10 20:57:12 +08:00
}
2022-04-21 15:08:24 +08:00
c.Now().AddHours(1).DiffForHumans() // 1 hour from now
c.Now().AddHours(1).ToMonthString() // August
c.Now().AddHours(1).ToShortMonthString() // Aug
c.Now().AddHours(1).ToWeekString() // Wednesday
c.Now().AddHours(1).ToShortWeekString() // Wed
c.Now().AddHours(1).Constellation() // Leo
c.Now().AddHours(1).Season() // Summer
2021-07-12 16:20:47 +08:00
```
2021-07-24 00:10:47 +08:00
2021-08-05 19:36:51 +08:00
###### Reset some resources(the rests still translate from the given locale)
2021-08-16 09:34:15 +08:00
2021-07-12 16:20:47 +08:00
```go
2021-09-10 20:44:55 +08:00
lang := carbon.NewLanguage()
2021-09-10 20:57:12 +08:00
lang.SetLocale("en")
2021-07-12 16:20:47 +08:00
resources := map[string]string {
2021-08-16 09:34:15 +08:00
"hour": "%dh",
2021-07-12 16:20:47 +08:00
}
lang.SetResources(resources)
c := carbon.SetLanguage(lang)
2021-09-10 20:57:12 +08:00
if c.Error != nil {
// Error handle...
log.Fatal(err)
}
2021-07-12 16:20:47 +08:00
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
c.Now().Season() // Summer
```
2021-08-05 19:36:51 +08:00
###### Reset all resources
2021-08-16 09:34:15 +08:00
2021-07-12 16:20:47 +08:00
```go
2021-09-10 20:44:55 +08:00
lang := carbon.NewLanguage()
2021-07-12 16:20:47 +08:00
resources := map[string]string {
2023-09-11 11:32:31 +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",
"now": "just now",
"ago": "%s ago",
"from_now": "in %s",
"before": "%s before",
"after": "%s after",
2021-07-12 16:20:47 +08:00
}
lang.SetResources(resources)
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-02-23 09:32:55 +08:00
```
2023-09-21 13:59:19 +08:00
##### Testing
```go
2023-12-27 18:00:46 +08:00
c := carbon.SetTimezone(carbon.UTC)
2023-09-21 13:59:19 +08:00
2023-12-27 18:00:46 +08:00
c.Now().ToDateString() // 2023-12-27
2023-12-28 11:39:15 +08:00
c.Now().IsSetTestNow() // false
2023-09-21 13:59:19 +08:00
2023-12-27 18:00:46 +08:00
c.SetTestNow(carbon.Parse("2020-08-05"))
c.Now().ToDateString() // 2020-08-05
c.Now().IsSetTestNow() // true
2023-09-21 13:59:19 +08:00
2023-12-27 18:00:46 +08:00
c.UnSetTestNow()
c.Now().ToDateString() // 2023-12-2
c.Now().IsSetTestNow() // false
2023-09-21 13:59:19 +08:00
```
2023-12-26 22:00:05 +08:00
##### Error
2021-08-16 09:34:15 +08:00
2021-07-24 00:10:47 +08:00
> If more than one error occurs, only the first error is returned
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
// Error handle...
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
// Output
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
#### Appendix
2021-08-16 09:34:15 +08:00
2021-07-24 00:10:47 +08:00
##### <a id="format-sign-table">Format sign table</a>
2023-12-26 22:00:05 +08:00
| sign | desc | length | range | example |
|:----:|:------------------------------------------------------------------------------------------------------:|:------:|:----------------:|:-------------------------------:|
| d | Day of the month, padded to 2 | 2 | 01-31 | 02 |
| D | Day of the week, as an abbreviate localized string | 3 | Mon-Sun | Mon |
| j | Day of the month, no padding | - | 1-31 | 2 |
| S | English ordinal suffix for the day of the month, 2 characters. Eg: st, nd, rd or th. Works well with j | 2 | st/nd/rd/th | th |
| l | Day of the week, as an unabbreviated localized string | - | Monday-Sunday | Monday |
| F | Month as an unabbreviated localized string | - | January-December | January |
| m | Month, padded to 2 | 2 | 01-12 | 01 |
| M | Month as an abbreviated localized string | 3 | Jan-Dec | Jan |
| n | Month, no padding | - | 1-12 | 1 |
| Y | Four-digit year | 4 | 0000-9999 | 2006 |
| y | Two-digit year | 2 | 00-99 | 06 |
| a | Lowercase morning or afternoon sign | 2 | am/pm | pm |
| A | Uppercase morning or afternoon sign | 2 | AM/PM | PM |
| g | Hour in 12-hour format, no padding | - | 1-12 | 3 |
| G | Hour in 24-hour format, no padding | - | 0-23 | 15 |
| h | Hour in 12-hour format, padded to 2 | 2 | 00-11 | 03 |
| H | Hour in 24-hour format, padded to 2 | 2 | 00-23 | 15 |
| i | Minute, padded to 2 | 2 | 01-59 | 04 |
| s | Second, padded to 2 | 2 | 01-59 | 05 |
2023-12-27 11:18:22 +08:00
| O | Difference to Greenwich time (GMT) without colon between hours and minutes | - | - | -0700 |
2023-12-26 22:00:05 +08:00
| P | Difference to Greenwich time (GMT) with colon between hours and minutes | - | - | -07:00 |
| T | Abbreviated timezone | - | - | MST |
| W | week of the year, padded to 2 | 2 | 01-52 | 01 |
| N | day of the week, padded to 2 | 2 | 01-07 | 02 |
| L | Whether it's a leap year | 1 | 0-1 | 0 |
| U | Unix timestamp with seconds | - | - | 1596604455 |
| V | Unix timestamp with millisecond | - | - | 1596604455666 |
| X | Unix timestamp with microsecond | - | - | 1596604455666666 |
| Z | Unix timestamp with nanoseconds | - | - | 1596604455666666666 |
| v | Millisecond | - | 1-999 | 999 |
| u | Microsecond | - | 1-999999 | 999999 |
| x | Nanosecond | - | 1-999999999 | 999999999 |
| w | Day of the week | 1 | 0-6 | 1 |
| t | Total days of the month | 2 | 28-31 | 31 |
| z | Day of the year | - | 1-365 | 2 |
| e | Location | - | - | America/New_York |
| Q | Quarter | 1 | 1-4 | 1 |
| C | Century | - | 0-99 | 21 |
2021-07-24 00:10:47 +08:00
2022-05-20 00:02:30 +08:00
#### FAQ
1、What is the difference between v1 and v2?
2022-06-15 12:34:01 +08:00
> There is no difference between v1 and v2 of the API, but the implementation of the translation resource files in `language.go` is different. The v1 is implemented by the third-party extension library [packr](https://github.com/gobuffalo/packr), and the v2 is implemented by the standard library [embed](https://pkg.go.dev/embed) after `golang1.16`. If your golang version is `1.16+`, the v2 is recommended, otherwise, the v1 is required.
2022-04-18 18:23:36 +08:00
2021-07-31 17:13:57 +08:00
#### References
2021-08-16 09:34:15 +08:00
2020-10-07 17:15:46 +08:00
* [briannesbitt/carbon ](https://github.com/briannesbitt/Carbon )
2023-09-09 13:26:58 +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 )
2023-09-09 13:26:58 +08:00
* [jodaOrg/joda-time ](https://github.com/jodaOrg/joda-time )
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 )
2022-04-18 18:23:36 +08:00
* [iamkun/dayjs ](https://github.com/iamkun/dayjs )
#### Contributors
2022-04-19 14:14:42 +08:00
Thanks to all of the following who contributed to `Carbon` :
2022-04-18 18:23:36 +08:00
2022-05-20 00:02:30 +08:00
< a href = "https://github.com/golang-module/carbon/graphs/contributors" > < img src = "https://contrib.rocks/image?repo=golang-module/carbon&max=100&columns=16" / > < / a >
2022-04-18 18:23:36 +08:00
#### Sponsors
2022-11-04 14:56:01 +08:00
`Carbon` is a non-commercial open source project. If you want to support `Carbon` , you can [buy a cup of coffee ](https://opencollective.com/go-carbon ) for developer.
2022-04-18 18:23:36 +08:00
#### Thanks
2022-04-18 23:29:32 +08:00
2022-04-21 15:08:24 +08:00
`Carbon` had been being developed with GoLand under the free JetBrains Open Source license, I would like to express my
thanks here.
2022-04-18 18:23:36 +08:00
2022-05-24 07:04:44 +08:00
< a href = "https://www.jetbrains.com" > < img src = "https://raw.githubusercontent.com/panjf2000/illustrations/master/jetbrains/jetbrains-variant-4.png" height = "100" alt = "JetBrains" / > < / a >