2

I have the following date string: 2017-09-04T04:00:00Z

I need to parse this string into a golang time in order to have uniform data across my application. Here is the code so far:

parsedTime := "2017-09-04T04:00:00Z"
test, err := time.Parse(time.RFC3339, parsedTime)
check(err)
fmt.Println(test)

I get the following error when I try to run the program:

": extra text: 0:00 +0000 UTC parsing time "2017-09-04T04:00:00Z

How can I either add the extra text that it is looking for or get the parser to stop looking after the Z?

I have also tried the following:

parsedTime := "2017-09-04T04:00:00Z"
test, err := time.Parse("2006-01-02T03:04:05Z", parsedTime)
check(err)
fmt.Println(test)

Which returns the following error:

": extra text: 017-09-04T04:00:00Z
2
  • 1
    Your second example seems to work fine Commented Mar 31, 2016 at 2:19
  • 1
    I'm not sure this is a complete example... both formats seem to work fine for me with go1.6 Commented Mar 31, 2016 at 5:04

1 Answer 1

1

Both formats you used work with the current version of go: https://play.golang.org/p/Typyq3Okrd

var formats = []string{
    time.RFC3339,
    "2006-01-02T03:04:05Z",
}

func main() {
    parsedTime := "2017-09-04T04:00:00Z"

    for _, format := range formats {
        if test, err := time.Parse(format, parsedTime); err != nil {
            fmt.Printf("ERROR: format %q resulted in error: %v\n", format, err)
        } else {
            fmt.Printf("format %q yielded %s\n", format, test)
        }
    }
}

Can you provide a working example that demonstrates your problem? You can use the go playground for shareable snippets.

Sign up to request clarification or add additional context in comments.

1 Comment

The "T04" part of the partedTime string will generate an error when using 24hr time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.