dongshan9619 2016-10-22 17:59
浏览 294
已采纳

Golang-解析YYYY-MM-DD日期的日期

Hi so I can't seem to find anything to help me on this.

I am using the format string "January 02, 2006" and the time string "2016-07-08"

However when I run a format using these parameters the response I get is July 7th, 2016. The correct response would be July 8th, 2016.

As a note, I am also trying to use this via Sprig.

{{ date "January 02, 2006" .MyDate }}

If I can get any assistance it would be much appreciated

  • 写回答

2条回答 默认 最新

  • dongshiru5913 2016-10-22 19:33
    关注

    It's because of the time zone, you're getting the right date, but sprig formats to "Local" by default, where golang.org/pkg/time defaults to "UTC"

    Here is a sample code: (omitting error handling for simplicity)

    func main() {
        // using the "time" package
        mydate, _ := time.Parse("2006-01-02", "2016-07-08")
        fmt.Println("time:", mydate.In(time.Local).Format("January 02, 2006 (MST)"), "-- specify Local time zone")
        fmt.Println("time:", mydate.Format("January 02, 2006 (MST)"), "-- defaults to UTC")
    
        d := struct{ MyDate time.Time }{mydate}
    
        //using sprig
        fmap := sprig.TxtFuncMap()
        localTpl := `sprig: {{ date "January 02, 2006 (MST)" .MyDate }} -- defaults to Local`
        t := template.Must(template.New("test").Funcs(fmap).Parse(localTpl))
        var localdate bytes.Buffer
        t.Execute(&localdate, d)
        fmt.Println(localdate.String())
    
        utcTpl := `sprig: {{ dateInZone "January 02, 2006 (MST)" .MyDate "UTC"}} -- specify UTC time zone`
        t = template.Must(template.New("test").Funcs(fmap).Parse(utcTpl))
        var utcdate bytes.Buffer
        t.Execute(&utcdate, d)
        fmt.Println(utcdate.String())
    
    }
    

    Output:

    time:  July 07, 2016 (EDT) -- specify Local time zone                                                                                  
    time:  July 08, 2016 (UTC) -- defaults to UTC                                                                                          
    sprig: July 07, 2016 (EDT) -- defaults to Local                                                                                        
    sprig: July 08, 2016 (UTC) -- specify UTC time zone  
    

    Here is some reference:

    time: https://golang.org/pkg/time

    In the absence of a time zone indicator, Parse returns a time in UTC.

    spig: https://github.com/Masterminds/sprig/blob/master/functions.go#L407

    func date(fmt string, date interface{}) string {
        return dateInZone(fmt, date, "Local")
    }
    

    Note: if you want to format to a specific time zone, look at the 2nd template:

    utcTpl := `sprig: {{ dateInZone "January 02, 2006 (MST)" .MyDate "UTC"}} -- specify UTC time zone`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)