The read date string not match with value display in Excel file

106 views Asked by At

I use Go language to parse dates from Excel files. The dates in Excel display 23/03/2018, but when I use the Go excelize package to read, the date string value is 03-23-18, which doesn't match the Excel date.

Can someone help me understand how to correctly parse Excel date formats in Go?

I use go 1.19 and github.com/xuri/excelize/v2 v2.7.1

Here's a simplified version of my code:

package main

import (
    "fmt"
    "time"
    "log"
    
    "github.com/xuri/excelize/v2"
)

func main() {
    // Open Excel file
    excelFile, err := excelize.OpenFile(tc.filePath)
    if err != nil {
        log.Panic(err)
    }

    // Creates a rows iterator for reading row by row
    rows, err := excelFile.Rows("Asset Data")
    if err != nil {
        log.Panic(err)
    }

    // Read the header row and convert to column map
    cm := make(map[string]int)
    if rows.Next() {
        headers, err := rows.Columns()
        if err != nil {
            log.Panic(err)
        }
        
        for idx, h := ranger headers {
            cm[h] = idx
        }
    }

    // Read body
    rows.Next()
    row, err := rows.Columns()
    if err != nil {
        log.Panic(err)
    }

    // the row["date of inspection"] display 23/03/2018 in Excel file
    dateOfInspection, err := time.Parse("02/01/2006", row["date of inspection"])
    if err != nil {
        log.Panic(err)
    }
    
    fmt.Println(dateOfInspection)
}

And this code show error parsing time "03-23-18": month out of range.

This is the Excel column and this column had format Date, the excelize can get correct value at 5 rows first, but wrong with the rest

Values of date of inspection column in Excel file

And this is column display when I change column format to General:

Values of date of inspection column in Excel file in General format

0

There are 0 answers