GoLang 获取两个时间相差多少小时

代码如下

如果调用 time .parse() 这个玩意相差 8小时。默认时区是utc 

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(getHourDiffer("2016-09-10 13:00:00", "2016-09-10 14:50:00"))
}

//获取相差时间
func getHourDiffer(start_time, end_time string) int64 {
	var hour int64
	t1, err := time.ParseInLocation("2006-01-02 15:04:05", start_time, time.Local)
	t2, err := time.ParseInLocation("2006-01-02 15:04:05", end_time, time.Local)
	if err == nil && t1.Before(t2) {
		diff := t2.Unix() - t1.Unix() //
		hour = diff / 3600
		return hour
	} else {
		return hour
	}
}

    原文作者:一名路过的小码农
    原文地址: https://blog.csdn.net/liangguangchuan/article/details/52494586
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞