去time.Parse()得到“月度超出范围”错误

时间:2017-04-03 04:24:56

标签: go time format

我是Go的新手,我正在解析一个nginx时间格式字符串。你可以在这里查看我的代码:



package main

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

func main(){
	//nginx time format
	nginx_time :="03/Apr/2017:08:29:05 +0800"
	t,err:=time.Parse("02/Jan/2016:15:04:05 MST",nginx_time)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(t.Format("2006-01-02 15:04:05"))
}




我收到以下错误:



GOROOT=/usr/local/go
GOPATH=/usr/local/share/go:/Users/sinopex/goroot:/Users/sinopex/goroot
/usr/local/go/bin/go build -o "/private/var/folders/5b/yf1f_9lj06bfvqvcq2h9myph0000gn/T/Build 1.go and rungo" /Users/sinopex/goroot/src/github.com/sinopex/golang/example/test/1.go
"/private/var/folders/5b/yf1f_9lj06bfvqvcq2h9myph0000gn/T/Build 1.go and rungo"
2017/04/03 12:17:12 parsing time "03/Apr/2017:08:29:05 +0800": month out of range
http://stackoverflow.com/questions/ask#
Process finished with exit code 1




关于我做错的任何想法?

谢谢。

1 个答案:

答案 0 :(得分:7)

您的解析格式与参考格式和输入格式在2个地方不匹配:

  1. 参考格式中的年份应为@Component({ ... encapsulation: ViewEncapsulation.None })

  2. 时区偏移量由数字指定,而不是按时区名称指定:2006

  3. 所以:

    -0700

    play.golang:https://play.golang.org/p/enJY0VBt3a