golang之时间

1.获取时间戳

t := time.Now().Unix()   //1556952234

2.获取当前日期

time.Now().Format("2006-01-02 15:04:05")    //2019-05-04 14:43:54

3.日期转时间戳

loc, _ := time.LoadLocation("Local")
	timeFormat, err := time.ParseInLocation("2006-01-02 15:04:05", "2019-05-04 00:32:31", loc)
	if err == nil {
		unixTime := timeFormat.Unix()
		fmt.Println(unixTime)   //1556901151
	}

4.时间戳转日期

	dataTimeStr := time.Unix(1556901151, 0).Format("2006-01-02 15:04:05")
	fmt.Println(dataTimeStr)   //2019-05-04 00:32:31

5.获取年月日

	year := time.Now().Year()
	day := time.Now().Day()
	fmt.Println(year, day)   //2019 4

 

你可能感兴趣的:(golang)