读取excel,int 数字时间转时间

golang解析excel的时候,会发现日期时间都变成了 数字,但在excel中显示是正常的。

原因

excel中的日期按照他自有的纪元存储。以 1899年12月30日0时0分0秒UTC为纪元。

解决办法

转换

func ExcelIntDate(dateStr string) (dt time.Time, err error) {
	var dateValue float64
	matched, err := regexp.MatchString(`^\d+$`, dateStr)
	if err != nil {
		return
	}

	if !matched {
		err = errors.New("not excel time")
		return
	}

	dateValue, err = strconv.ParseFloat(dateStr, 64)
	if err != nil {
		return
	}
	epoch := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC) // UTC 1899/12/30 00:00:00
	dt = epoch.Add(time.Duration(dateValue) * 24 * time.Hour)
	return
}

测试

	var dateStr string
	dateStr = "44666" // 2022-04-15 00:00:00 +0000 UTC
	dateStr = "44621" // 2022-03-01 00:00:00 +0000 UTC
	fmt.Println(ExcelIntDate(dateStr))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值