十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.GO.dev/github.com/gogf/gf/v2/os/gtime

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网页空间、营销软件、网站建设、安平网站维护、网站推广。
New 创建并返回一个具有给定参数的 Time对象。func New(param ...interface{}) *Timefunc 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:08Now创建并返回一个当前时间对象。func Now() *Timefunc ExampleNow() {
	t := gtime.Now()
	fmt.Println(t)
	// Output:
	// 2021-11-06 13:41:08
}func (t *Time) Format(format string) stringfunc 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
}func (t *Time) String() stringfunc 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
}TimestampMicro/TimestampMilli/TimestampNano。func (t *Time) Timestamp() int64
func Timestamp() int64func 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
}func (t *Time) ToZone(zone string) (*Time, error)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
}func SetTimeZone(zone string) errorfunc 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
}func StrToTime(str string, format ...string) (*Time, error)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
}func (t *Time) Add(d time.Duration) *Timefunc 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
}StartOfHalf/StartOfHour/StartOfMonth/StartOfMinute/StartOfQuarter等。func (t *Time) StartOfDay() *Timefunc ExampleTime_StartOfDay() {
	gt1 := gtime.New("2018-08-08 08:08:08")
	fmt.Println(gt1.StartOfDay())
	// Output:
	// 2018-08-08 00:00:00
}EndOfHalf/EndOfHour/EndOfMonth/EndOfMinute/EndOfQuarter等。func (t *Time) EndOfDay() *Timefunc ExampleTime_EndOfDay() {
	gt1 := gtime.New("2018-08-08 08:08:08")
	fmt.Println(gt1.EndOfDay())
	// Output:
	// 2018-08-08 23:59:59
}func (t *Time) Month() intfunc ExampleTime_Month() {
	gt := gtime.New("2018-08-08 08:08:08")
	t1 := gt.Month()
	fmt.Println(t1)
	// Output:
	// 8
}func (t *Time) Second() intfunc ExampleTime_Second() {
	gt := gtime.New("2018-08-08 08:08:08")
	t1 := gt.Second()
	fmt.Println(t1)
	// Output:
	// 8
}0001-01-01 00:00:00。注意不代表时间戳为0, 时间戳为0是1970-01-01 08:00:00func (t *Time) IsZero() boolfunc ExampleTime_IsZero() {
	gt := gtime.New("0-0-0")
	fmt.Println(gt.IsZero())
	// Output:
	// true
}func (t *Time) AddDate(years int, months int, days int) *Timefunc 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
}func (t *Time) Equal(u *Time) boolfunc 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
}func (t *Time) Before(u *Time) boolfunc 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
}func (t *Time) After(u *Time) boolfunc 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
}func (t *Time) Layout(layout string) stringfunc ExampleTime_Layout() {
	gt1 := gtime.New("2018-08-08 08:08:08")
	fmt.Println(gt1.Layout("2006-01-02"))
	// Output:
	// 2018-08-08
}func (t *Time) IsLeapYear() boolfunc ExampleTime_IsLeapYear() {
	gt1 := gtime.New("2018-08-08 08:08:08")
	fmt.Println(gt1.IsLeapYear())
	// Output:
	// false
}func Date() stringfunc ExampleDate() {
	fmt.Println(gtime.Date())
	// May Output:
	// 2006-01-02
}func Datetime() stringfunc ExampleDatetime() {
	fmt.Println(gtime.Datetime())
	// May Output:
	// 2006-01-02 15:04:05
}func ISO8601() stringfunc ExampleISO8601() {
	fmt.Println(gtime.ISO8601())
	// May Output:
	// 2006-01-02T15:04:05-07:00
}func RFC822() stringfunc ExampleRFC822() {
	fmt.Println(gtime.RFC822())
	// May Output:
	// Mon, 02 Jan 06 15:04 MST
}StrToTimeFormat根据传入的时间字符串以及格式返回时间对象func StrToTimeFormat(str string, format string) (*Time, error)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根据传入的时间字符串以及格式返回时间对象func StrToTimeLayout(str string, layout string) (*Time, error)func ExampleStrToTimeLayout() {
	res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02")
	fmt.Println(res)
	// Output:
	// 2018-08-08 00:00:00
}MarshalJSON重载json.Marshal中的方法。func (t *Time) MarshalJSON() ([]byte, error)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重载json.Unmarshal中的方法。func (t *Time) UnmarshalJSON() ([]byte, error)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返回当前周处于全年第几周,从1开始计算。类似的还有DayOfYear/DaysInMonthfunc (t *Time) WeeksOfYear() intfunc ExampleTime_WeeksOfYear() {
	gt1 := gtime.New("2018-01-08 08:08:08")
	fmt.Println(gt1.WeeksOfYear())
	// Output:
	// 2
}