go

How to convert ISO 8601 time in golang?


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