PaolaJ.
PaolaJ.

Reputation: 11552

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.

Upvotes: 2

Views: 20073

Answers (2)

FObersteiner
FObersteiner

Reputation: 25594

the format is pretty standard ISO8601, so you can use the time.RFC3339 layout, e.g.

t, e := time.Parse(time.RFC3339, "2009-01-01T01:02:01.111+02:00")

...and proceed with .UnixNano() as in thwd's answer. More predefined layouts can be found in src/time/format.go.

Upvotes: 1

thwd
thwd

Reputation: 24848

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.

Upvotes: 8

Related Questions