码迷,mamicode.com
首页 > 编程语言 > 详细

GO语言常用标准库03---time包

时间:2019-11-14 00:05:02      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:print   fun   UNC   cat   parse   cst   常用   nose   sub   

package main

import (
	"fmt"
	"time"
)

func main021() {
	nowTime := time.Now()
	fmt.Println(nowTime.Year())//2018
	fmt.Println(nowTime.Month())//December
	fmt.Println(nowTime.Day())//25
	fmt.Println(nowTime.Date())//2018 December 25
	fmt.Println(nowTime.Hour())//10
	fmt.Println(nowTime.Minute())//26
	fmt.Println(nowTime.Second())//?
	fmt.Println(nowTime.Nanosecond())//?
	fmt.Println(nowTime.Weekday())//Tuesday
	fmt.Println(nowTime.YearDay())//359

	//定义2019年元旦的Time对象
	yuandanTime := time.Date(2019, time.January, 1, 0, 0, 0, 0, time.Now().Location())
	fmt.Println(yuandanTime) //2019-01-01 00:00:00 +0800 CST

	//计算当前时间距离2019年元旦的时间差
	duration := nowTime.Sub(yuandanTime)
	fmt.Println(duration)//-157h19m31.9387253s

	//解析获得时间差Duration对象
	duration, _ = time.ParseDuration("157h19m31.9387253s")

	//将当前时间时间加上时间差获得新的时间对象
	newTime := nowTime.Add(duration)
	fmt.Println("newTime=",newTime)

}

/*计算中国成为大一统国家的历史长度*/
func main() {
	duration := time.Now().Sub(time.Date(-211, time.January, 1, 0, 0, 0, 0, time.Now().Location()))
	fmt.Println(duration.String())

	time.Now().Unix()
}

  

GO语言常用标准库03---time包

标签:print   fun   UNC   cat   parse   cst   常用   nose   sub   

原文地址:https://www.cnblogs.com/yunweiqiang/p/11854126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!