当前位置:网站首页>Read excel, int digital time to time

Read excel, int digital time to time

2022-04-23 17:59:00 User nickname cannot be empty

golang analysis excel When , You will find that the date and time have become Numbers , But in excel The display in is normal .

reason

excel The dates in are stored according to his own era . With 1899 year 12 month 30 Japan 0 when 0 branch 0 second UTC For the era .

terms of settlement

transformation

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
}

test

	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))

版权声明
本文为[User nickname cannot be empty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231757283909.html