tip
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 aTime
object with the given parameters. - Format:
func New(param ...interface{}) *Time
- Example: Create a time object.
func ExampleNew() {
t1 := gtime.New(time.Now())
t2 := gtime.New("2018-08-08 08:08:08")
t3 := gtime.New(1533686888)
fmt.Println(t1)
fmt.Println(t2)
fmt.Println(t3)
// Output:
// 2021-11-18 14:18:27
// 2018-08-08 08:08:08
// 2018-08-08 08:08:08
Now
- Description:
Now
creates and returns the current time object. - Format:
func Now() *Time
- Example: Get the current time object.
func ExampleNow() {
t := gtime.Now()
fmt.Println(t)
// Output:
// 2021-11-06 13:41:08
}
Format
- Description: Format output time.
- Format:
func (t *Time) Format(format string) string
- Example: Format output time. The complete time format can be referred to in Time - Format.
func ExampleTime_Format() {
gt1 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.Format("Y-m-d"))
fmt.Println(gt1.Format("l"))
fmt.Println(gt1.Format("F j, Y, g:i a"))
fmt.Println(gt1.Format("j, n, Y"))
fmt.Println(gt1.Format("h-i-s, j-m-y, it is w Day z"))
fmt.Println(gt1.Format("D M j G:i:s T Y"))
// Output:
// 2018-08-08
// Wednesday
// August 8, 2018, 8:08 am
// 8, 8, 2018
// 08-08-08, 8-08-18, 0831 0808 3 Wedam18 219
// Wed Aug 8 8:08:08 CST 2018
}
String
- Description: Output as a string.
- Format:
func (t *Time) String() string
- Example: Output as a string type.
func ExampleTime_String() {
gt := gtime.New("2018-08-08 08:08:08")
t1 := gt.String()
fmt.Println(t1)
fmt.Println(reflect.TypeOf(t1))
// Output:
// 2018-08-08 08:08:08
// string
}
Timestamp
- Description: Get the second-level timestamp of the current object. Corresponding methods include
TimestampMicro/TimestampMilli/TimestampNano
. - Format:
func (t *Time) Timestamp() int64
func Timestamp() int64
- Example: Get the second-level timestamp of the current object.
func ExampleTime_Timestamp() {
t := gtime.Now()
fmt.Println(t.Timestamp())
fmt.Println(gtime.Timestamp())
fmt.Println(t.TimestampMicro())
fmt.Println(t.TimestampMilli())
fmt.Println(t.TimestampNano())
// Output:
// 1533686888
// 1533686888
// 1533686888000
// 1533686888000000
// 1533686888000000000
}
ToZone
- Description: Set timezone.
- Format:
func (t *Time) ToZone(zone string) (*Time, error)
- Example: Get the second-level timestamp of the current object.
func ExampleTime_ToZone() {
gt1 := gtime.Now()
gt2, _ := gt1.ToZone("Asia/Shanghai")
gt3, _ := gt1.ToZone("Asia/Tokyo")
fmt.Println(gt2)
fmt.Println(gt3)
// May Output:
// 2021-11-11 17:10:10
// 2021-11-11 18:10:10
}
SetTimeZone
- Description: Set timezone.
- Format:
func SetTimeZone(zone string) error
- Example: Set timezone.
func ExampleSetTimeZone() {
gtime.SetTimeZone("Asia/Shanghai")
fmt.Println(gtime.Datetime())
gtime.SetTimeZone("Asia/Tokyo")
fmt.Println(gtime.Datetime())
// May Output:
// 2018-08-08 08:08:08
// 2018-08-08 09:08:08
}
StrToTime
- Description: Convert time string to time object.
- Format:
func StrToTime(str string, format ...string) (*Time, error)
- Example: Convert time string to time object.
func ExampleStrToTime() {
res, _ := gtime.StrToTime("2006-01-02T15:04:05-07:00", "Y-m-d H:i:s")
fmt.Println(res)
// May Output:
// 2006-01-02 15:04:05
}
Add
- Description: Add time to the current time object.
- Format:
func (t *Time) Add(d time.Duration) *Time
- Example: Add time to the current time object.
func ExampleTime_Add() {
gt := gtime.New("2018-08-08 08:08:08")
gt1 := gt.Add(time.Duration(10) * time.Second)
fmt.Println(gt1)
// Output:
// 2018-08-08 08:08:18
}
StartOfDay
- Description: Return the start time object of today. Similar methods include
StartOfHalf/StartOfHour/StartOfMonth/StartOfMinute/StartOfQuarter
, etc. - Format:
func (t *Time) StartOfDay() *Time
- Example: Return the start time object of today.
func ExampleTime_StartOfDay() {
gt1 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.StartOfDay())
// Output:
// 2018-08-08 00:00:00
}
EndOfDay
- Description: Return the end time object of today. Similar methods include
EndOfHalf/EndOfHour/EndOfMonth/EndOfMinute/EndOfQuarter
, etc. - Format:
func (t *Time) EndOfDay() *Time
- Example: Return the end time object of today.
func ExampleTime_EndOfDay() {
gt1 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.EndOfDay())
// Output:
// 2018-08-08 23:59:59
}
Month
- Description: Return the month index in a year. For example, January corresponds to 1.
- Format:
func (t *Time) Month() int
- Example: Return the month index in a year.
func ExampleTime_Month() {
gt := gtime.New("2018-08-08 08:08:08")
t1 := gt.Month()
fmt.Println(t1)
// Output:
// 8
}
Second
- Description: Return the number of seconds in the current minute. For example, 10:10:08 corresponds to 8 seconds.
- Format:
func (t *Time) Second() int
- Example: Return the number of seconds in the current minute.
func ExampleTime_Second() {
gt := gtime.New("2018-08-08 08:08:08")
t1 := gt.Second()
fmt.Println(t1)
// Output:
// 8
}
IsZero
- Description: Check if the time equals
0001-01-01 00:00:00
. Note it does not represent the timestamp 0, which is1970-01-01 08:00:00
. - Format:
func (t *Time) IsZero() bool
- Example: Check if the month index in a year.
func ExampleTime_IsZero() {
gt := gtime.New("0-0-0")
fmt.Println(gt.IsZero())
// Output:
// true
}
AddDate
- Description: Add specified year, month, and day to the current time object.
- Format:
func (t *Time) AddDate(years int, months int, days int) *Time
- Example: Add specified year, month, and day to the current time object.
func ExampleTime_AddDate() {
var (
year = 1
month = 2
day = 3
)
gt := gtime.New("2018-08-08 08:08:08")
gt = gt.AddDate(year, month, day)
fmt.Println(gt)
// Output:
// 2019-10-11 08:08:08
}
Equal
- Description: Check if two time objects are equal.
- Format:
func (t *Time) Equal(u *Time) bool
- Example: Check if two time objects are equal.
func ExampleTime_Equal() {
gt1 := gtime.New("2018-08-08 08:08:08")
gt2 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.Equal(gt2))
// Output:
// true
}
Before
- Description: Determine the order of two time objects.
- Format:
func (t *Time) Before(u *Time) bool
- Example: Determine the order of two time objects.
func ExampleTime_Before() {
gt1 := gtime.New("2018-08-07 08:08:08")
gt2 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.Before(gt2))
// Output:
// true
}
After
- Description: Determine the order of two time objects.
- Format:
func (t *Time) After(u *Time) bool
- Example: Determine the order of two time objects.
func ExampleTime_After() {
gt1 := gtime.New("2018-08-07 08:08:08")
gt2 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.After(gt2))
// Output:
// false
}
Layout
- Description: Format output time.
- Format:
func (t *Time) Layout(layout string) string
- Example: Format output time.
func ExampleTime_Layout() {
gt1 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.Layout("2006-01-02"))
// Output:
// 2018-08-08
}
IsLeapYear
- Description: Check if it’s a leap year.
- Format:
func (t *Time) IsLeapYear() bool
- Example: Check if it’s a leap year.
func ExampleTime_IsLeapYear() {
gt1 := gtime.New("2018-08-08 08:08:08")
fmt.Println(gt1.IsLeapYear())
// Output:
// false
}
Date
- Description: Get the date.
- Format:
func Date() string
- Example: Get the date.
func ExampleDate() {
fmt.Println(gtime.Date())
// May Output:
// 2006-01-02
}
Datetime
- Description: Get the datetime.
- Format:
func Datetime() string
- Example: Get the datetime.
func ExampleDatetime() {
fmt.Println(gtime.Datetime())
// May Output:
// 2006-01-02 15:04:05
}
ISO8601
- Description: Return time in ISO8601 format.
- Format:
func ISO8601() string
- Example:
func ExampleISO8601() {
fmt.Println(gtime.ISO8601())
// May Output:
// 2006-01-02T15:04:05-07:00
}
RFC822
- Description: Return time in RFC822 format.
- Format:
func RFC822() string
- Example:
func ExampleRFC822() {
fmt.Println(gtime.RFC822())
// May Output:
// Mon, 02 Jan 06 15:04 MST
}
StrToTimeFormat
- Description:
StrToTimeFormat
returns time object based on the input time string and format. - Format:
func StrToTimeFormat(str string, format string) (*Time, error)
- Example:
func ExampleStrToTimeFormat() {
res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
fmt.Println(res)
// Output:
// 2006-01-02 15:04:05
}
StrToTimeLayout
- Description:
StrToTimeLayout
returns time object based on the input time string and format. - Format:
func StrToTimeLayout(str string, layout string) (*Time, error)
- Example:
func ExampleStrToTimeLayout() {
res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02")
fmt.Println(res)
// Output:
// 2018-08-08 00:00:00
}
MarshalJSON
- Description:
MarshalJSON
overrides the method injson.Marshal
. - Format:
func (t *Time) MarshalJSON() ([]byte, error)
- Example:
func ExampleTime_MarshalJSON() {
type Person struct {
Name string `json:"name"`
Birthday *gtime.Time `json:"birthday"`
}
p := new(Person)
p.Name = "goframe"
p.Birthday = gtime.New("2018-08-08 08:08:08")
j, _ := json.Marshal(p)
fmt.Println(string(j))
// Output:
// {"name":"xiaoming","birthday":"2018-08-08 08:08:08"}
}
UnmarshalJSON
- Description:
UnmarshalJSON
overrides the method injson.Unmarshal
. - Format:
func (t *Time) UnmarshalJSON() ([]byte, error)
- Example:
func ExampleTime_MarshalJSON() {
type Person struct {
Name string `json:"name"`
Birthday *gtime.Time `json:"birthday"`
}
p := new(Person)
p.Name = "goframe"
p.Birthday = gtime.New("2018-08-08 08:08:08")
j, _ := json.Marshal(p)
fmt.Println(string(j))
// Output:
// {"name":"xiaoming","birthday":"2018-08-08 08:08:08"}
}
WeekOfYear
- Description:
WeekOfYear
returns the current week number of the year, starting from 1. Similar methods includeDayOfYear/DaysInMonth
. - Format:
func (t *Time) WeeksOfYear() int
- Example:
func ExampleTime_WeeksOfYear() {
gt1 := gtime.New("2018-01-08 08:08:08")
fmt.Println(gt1.WeeksOfYear())
// Output:
// 2
}D