Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 470
PaolaJ. Avatar asked Dec 06 '25 04:12

PaolaJ.


1 Answers

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.

like image 154
thwd Avatar answered Dec 07 '25 18:12

thwd