11# Carbon
2- Englsih | [ Chinese] ( ./README.md )
2+ English | [ Chinese] ( ./README.md )
33
44#### Description
55A simple,semantic and developer-friendly golang package for datetime
@@ -24,22 +24,12 @@ go get -u gitee.com/go-package/carbon
2424
2525import (
2626 " gitee.com/go-package/carbon"
27- )
28-
27+ )
2928```
3029
3130#### Usage and example
3231> The default timezone is Local, assuming the current time is 2020-08-05 13:14:15
3332
34- ##### Set timezone
35- ``` go
36- carbon.Timezone (carbon.PRC ).Now ().ToDateTimeString () // 2020-08-05 13:14:15
37- carbon.Timezone (carbon.Tokyo ).Now ().ToDateTimeString () // 2020-08-05 14:14:15
38- carbon.Timezone (carbon.Tokyo ).Timezone (carbon.PRC ).Now ().ToDateTimeString () // 2020-08-05 13:14:15
39- ```
40-
41- > For more timezone constants, please see the [ const.go] ( ./const.go ) file
42-
4333##### Yesterday,today and tomorrow
4434``` go
4535// Datetime of today
@@ -70,39 +60,6 @@ carbon.Tomorrow().ToTimeString() // 13:14:15
7060carbon.Tomorrow ().ToTimestamp () // 1596690855
7161```
7262
73- ##### Beginning and end
74- ``` go
75- // Beginning of the year
76- carbon.Parse (" 2020-08-05 13:14:15" ).BeginningOfYear ().ToDateTimeString () // 2020-01-01 00:00:00
77- // End of the year
78- carbon.Parse (" 2020-08-05 13:14:15" ).EndOfYear ().ToEndTimeString () // 2020-12-31 23:59:59
79-
80- // Beginning of the month
81- carbon.Parse (" 2020-08-05 13:14:15" ).BeginningOfMonth ().ToStartTimeString () // 2020-08-01 00:00:00
82- // End of the month
83- carbon.Parse (" 2020-08-05 13:14:15" ).EndOfMonth ().ToEndTimeString () // 2020-08-31 23:59:59
84-
85- // Beginning of the week
86- carbon.Parse (" 2020-08-05 13:14:15" ).FirstOfWeek ().ToStartTimeString () // 2020-08-03 00:00:00
87- // End of the week
88- carbon.Parse (" 2020-08-05 13:14:15" ).LastOfWeek ().ToEndTimeString () // 2020-08-09 23:59:59
89-
90- // Beginning of the day
91- carbon.Parse (" 2020-08-05 13:14:15" ).BeginningOfDay ().ToDateTimeString () // 2020-08-05 00:00:00
92- // End of the day
93- carbon.Parse (" 2020-08-05 13:14:15" ).EndOfDay ().ToDateTimeString () // 2020-08-05 23:59:59
94-
95- // Beginning of the hour
96- carbon.Parse (" 2020-08-05 13:14:15" ).BeginningOfHour ().ToDateTimeString () // 2020-08-05 13:00:00
97- // End of the hour
98- carbon.Parse (" 2020-08-05 13:14:15" ).EndOfHour ().ToDateTimeString () // 2020-08-05 13:59:59
99-
100- // Beginning of the minute
101- carbon.Parse (" 2020-08-05 13:14:15" ).BeginningOfMinute ().ToDateTimeString () // 2020-08-05 13:14:00
102- // End of the minute
103- carbon.Parse (" 2020-08-05 13:14:15" ).EndOfMinute ().ToDateTimeString () // 2020-08-05 13:14:59
104- ```
105-
10663##### Create carbon instance
10764``` go
10865// Create Carbon instance from timestamp
@@ -152,12 +109,78 @@ carbon.ParseByDuration("-10.5m").ToDateTimeString // 2020-08-05 13:03:45
152109carbon.ParseByDuration (" 10s" ).ToDateTimeString // 2020-08-05 13:14:25
153110// Ten seconds ago
154111carbon.ParseByDuration (" -10.5s" ).ToDateTimeString // 2020-08-05 13:14:04
112+ ```
113+
114+ ##### Time setter
115+ ``` go
116+ // Set timezone
117+ carbon.SetTimezone (carbon.PRC ).Now ().ToDateTimeString () // 2020-08-05 13:14:15
118+ carbon.SetTimezone (carbon.Tokyo ).Now ().ToDateTimeString () // 2020-08-05 14:14:15
119+ carbon.SetTimezone (carbon.Tokyo ).SetTimezone (carbon.PRC ).Now ().ToDateTimeString () // 2020-08-05 13:14:15
120+
121+ // Set year
122+ carbon.Parse (" 2019-08-05" ).SetYear (2020 ).ToDateString () // 2020-08-05
123+ carbon.Parse (" 2020-02-29" ).SetYear (2019 ).ToDateString () // 2019-03-01
124+
125+ // Set month
126+ carbon.Parse (" 2020-01-30" ).SetMonth (2 ).ToDateString () // 2020-03-01
127+ carbon.Parse (" 2020-01-31" ).SetMonth (2 ).ToDateString () // 2020-03-02
128+ carbon.Parse (" 2020-08-05" ).SetMonth (2 ).ToDateString () // 2020-02-05
129+
130+ // Set day
131+ carbon.Parse (" 2019-08-05" ).SetDay (31 ).ToDateString () // 2020-08-31
132+ carbon.Parse (" 2020-02-01" ).SetDay (31 ).ToDateString () // 2020-03-02
133+
134+ // Set hour
135+ carbon.Parse (" 2020-08-05 13:14:15" ).SetHour (10 ).ToDateTimeString () // 2020-08-05 10:14:15
136+ carbon.Parse (" 2020-08-05 13:14:15" ).SetHour (24 ).ToDateTimeString () // 2020-08-06 00:14:15
137+
138+ // Set minute
139+ carbon.Parse (" 2020-08-05 13:14:15" ).SetMinute (10 ).ToDateTimeString () // 2020-08-05 13:10:15
140+ carbon.Parse (" 2020-08-05 13:14:15" ).SetMinute (60 ).ToDateTimeString () // 2020-08-05 14:00:15
141+
142+ // Set second
143+ carbon.Parse (" 2020-08-05 13:14:15" ).SetSecond (10 ).ToDateTimeString () // 2020-08-05 13:14:10
144+ carbon.Parse (" 2020-08-05 13:14:15" ).SetSecond (60 ).ToDateTimeString () // 2020-08-05 13:15:00
145+ ```
146+ > For more timezone constants, please see the [ const.go] ( ./const.go ) file
147+
148+ ##### Start and end
149+ ``` go
150+ // Start of the year
151+ carbon.Parse (" 2020-08-05 13:14:15" ).StartOfYear ().ToDateTimeString () // 2020-01-01 00:00:00
152+ // End of the year
153+ carbon.Parse (" 2020-08-05 13:14:15" ).EndOfYear ().ToEndTimeString () // 2020-12-31 23:59:59
155154
155+ // Start of the month
156+ carbon.Parse (" 2020-08-05 13:14:15" ).StartOfMonth ().ToStartTimeString () // 2020-08-01 00:00:00
157+ // End of the month
158+ carbon.Parse (" 2020-08-05 13:14:15" ).EndOfMonth ().ToEndTimeString () // 2020-08-31 23:59:59
159+
160+ // Start of the week
161+ carbon.Parse (" 2020-08-05 13:14:15" ).StartOfWeek ().ToStartTimeString () // 2020-08-03 00:00:00
162+ // End of the week
163+ carbon.Parse (" 2020-08-05 13:14:15" ).LastOfWeek ().ToEndTimeString () // 2020-08-09 23:59:59
164+
165+ // Start of the day
166+ carbon.Parse (" 2020-08-05 13:14:15" ).StartOfDay ().ToDateTimeString () // 2020-08-05 00:00:00
167+ // End of the day
168+ carbon.Parse (" 2020-08-05 13:14:15" ).EndOfDay ().ToDateTimeString () // 2020-08-05 23:59:59
169+
170+ // Start of the hour
171+ carbon.Parse (" 2020-08-05 13:14:15" ).StartOfHour ().ToDateTimeString () // 2020-08-05 13:00:00
172+ // End of the hour
173+ carbon.Parse (" 2020-08-05 13:14:15" ).EndOfHour ().ToDateTimeString () // 2020-08-05 13:59:59
174+
175+ // Start of the minute
176+ carbon.Parse (" 2020-08-05 13:14:15" ).StartOfMinute ().ToDateTimeString () // 2020-08-05 13:14:00
177+ // End of the minute
178+ carbon.Parse (" 2020-08-05 13:14:15" ).EndOfMinute ().ToDateTimeString () // 2020-08-05 13:14:59
156179```
157180
158181##### Time travel
159182``` go
160- // After Three years
183+ // After three years
161184carbon.Parse (" 2020-02-29 13:14:15" ).AddYears (3 ).ToDateTimeString () // 2023-03-01 13:14:15
162185// Next three years
163186carbon.Parse (" 2020-02-29 13:14:15" ).NextYears (3 ).ToDateTimeString () // 2023-02-28 13:14:15
@@ -191,9 +214,9 @@ carbon.Parse("2020-03-31 13:14:15").SubMonth().ToDateTimeString() // 2020-03-02
191214// Previous one month
192215carbon.Parse (" 2020-03-31 13:14:15" ).PreMonth ().ToDateTimeString () // 2020-02-29 13:14:15
193216
194- // After Three days
217+ // After three days
195218carbon.Parse (" 2020-08-05 13:14:15" ).AddDays (3 ).ToDateTimeString () // 2020-08-08 13:14:15
196- // After One day
219+ // After one day
197220carbon.Parse (" 2020-08-05 13:14:15" ).AddDay ().ToDateTimeString () // 2020-08-05 13:14:15
198221// After three days
199222carbon.Parse (" 2020-08-05 13:14:15" ).SubDays (3 ).ToDateTimeString () // 2020-08-02 13:14:15
@@ -238,11 +261,37 @@ carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-
238261carbon.Parse (" 2020-08-05 13:14:15" ).Duration (" -2.5s" ).ToDateTimeString () // 2020-08-05 13:14:12
239262// Before one second
240263carbon.Parse (" 2020-08-05 13:14:15" ).SubSecond ().ToDateTimeString () // 2020-08-05 13:14:14
264+ ```
241265
266+ ##### Time difference
267+ ``` go
268+ // Difference in weeks
269+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffInWeeks (carbon.Parse (" 2020-07-28 13:14:15" )) // -1
270+ // Difference absolute in weeks
271+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffAbsInWeeks (carbon.Parse (" 2020-07-28 13:14:15" )) // 1
272+
273+ // Difference in days
274+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffInDays (carbon.Parse (" 2020-08-04 13:14:15" )) // -1
275+ // Difference absolute in days
276+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffAbsInDays (carbon.Parse (" 2020-08-04 13:14:15" )) // 1
277+
278+ // Difference in hours
279+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffInHours (carbon.Parse (" 2020-08-05 12:14:15" )) // -1
280+ // Difference absolute in hours
281+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffAbsInHours (carbon.Parse (" 2020-08-05 12:14:15" )) // 1
282+
283+ // Difference in minutes
284+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffInMinutes (carbon.Parse (" 2020-08-05 13:13:15" )) // -1
285+ // Difference absolute in minutes
286+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffAbsInMinutes (carbon.Parse (" 2020-08-05 13:13:15" )) // 1
287+
288+ // Difference in seconds
289+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffInSeconds (carbon.Parse (" 2020-08-05 13:14:14" )) // -1
290+ // Difference absolute in seconds
291+ carbon.Parse (" 2020-08-05 13:14:15" ).DiffAbsInSeconds (carbon.Parse (" 2020-08-05 13:14:14" )) // 1
242292```
243293
244- ##### Time output
245-
294+ ##### Time output
246295``` go
247296// To timestamp
248297carbon.Parse (" 2020-08-05 13:14:15" ).ToTimestamp () // 1596604455
@@ -294,50 +343,50 @@ carbon.Parse("2020-08-05 13:14:15").ToRFC2822String() // Wed, 05 Aug 2020 13:14:
294343carbon.Parse (" 2020-08-05 13:14:15" ).ToRFC3339String () // 2020-08-05T13:14:15+08:00
295344// To string of RFC7231 format
296345carbon.Parse (" 2020-08-05 13:14:15" ).ToRFC7231String () // Wed, 05 Aug 2020 05:14:15 GMT
297-
298346```
299347> For more format signs, please see the <a href =" #format-sign-table " >Format sign table</a >
300348
301- ##### Statistics
349+ ##### Time getter
302350``` go
303- // Total days of the year
351+ // Get total days of the year
304352carbon.Parse (" 2019-08-05 13:14:15" ).DaysInYear () // 365
305353carbon.Parse (" 2020-08-05 13:14:15" ).DaysInYear () // 366
306- // Total days of the month
354+ // Get total days of the month
307355carbon.Parse (" 2020-02-01 13:14:15" ).DaysInMonth () // 29
308356carbon.Parse (" 2020-04-01 13:14:15" ).DaysInMonth () // 30
309357carbon.Parse (" 2020-08-01 13:14:15" ).DaysInMonth () // 31
310358
311- // current age
312- carbon.Parse (" 1990-01-01 13:14:15" ).Age () // 30
313- carbon.Parse (" 1990-12-31 13:14:15" ).Age () // 29
359+ // Get day of the year
360+ carbon.Parse (" 2020-08-05 13:14:15" ).DayOfYear () // 218
361+ // Get week of the year
362+ carbon.Parse (" 2020-08-05 13:14:15" ).WeekOfYear () // 32
363+ // Get day of the month
364+ carbon.Parse (" 2020-08-05 13:14:15" ).DayOfMonth () // 5
365+ // Get week of the month
366+ carbon.Parse (" 2020-08-05 13:14:15" ).WeekOfMonth () // 1
367+ // Get day of the week
368+ carbon.Parse (" 2020-08-05 13:14:15" ).DayOfWeek () // 3
314369
315- // Current year
370+ // Get current year
316371carbon.Parse (" 2020-08-05 13:14:15" ).Year () // 2020
317- // Current month
372+ // Get current month
318373carbon.Parse (" 2020-08-05 13:14:15" ).Month () // 8
319- // Current day
374+ // Get current day
320375carbon.Parse (" 2020-08-05 13:14:15" ).Day () // 5
321- // Current hour
376+ // Get current hour
322377carbon.Parse (" 2020-08-05 13:14:15" ).Hour () // 13
323- // Current minute
378+ // Get current minute
324379carbon.Parse (" 2020-08-05 13:14:15" ).Minute () // 14
325- // Current second
380+ // Get current second
326381carbon.Parse (" 2020-08-05 13:14:15" ).Second () // 15
327- ```
328382
329- ##### Week and day
330- ``` go
331- // Day of the year
332- carbon.Parse (" 2020-08-05 13:14:15" ).DayOfYear () // 218
333- // Week of the year
334- carbon.Parse (" 2020-08-05 13:14:15" ).WeekOfYear () // 32
335- // Day of the month
336- carbon.Parse (" 2020-08-05 13:14:15" ).DayOfMonth () // 5
337- // Week of the month
338- carbon.Parse (" 2020-08-05 13:14:15" ).WeekOfMonth () // 1
339- // Day of the week
340- carbon.Parse (" 2020-08-05 13:14:15" ).DayOfWeek () // 3
383+ // Get timezone name
384+ carbon.SetTimezone (carbon.PRC ).Timezone () // PRC
385+ carbon.SetTimezone (carbon.Tokyo ).Timezone () // Asia/Tokyo
386+
387+ // Get current age
388+ carbon.Parse (" 2002-01-01 13:14:15" ).Age () // 17
389+ carbon.Parse (" 2002-12-31 13:14:15" ).Age () // 18
341390```
342391
343392##### Time judgment
@@ -357,62 +406,65 @@ carbon.Parse(carbon.Now().ToDateTimeString()).IsNow() // true
357406carbon.Parse (" 2020-08-06 13:14:15" ).IsFuture () // true
358407// Is pass time
359408carbon.Parse (" 2020-08-04 13:14:15" ).IsPast () // true
409+
360410// Is leap year
361411carbon.Parse (" 2020-08-05 13:14:15" ).IsLeapYear () // true
412+ // Is long year
413+ carbon.Parse (" 2020-08-05 13:14:15" ).IsLongYear () // true
362414
363- // Is January
415+ // Is january
364416carbon.Parse (" 2020-08-05 13:14:15" ).IsJanuary () // false
365- // Is February
417+ // Is february
366418carbon.Parse (" 2020-08-05 13:14:15" ).IsFebruary () // false
367- // Is March
419+ // Is march
368420carbon.Parse (" 2020-08-05 13:14:15" ).IsMarch () // false
369- // Is April
421+ // Is april
370422carbon.Parse (" 2020-08-05 13:14:15" ).IsApril () // false
371- // Is May
423+ // Is may
372424carbon.Parse (" 2020-08-05 13:14:15" ).IsMay () // false
373- // Is June
425+ // Is june
374426carbon.Parse (" 2020-08-05 13:14:15" ).IsJune () // false
375- // Is July
427+ // Is july
376428carbon.Parse (" 2020-08-05 13:14:15" ).IsJuly () // false
377- // Is August
429+ // Is august
378430carbon.Parse (" 2020-08-05 13:14:15" ).IsAugust () // false
379- // Is September
431+ // Is september
380432carbon.Parse (" 2020-08-05 13:14:15" ).IsSeptember () // true
381- // Is October
433+ // Is october
382434carbon.Parse (" 2020-08-05 13:14:15" ).IsOctober () // false
383- // Is November
435+ // Is november
384436carbon.Parse (" 2020-08-05 13:14:15" ).IsNovember () // false
385- // Is December
437+ // Is december
386438carbon.Parse (" 2020-08-05 13:14:15" ).IsDecember () // false
387439
388- // Is Monday
440+ // Is monday
389441carbon.Parse (" 2020-08-05 13:14:15" ).IsMonday () // false
390- // Is Tuesday
442+ // Is tuesday
391443carbon.Parse (" 2020-08-05 13:14:15" ).IsTuesday () // true
392- // Is Wednesday
444+ // Is wednesday
393445carbon.Parse (" 2020-08-05 13:14:15" ).IsWednesday () // false
394- // Is Thursday
446+ // Is thursday
395447carbon.Parse (" 2020-08-05 13:14:15" ).IsThursday () // false
396- // Is Friday
448+ // Is friday
397449carbon.Parse (" 2020-08-05 13:14:15" ).IsFriday () // false
398- // Is Saturday
450+ // Is saturday
399451carbon.Parse (" 2020-08-05 13:14:15" ).IsSaturday () // false
400- // Is Sunday
452+ // Is sunday
401453carbon.Parse (" 2020-08-05 13:14:15" ).IsSunday () // false
402- // Is Weekday
454+ // Is weekday
403455carbon.Parse (" 2020-08-05 13:14:15" ).IsWeekday () // false
404- // Is Weekend
456+ // Is weekend
405457carbon.Parse (" 2020-08-05 13:14:15" ).IsWeekend () // true
406458
407- // Is Yesterday
459+ // Is yesterday
408460carbon.Parse (" 2020-08-04 13:14:15" ).IsYesterday () // true
409461carbon.Parse (" 2020-08-04 00:00:00" ).IsYesterday () // true
410462carbon.Parse (" 2020-08-04" ).IsYesterday () // true
411- // Is Today
463+ // Is today
412464carbon.Parse (" 2020-08-05 13:14:15" ).IsToday () // true
413465carbon.Parse (" 2020-08-05 00:00:00" ).IsToday () // true
414466carbon.Parse (" 2020-08-05" ).IsToday () // true
415- // Is Tomorrow
467+ // Is tomorrow
416468carbon.Parse (" 2020-08-06 13:14:15" ).IsTomorrow () // true
417469carbon.Parse (" 2020-08-06 00:00:00" ).IsTomorrow () // true
418470carbon.Parse (" 2020-08-06" ).IsTomorrow () // true
0 commit comments