Time - Methods - 图1tip

The following list of common methods might be updated slower than new code features. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/gtime

New

  • Description: New creates and returns a Time object with the given parameters.
  • Format:
  1. func New(param ...interface{}) *Time
  • Example: Create a time object.
  1. func ExampleNew() {
  2. t1 := gtime.New(time.Now())
  3. t2 := gtime.New("2018-08-08 08:08:08")
  4. t3 := gtime.New(1533686888)
  5. fmt.Println(t1)
  6. fmt.Println(t2)
  7. fmt.Println(t3)
  8. // Output:
  9. // 2021-11-18 14:18:27
  10. // 2018-08-08 08:08:08
  11. // 2018-08-08 08:08:08

Now

  • Description: Now creates and returns the current time object.
  • Format:
  1. func Now() *Time
  • Example: Get the current time object.
  1. func ExampleNow() {
  2. t := gtime.Now()
  3. fmt.Println(t)
  4. // Output:
  5. // 2021-11-06 13:41:08
  6. }

Format

  • Description: Format output time.
  • Format:
  1. func (t *Time) Format(format string) string
  • Example: Format output time. The complete time format can be referred to in Time - Format.
  1. func ExampleTime_Format() {
  2. gt1 := gtime.New("2018-08-08 08:08:08")
  3. fmt.Println(gt1.Format("Y-m-d"))
  4. fmt.Println(gt1.Format("l"))
  5. fmt.Println(gt1.Format("F j, Y, g:i a"))
  6. fmt.Println(gt1.Format("j, n, Y"))
  7. fmt.Println(gt1.Format("h-i-s, j-m-y, it is w Day z"))
  8. fmt.Println(gt1.Format("D M j G:i:s T Y"))
  9. // Output:
  10. // 2018-08-08
  11. // Wednesday
  12. // August 8, 2018, 8:08 am
  13. // 8, 8, 2018
  14. // 08-08-08, 8-08-18, 0831 0808 3 Wedam18 219
  15. // Wed Aug 8 8:08:08 CST 2018
  16. }

String

  • Description: Output as a string.
  • Format:
  1. func (t *Time) String() string
  • Example: Output as a string type.
  1. func ExampleTime_String() {
  2. gt := gtime.New("2018-08-08 08:08:08")
  3. t1 := gt.String()
  4. fmt.Println(t1)
  5. fmt.Println(reflect.TypeOf(t1))
  6. // Output:
  7. // 2018-08-08 08:08:08
  8. // string
  9. }

Timestamp

  • Description: Get the second-level timestamp of the current object. Corresponding methods include TimestampMicro/TimestampMilli/TimestampNano.
  • Format:
  1. func (t *Time) Timestamp() int64
  2. func Timestamp() int64
  • Example: Get the second-level timestamp of the current object.
  1. func ExampleTime_Timestamp() {
  2. t := gtime.Now()
  3. fmt.Println(t.Timestamp())
  4. fmt.Println(gtime.Timestamp())
  5. fmt.Println(t.TimestampMicro())
  6. fmt.Println(t.TimestampMilli())
  7. fmt.Println(t.TimestampNano())
  8. // Output:
  9. // 1533686888
  10. // 1533686888
  11. // 1533686888000
  12. // 1533686888000000
  13. // 1533686888000000000
  14. }

ToZone

  • Description: Set timezone.
  • Format:
  1. func (t *Time) ToZone(zone string) (*Time, error)
  • Example: Get the second-level timestamp of the current object.
  1. func ExampleTime_ToZone() {
  2. gt1 := gtime.Now()
  3. gt2, _ := gt1.ToZone("Asia/Shanghai")
  4. gt3, _ := gt1.ToZone("Asia/Tokyo")
  5. fmt.Println(gt2)
  6. fmt.Println(gt3)
  7. // May Output:
  8. // 2021-11-11 17:10:10
  9. // 2021-11-11 18:10:10
  10. }

SetTimeZone

  • Description: Set timezone.
  • Format:
  1. func SetTimeZone(zone string) error
  • Example: Set timezone.
  1. func ExampleSetTimeZone() {
  2. gtime.SetTimeZone("Asia/Shanghai")
  3. fmt.Println(gtime.Datetime())
  4. gtime.SetTimeZone("Asia/Tokyo")
  5. fmt.Println(gtime.Datetime())
  6. // May Output:
  7. // 2018-08-08 08:08:08
  8. // 2018-08-08 09:08:08
  9. }

StrToTime

  • Description: Convert time string to time object.
  • Format:
  1. func StrToTime(str string, format ...string) (*Time, error)
  • Example: Convert time string to time object.
  1. func ExampleStrToTime() {
  2. res, _ := gtime.StrToTime("2006-01-02T15:04:05-07:00", "Y-m-d H:i:s")
  3. fmt.Println(res)
  4. // May Output:
  5. // 2006-01-02 15:04:05
  6. }

Add

  • Description: Add time to the current time object.
  • Format:
  1. func (t *Time) Add(d time.Duration) *Time
  • Example: Add time to the current time object.
  1. func ExampleTime_Add() {
  2. gt := gtime.New("2018-08-08 08:08:08")
  3. gt1 := gt.Add(time.Duration(10) * time.Second)
  4. fmt.Println(gt1)
  5. // Output:
  6. // 2018-08-08 08:08:18
  7. }

StartOfDay

  • Description: Return the start time object of today. Similar methods include StartOfHalf/StartOfHour/StartOfMonth/StartOfMinute/StartOfQuarter, etc.
  • Format:
  1. func (t *Time) StartOfDay() *Time
  • Example: Return the start time object of today.
  1. func ExampleTime_StartOfDay() {
  2. gt1 := gtime.New("2018-08-08 08:08:08")
  3. fmt.Println(gt1.StartOfDay())
  4. // Output:
  5. // 2018-08-08 00:00:00
  6. }

EndOfDay

  • Description: Return the end time object of today. Similar methods include EndOfHalf/EndOfHour/EndOfMonth/EndOfMinute/EndOfQuarter, etc.
  • Format:
  1. func (t *Time) EndOfDay() *Time
  • Example: Return the end time object of today.
  1. func ExampleTime_EndOfDay() {
  2. gt1 := gtime.New("2018-08-08 08:08:08")
  3. fmt.Println(gt1.EndOfDay())
  4. // Output:
  5. // 2018-08-08 23:59:59
  6. }

Month

  • Description: Return the month index in a year. For example, January corresponds to 1.
  • Format:
  1. func (t *Time) Month() int
  • Example: Return the month index in a year.
  1. func ExampleTime_Month() {
  2. gt := gtime.New("2018-08-08 08:08:08")
  3. t1 := gt.Month()
  4. fmt.Println(t1)
  5. // Output:
  6. // 8
  7. }

Second

  • Description: Return the number of seconds in the current minute. For example, 10:10:08 corresponds to 8 seconds.
  • Format:
  1. func (t *Time) Second() int
  • Example: Return the number of seconds in the current minute.
  1. func ExampleTime_Second() {
  2. gt := gtime.New("2018-08-08 08:08:08")
  3. t1 := gt.Second()
  4. fmt.Println(t1)
  5. // Output:
  6. // 8
  7. }

IsZero

  • Description: Check if the time equals 0001-01-01 00:00:00. Note it does not represent the timestamp 0, which is 1970-01-01 08:00:00.
  • Format:
  1. func (t *Time) IsZero() bool
  • Example: Check if the month index in a year.
  1. func ExampleTime_IsZero() {
  2. gt := gtime.New("0-0-0")
  3. fmt.Println(gt.IsZero())
  4. // Output:
  5. // true
  6. }

AddDate

  • Description: Add specified year, month, and day to the current time object.
  • Format:
  1. func (t *Time) AddDate(years int, months int, days int) *Time
  • Example: Add specified year, month, and day to the current time object.
  1. func ExampleTime_AddDate() {
  2. var (
  3. year = 1
  4. month = 2
  5. day = 3
  6. )
  7. gt := gtime.New("2018-08-08 08:08:08")
  8. gt = gt.AddDate(year, month, day)
  9. fmt.Println(gt)
  10. // Output:
  11. // 2019-10-11 08:08:08
  12. }

Equal

  • Description: Check if two time objects are equal.
  • Format:
  1. func (t *Time) Equal(u *Time) bool
  • Example: Check if two time objects are equal.
  1. func ExampleTime_Equal() {
  2. gt1 := gtime.New("2018-08-08 08:08:08")
  3. gt2 := gtime.New("2018-08-08 08:08:08")
  4. fmt.Println(gt1.Equal(gt2))
  5. // Output:
  6. // true
  7. }

Before

  • Description: Determine the order of two time objects.
  • Format:
  1. func (t *Time) Before(u *Time) bool
  • Example: Determine the order of two time objects.
  1. func ExampleTime_Before() {
  2. gt1 := gtime.New("2018-08-07 08:08:08")
  3. gt2 := gtime.New("2018-08-08 08:08:08")
  4. fmt.Println(gt1.Before(gt2))
  5. // Output:
  6. // true
  7. }

After

  • Description: Determine the order of two time objects.
  • Format:
  1. func (t *Time) After(u *Time) bool
  • Example: Determine the order of two time objects.
  1. func ExampleTime_After() {
  2. gt1 := gtime.New("2018-08-07 08:08:08")
  3. gt2 := gtime.New("2018-08-08 08:08:08")
  4. fmt.Println(gt1.After(gt2))
  5. // Output:
  6. // false
  7. }

Layout

  • Description: Format output time.
  • Format:
  1. func (t *Time) Layout(layout string) string
  • Example: Format output time.
  1. func ExampleTime_Layout() {
  2. gt1 := gtime.New("2018-08-08 08:08:08")
  3. fmt.Println(gt1.Layout("2006-01-02"))
  4. // Output:
  5. // 2018-08-08
  6. }

IsLeapYear

  • Description: Check if it’s a leap year.
  • Format:
  1. func (t *Time) IsLeapYear() bool
  • Example: Check if it’s a leap year.
  1. func ExampleTime_IsLeapYear() {
  2. gt1 := gtime.New("2018-08-08 08:08:08")
  3. fmt.Println(gt1.IsLeapYear())
  4. // Output:
  5. // false
  6. }

Date

  • Description: Get the date.
  • Format:
  1. func Date() string
  • Example: Get the date.
  1. func ExampleDate() {
  2. fmt.Println(gtime.Date())
  3. // May Output:
  4. // 2006-01-02
  5. }

Datetime

  • Description: Get the datetime.
  • Format:
  1. func Datetime() string
  • Example: Get the datetime.
  1. func ExampleDatetime() {
  2. fmt.Println(gtime.Datetime())
  3. // May Output:
  4. // 2006-01-02 15:04:05
  5. }

ISO8601

  • Description: Return time in ISO8601 format.
  • Format:
  1. func ISO8601() string
  • Example:
  1. func ExampleISO8601() {
  2. fmt.Println(gtime.ISO8601())
  3. // May Output:
  4. // 2006-01-02T15:04:05-07:00
  5. }

RFC822

  • Description: Return time in RFC822 format.
  • Format:
  1. func RFC822() string
  • Example:
  1. func ExampleRFC822() {
  2. fmt.Println(gtime.RFC822())
  3. // May Output:
  4. // Mon, 02 Jan 06 15:04 MST
  5. }

StrToTimeFormat

  • Description: StrToTimeFormat returns time object based on the input time string and format.
  • Format:
  1. func StrToTimeFormat(str string, format string) (*Time, error)
  • Example:
  1. func ExampleStrToTimeFormat() {
  2. res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
  3. fmt.Println(res)
  4. // Output:
  5. // 2006-01-02 15:04:05
  6. }

StrToTimeLayout

  • Description: StrToTimeLayout returns time object based on the input time string and format.
  • Format:
  1. func StrToTimeLayout(str string, layout string) (*Time, error)
  • Example:
  1. func ExampleStrToTimeLayout() {
  2. res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02")
  3. fmt.Println(res)
  4. // Output:
  5. // 2018-08-08 00:00:00
  6. }

MarshalJSON

  • Description: MarshalJSON overrides the method in json.Marshal.
  • Format:
  1. func (t *Time) MarshalJSON() ([]byte, error)
  • Example:
  1. func ExampleTime_MarshalJSON() {
  2. type Person struct {
  3. Name string `json:"name"`
  4. Birthday *gtime.Time `json:"birthday"`
  5. }
  6. p := new(Person)
  7. p.Name = "goframe"
  8. p.Birthday = gtime.New("2018-08-08 08:08:08")
  9. j, _ := json.Marshal(p)
  10. fmt.Println(string(j))
  11. // Output:
  12. // {"name":"xiaoming","birthday":"2018-08-08 08:08:08"}
  13. }

UnmarshalJSON

  • Description: UnmarshalJSON overrides the method in json.Unmarshal.
  • Format:
  1. func (t *Time) UnmarshalJSON() ([]byte, error)
  • Example:
  1. func ExampleTime_MarshalJSON() {
  2. type Person struct {
  3. Name string `json:"name"`
  4. Birthday *gtime.Time `json:"birthday"`
  5. }
  6. p := new(Person)
  7. p.Name = "goframe"
  8. p.Birthday = gtime.New("2018-08-08 08:08:08")
  9. j, _ := json.Marshal(p)
  10. fmt.Println(string(j))
  11. // Output:
  12. // {"name":"xiaoming","birthday":"2018-08-08 08:08:08"}
  13. }

WeekOfYear

  • Description: WeekOfYear returns the current week number of the year, starting from 1. Similar methods include DayOfYear/DaysInMonth.
  • Format:
  1. func (t *Time) WeeksOfYear() int
  • Example:
  1. func ExampleTime_WeeksOfYear() {
  2. gt1 := gtime.New("2018-01-08 08:08:08")
  3. fmt.Println(gt1.WeeksOfYear())
  4. // Output:
  5. // 2
  6. }D