time包简介 #
时间函数
包名:time
时间结构体:time.Time
年-》月-》日-》时-》分-》秒-》毫秒-》微秒-》纳秒-》皮秒-》飞秒
layout:
const TIME_FORMAT = "2006-01-02 15:04:05"
const TIME_FORMAT_1 = "2006-01-02"
const TIME_FORMAT_2 = "2006.01.02"
const TIME_FORMAT_3 = "2006年01月02日"
const TIME_FORMAT_4 = "2006"
const TIME_FORMAT_5 = "200601"
const TIME_FORMAT_6 = "20060102"
const TIME_FORMAT_7 = "01" //月
const TIME_FORMAT_8 = "02" //日
time包常用函数 #
func Now() Time 获取当前时间,返回time.Time结构体对象
now := time.Now() //获取当前时间,返回time.Time结构体对象
fmt.Printf("当前时间:%v\n", now) //当前时间:2019-09-22 19:41:10.469433 +0800 CST m=+0.001616876
year := now.Year() //年,返回int型
month := now.Month() //月
day := now.Day() //日
hour := now.Hour() //小时
minute := now.Minute() //分钟
second := now.Second() //秒
//格式化方式1:2019-09-22 19:41:10
fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
//格式化方式2: 2019-09-22 19:41:10
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
func Unix(sec int64, nsec int64) Time 时间戳+纳秒转time.Time结构体对象,第1个入参为时间戳,第2个入参为纳秒
func Until(t Time) Duration //返回time.Duration类型,type Duration int64
func Since(t Time) Duration
func Parse(layout, value string) (Time, error)
func ParseDuration(s string) (Duration, error)
func ParseInLocation(layout, value string, loc *Location) (Time, error)
func Sleep(d Duration) //睡眠时间
示例:
time.Sleep(2 * time.Second ) //睡眠2秒
func After(d Duration) <-chan Time
func AfterFunc(d Duration, f func()) *Timer
func NewTimer(d Duration) *Timer
func NewTicker(d Duration) *Ticker
func Tick(d Duration) <-chan Time
func LoadLocation(name string) (*Location, error) 加载时区
示例: loc, err := time.LoadLocation("Asia/Shanghai")
time.Time结构体支持的函数 #
func (t Time) UTC() Time //time.Time结构体对象转utc格式,返回time.Time结构体对象
func (t Time) Local() Time
func (t Time) In(loc *Location) Time
func (t Time) Location() *Location
func (t Time) Zone() (name string, offset int)
func (t Time) Year() int 返回年
func (t Time) Month() Month 返回月,type Month int
func (t Time) Day() int 返回日
func (t Time) Hour() int 返回时
func (t Time) Minute() int 返回分
func (t Time) Second() int 返回秒
func (t Time) Nanosecond() int 返回纳秒
func (t Time) YearDay() int 返回一年中的第几天
func (t Time) Weekday() Weekday 返回星期,type Weekday int
示例:fmt.Println(time.Now().Weekday().String()) //Thursday表周四
func (t Time) Unix() int64 返回时间戳,单位秒
示例:
now := time.Now() //获取当前时间
t1 := now.Unix() //时间戳,到秒
fmt.Printf("时间戳:%v\n", t1) //时间戳:1569152644
timestamp := time.Now().Unix() //当前时间戳,返回int64类型,连贯写法
func (t Time) UnixNano() int64 返回纳秒时间戳
示例:
now := time.Now() //获取当前时间
t2 := now.UnixNano() //纳秒时间戳
fmt.Printf("纳秒时间戳:%v\n", t2) //纳秒时间戳:1569152644314431000
func (t Time) MarshalBinary() ([]byte, error)
func (t *Time) UnmarshalBinary(data []byte) error
func (t Time) GobEncode() ([]byte, error)
func (t *Time) GobDecode(data []byte) error
func (t Time) MarshalJSON() ([]byte, error)
func (t *Time) UnmarshalJSON(data []byte) error
func (t Time) MarshalText() ([]byte, error)
func (t *Time) UnmarshalText(data []byte) error
func (t Time) Format(layout string) string
示例:
a1:=time.Now().Format("2006-01-02 15:04:05") //2019-11-28 10:33:53
a2:=time.Now().Format("2006/01/02 15:04:05") //2019/11/28 10:36:26
a3:=time.Now().Format("01/02/2006 15:04:05") //11/28/2019 10:37:21
a4:=time.Now().Format("2006年01月02日") //2019年11月28日
func (t Time) AppendFormat(b []byte, layout string) []byte
func (t Time) String() string 时间转字符串
更多时间文档: https://golang.google.cn/pkg/time/
time.Duration类型支持的方法 #
类型别名: type Duration int64
支持的方法:
func (d Duration) Hours() float64 时
func (d Duration) Minutes() float64 分
func (d Duration) Seconds() float64 秒
func (d Duration) Milliseconds() int64 毫秒
func (d Duration) Microseconds() int64 微秒
func (d Duration) Nanoseconds() int64 纳秒
func (d Duration) Round(m Duration) Duration
func (d Duration) String() string
func (d Duration) Truncate(m Duration) Duration
内置时间单位常量 #
type Duration int64
const (
Nanosecond Duration = 1 //纳秒
Microsecond = 1000 * Nanosecond //毫秒
Millisecond = 1000 * Microsecond //微秒
Second = 1000 * Millisecond //秒
Minute = 60 * Second //分
Hour = 60 * Minute //时
)
时间示例 #
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("小于12点")
default:
fmt.Println("大于12点")
}
}
定时器 #