Skip to content

not support 24-hour time by getValueFrom() #163

@PineTreeWild

Description

@PineTreeWild

The value in cell B2 is 00:30:00, but the value parsed is 12:30:00.
cell := xlsx.GetCellValue("Sheet1", "B2")

I take a try to fix it as follows, but 12-hour time is not supported in this way.

func parseTime(i int, v string) string {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return v
}
val := timeFromExcelTime(f, false)
format := builtInNumFmt[i] ===========>>h:mm:ss

    replacements := []struct{ xltime, gotime string }{
            {"yyyy", "2006"},
            {"yy", "06"},
            {"mmmm", "%%%%"},
            {"dddd", "&&&&"},
            {"dd", "02"},
            {"d", "2"},
            {"mmm", "Jan"},
            {"mmss", "0405"},
            {"ss", "05"},
            {"hh", "15"},
            {"h", "3"},  ===========>>{"h", "15"}
            {"mm:", "04:"},
            {":mm", ":04"},
            {"mm", "01"},
            {"am/pm", "pm"},
            {"m/", "1/"},
            {"%%%%", "January"},
            {"&&&&", "Monday"},
    }
    for _, repl := range replacements {
	format = strings.Replace(format, repl.xltime, repl.gotime, 1)
}
// If the hour is optional, strip it out, along with the possible dangling
// colon that would remain.
if val.Hour() < 1 {    ==============>>val.Hour() is 0 
	format = strings.Replace(format, "]:", "]", 1)
	format = strings.Replace(format, "[3]", "", 1)
	format = strings.Replace(format, "[15]", "", 1)
} else {
	format = strings.Replace(format, "[3]", "3", 1)
	format = strings.Replace(format, "[15]", "15", 1)
}
    return val.Format(format)

}

The xlsx file is attached. If it is a new issue, can you give a formal workaround for it? Thanks.
time.xlsx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions