Search code examples
datetimegounix-timestamp

Convert formatted time to UTC milliseconds


How to convert time in format

2009-01-01T01:02:01.111+02:00

to UTC in milliseconds?

Is there already package for this conversion? I looked at the https://golang.org/src/time/format.go but couldn't find same format to convert.


Solution

  • Use time.Parse.

    Demo: http://play.golang.org/p/ouiDtIVjQI

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t, e := time.Parse(`2006-01-02T15:04:05.000-07:00`, `2009-01-01T01:02:01.111+02:00`)
    
        if e != nil {
            panic(e)
        }
    
        fmt.Println(t.UTC().UnixNano() / 1000000)
    }
    

    Use the format string 2006-01-02T15:04:05.000-07:00 for the reference date.