golang 时间计算

原创
2018/01/22 11:22
阅读数 859
package test

import (
	"testing"
	"time"
	"fmt"
)

func Test_time(t *testing.T)  {
	now := time.Now()
	fmt.Println("当前时间:", now)
	//两小时之后的时间
	h,_ := time.ParseDuration("2h")
	nowAfter2Hour := now.Add(h)
	fmt.Println("两小时之后的时间:", nowAfter2Hour)

	//两小时之前的时间
	negativeH,_ := time.ParseDuration("-2h")
	nowBefore2Hour := now.Add(negativeH)
	fmt.Println("两小时之前的时间:", nowBefore2Hour)

	//十分钟之后的时间
	m,_ := time.ParseDuration("10m")
	nowAfter10Minute := now.Add(m)
	fmt.Println("十分钟之后的时间:", nowAfter10Minute)

	//十分钟之前的时间
	negativeM,_ := time.ParseDuration("-10m")
	nowBefore10Minute := now.Add(negativeM)
	fmt.Println("十分钟之前的时间:", nowBefore10Minute)

	//30s之后的时间
	s,_ :=  time.ParseDuration("30s")
	nowAfter30Second := now.Add(s)
	fmt.Println("30s之后的时间:", nowAfter30Second)

	//30s之前的时间
	negativeS,_ := time.ParseDuration("-30s")
	nowBefore30Second := now.Add(negativeS)
	fmt.Println("30s之前的时间:", nowBefore30Second)

	//1年1个月1天 之后的时间
	fmt.Println("1年2个月3天之后的时间:", now.AddDate(1, 2, 3))

	//2年2个月2天之前的时间
	fmt.Println("2年3个月4天之前的时间:", now.AddDate(-2, -3, -4))

}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部