【问题标题】:How to parse this date 2018-10-22T2250?如何解析这个日期 2018-10-22T2250?
【发布时间】:2019-10-17 17:46:31
【问题描述】:

如何在 golang 中解析这个奇怪的日期时间 2018-10-22T2250? 我找不到日期布局

【问题讨论】:

  • golang.org/pkg/time/#Parse - 你试过什么?
  • 我从另一个服务收到了这个时间戳,但我无法解析它
  • 这不能回答问题。你试过什么?您必须包含您需要帮助调试的代码。

标签: go time timestamp


【解决方案1】:

您可以创建自己的自定义格式。在生产中,您还应该处理错误。

package main

import (
    "fmt"
    "time"
)

func main() {
    timeString := "2018-10-22T2250"
    timeFormat := "2006-01-02T1504"

    t, _ := time.Parse(timeFormat, timeString)

    fmt.Println(t)
}

Playground link

这将返回 UTC 时间。您可能需要调整到另一个时区,具体取决于您的来源。

//init the location
loc, _ := time.LoadLocation("Asia/Shanghai")
//localize the time
localTime := t.In(loc)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多