master
huanghai 5 years ago
parent b7f87b1d24
commit 13cfef29fb

@ -0,0 +1,15 @@
package main
import (
"dsBaseRpc/Utils/DateUtil"
"fmt"
)
func main(){
s:=DateUtil.ConvertToFormatDay("66666")
fmt.Println(s)
s=DateUtil.ConvertToFormatDay("10000")
fmt.Println(s)
}

@ -1,6 +1,9 @@
package DateUtil
import "time"
import (
"strconv"
"time"
)
/**
@ -23,6 +26,12 @@ func ConvertDateTime(ts string) time.Time {
}
func ConvertDate(ts string) time.Time {
//如果是整数并且是5位的
_, err := strconv.Atoi(ts)
if err == nil { //如果可以转为整数那么可能是距离1900-01-01的天数
return ConvertDateTime(ConvertToFormatDay(ts))
}
//如果是带时分秒的
if len(TimeLayoutStr) == len(ts) {
return ConvertDateTime(ts)
@ -35,8 +44,17 @@ func ConvertDate(ts string) time.Time {
2020-08-18
*/
*/
func CheckDateStr(s string) bool {
//转一下整数
dateInt, err := strconv.Atoi(s)
if err == nil { //如果可以转为整数那么可能是距离1900-01-01的天数
//能转成整数那么是不是在10000--->66666,代表1927-05-18--->2082-07-09
if dateInt < 10000 || dateInt > 66666 {
return false
}
return true
}
//这种情况下time.Parse会转成时间2014-03-01 00:00:00有一个办法是转换后如果没有报错你再Format
//跟原来的的对比一下,如果不同,那就可以是说是错误的。
t, err := time.Parse("2006-01-02", s)
@ -49,3 +67,21 @@ func CheckDateStr(s string) bool {
return false
}
}
// excel日期字段格式化 yyyy-mm-dd
func ConvertToFormatDay(excelDaysString string) string {
// 2006-01-02 距离 1900-01-01的天数
baseDiffDay := 38719 //在网上工具计算的天数需要加2天什么原因没弄清楚
curDiffDay := excelDaysString
b, _ := strconv.Atoi(curDiffDay)
// 获取excel的日期距离2006-01-02的天数
realDiffDay := b - baseDiffDay
//fmt.Println("realDiffDay:",realDiffDay)
// 距离2006-01-02 秒数
realDiffSecond := realDiffDay * 24 * 3600
//fmt.Println("realDiffSecond:",realDiffSecond)
// 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
baseOriginSecond := 1136185445
resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
return resultTime
}

Loading…
Cancel
Save