Parsing Incorrect Values When Reading Excel File in Go Language

97 views Asked by At

I'm working on a Go application where I need to parse dates from Excel files. The dates in Excel are in the format 27/06/1905, but when I use the Go excelize package to read, the date string value is 27-06-05, and the resulting date after format with time package is 27-06-2005, which doesn't match the Excel date.

I suspect this discrepancy is due to the default date format used by Excel and how the time package in Go interprets the 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:

go

package main

import (
    "fmt"
    "time"
)

func main() {
    // Open Excel file
    excelFile, _ := excelize.OpenFile(tc.filePath)

    // Creates a rows iterator for reading row by row
    rows, _ := excelFile.Rows("Asset Data")

    // Read the header row and convert to column map
    var cm columnMap
    if rows.Next() {
        headers, _ := rows.Columns()

        cm = convertExcelHeaderToColumnMap(headers)
    }

    // Read body
    rows.Next()
    row, err := rows.Columns()
    require.NoError(t, err)

    // value of row[DateOfGenerationIndex] is 27-06-05
    dateOfGeneration, err := time.Parse("01-02-06", row[DateOfGenerationIndex])

    fmt.Println(dateOfGeneration) // value of dateOfGeneration is 27-06-2005
}

I've tried adjusting the layout strings, but the resulting date doesn't match the Excel date.

0

There are 0 answers