xtime.go 607 字节
package xtime

import (
	"math"
	"time"
)

// 当天的末尾时间, 例 2006-01-02 23:59:59
func DayEndTime(t time.Time) time.Time {
	y, m, d := t.Local().Date()
	t2 := time.Date(y, m, d, 23, 59, 59, 0, time.Local)
	return t2
}

// 当天的0点时间 ,例 2006-01-02 00:00:00
func DayZeroTime(t time.Time) time.Time {
	y, m, d := t.Local().Date()
	t2 := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
	return t2
}

// 两个时间相差多少天
func SubDayAbs(t1, t2 time.Time) int {
	day1 := DayZeroTime(t1)
	day2 := DayZeroTime(t2)

	t3 := day1.Sub(day2)
	h := int(math.Abs(t3.Hours()))
	return (h / 24)
}