go语言计算两个时间的时间差方法

WBOY
Release: 2016-06-06 11:22:45
Original
2751 people have browsed it

本文实例讲述了go语言计算两个时间的时间差方法。分享给大家供大家参考。具体分析如下:

go语言计算两个时间的时间差,代码很简单,返回1天前、1周前还是1月前的时间

package main
import (
  "fmt"
  "time"
)
func main() {
//Add方法和Sub方法是相反的,获取t0和t1的时间距离d是使用Sub
//将t0加d获取t1就是使用Add方法
  k := time.Now()
  //一天之前
  d, _ := time.ParseDuration("-24h")
  fmt.Println(k.Add(d))
  //一周之前
  fmt.Println(k.Add(d * 7))
  //一月之前
  fmt.Println(k.Add(d * 30))
}
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!