type Time struct { // sec gives the number of seconds elapsed since // January 1, year 1 00:00:00 UTC. sec int64 // nsec specifies a non-negative nanosecond // offset within the second named by Seconds. // It must be in the range [0, 999999999]. nsec int32 // loc specifies the Location that should be used to // determine the minute, hour, month, day, and year // that correspond to this Time. // Only the zero Time has a nil Location. // In that case it is interpreted to mean UTC. loc *Location }
// A Month specifies a month of the year (January = 1, ...). type Month int
// A Duration represents the elapsed time between two instants // as an int64 nanosecond count. The representation limits the // largest representable duration to approximately 290 years. type Duration int64
相关函数
time.Now()
time.Now返回当前时间,返回类型是Time, 该返回值是操作时间的关键。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
now := time.Now() fmt.Println(now.Year()) //Month 函数返回的类型是:Month(其实是int) //type Month int // String returns the English name of the day ("Sunday", "Monday", ...). func(d Weekday)String()string { return days[d] } fmt.Println(now.Month()) //Month类型实现了String方法,在fmt输出时会转为数字对应的string类型的表示形式 // month := now.Month() fmt.Println(int(month)) //强制转为int,则会输出数字月份 fmt.Println(now.Day()) fmt.Println(now.Hour()) fmt.Println(now.Minute()) fmt.Println(now.Second()) fmt.Println(now.Unix()) fmt.Println(now.UnixNano())