HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How to convert ISO 8601 time in golang?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howtimeconvert8601golangiso

Problem

What is the equivalent code in golang for the following shell command ?
date -u +%Y-%m-%dT%T%z

Solution

If you're looking for a simple, but not perfect solution consider using time.RFC3339 constant. But also know that there are differences between ISO8601 which are too complex for this answer.

See https://ijmacd.github.io/rfc3339-iso8601/ for differences and also has a handy test file generator to show differences. There is also a good discussion on SO here What's the difference between ISO 8601 and RFC 3339 Date Formats?

package main

import (
    "time"
    "fmt"
)

func main(){
    fmt.Println(time.Now().Format(time.RFC3339))
}


golang Time.Format

Code Snippets

package main

import (
    "time"
    "fmt"
)

func main(){
    fmt.Println(time.Now().Format(time.RFC3339))
}

Context

Stack Overflow Q#35479041, score: 284

Revisions (0)

No revisions yet.