golang时间日期处理

时间处理
import “time”
时间戳

time.Now().Unix()

时间格式化

time.Now().Format("2006-01-02 15:04:05")

时间点必须为2006-01-02 15:04:05, go语言诞生之日

时间戳转字符串格式化时间

str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")
fmt.Println(str_time)

字符串格式化时间转时间戳

the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")
if err == nil {
    unix_time := the_time.Unix()
	fmt.Println(unix_time)		
}

字符串格式化时间转时间戳

the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")
if err == nil {
    unix_time := the_time.Unix()
	fmt.Println(unix_time)		
}

检查两个时间对象是否在同一日期

func DateEqual(t1 time.Time, t2 time.Time) bool {
	y1, m1, d1 := t1.Date()
	y2, m2, d2 := t2.Date()
	return y1 == y2 && m1 == m2 && d1 == d2
}

func DateEqual(date1, date2 time.Time) bool {
    return date1.Format("20060102") == date2.Format("20060102")
}

// 计算超过工作日的天数
func CalCulateOutWorkDay(startDate string, endDate string) int {
	start, _ := time.Parse("2006-01-02", startDate)
	end, _ := time.Parse("2006-01-02", endDate)
	var currentTime = start
	var workCount int
	for {
		if currentTime.After(end) {
			break
		}
		if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {

		} else {
			workCount++
		}
		currentTime = currentTime.Add(24 * time.Hour)
	}
	return workCount
}
https://blog.csdn.net/elecjun/article/details/123255478

https://zhuanlan.zhihu.com/p/383913108

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值