go - Go中不正确的时间转换

标签 go time

我正在尝试将时间字符串“2020-02-01T12:30:00 + 01:00”(来自Google日历API)转换为time.Go中的时间格式,由于某种原因,它一直给我“2020-01- 01 12:30:00 +0000 UTC”作为输出(这是一月的第一天,而不是二月的第一天)。知道我在做什么错吗?

提前致谢!

package main

import (
    "fmt"
    "time"
    "log"
)

func main() {

    input := "2020-02-01T12:30:00+01:00"
    output, err := StrToTime(input)
    if err != nil{
        log.Fatal(err)
    }

    fmt.Println(output)


}

func StrToTime(strDateTime string) (time.Time, error) {

    layout := "2006-01-02T15:04:05+01:00"
    t, err := time.Parse(layout, strDateTime)

    if err != nil {
        return time.Time{}, fmt.Errorf("could not parse datetime: %v", err)
    }

    return t, nil

}

最佳答案

发生这种情况是因为您指定的时间偏移部分错误,应该是-07:00而不是+01:00

到目前为止,它第二次将01视为月份部分,并将最初正确解析的02覆盖为01(但不是从输入的时间偏移部分开始)。

关于go - Go中不正确的时间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048724/

相关文章:

image - 在 Golang 中比较 base64 图像字符串

go - 在 VS Code 中调试项目时出现错误的 gopath

pointers - 范围内的 golang 指针不起作用

pointers - 时间.时间 : pointer or value

go - golang中的时间格式-DayOfyearTHour:Minute.000Z

java - 字符串时间到实际时间

unix - Unix上的文件修改时间(秒)

date - 在erlang中将时间戳转换为日期时间

amazon-web-services - 通过 Go SDK (Amazon S3) 从 Bucket 生成 Torrent

go - makefile 中的 protoc 以构建 protobuf