kpratihast
kpratihast

Reputation: 872

How to convert RFC3339 format to ISO8601 in golang?

I have a timestamp which I am getting using time.Now().Format(time.RFC3339). The format is 2018-10-17T07:26:33Z However, I want the format in ISO 8601: 2018-10-17T07:26:33.000Z

How do I get those extra milliseconds in the end ?

Upvotes: 5

Views: 10129

Answers (3)

Vorsprung
Vorsprung

Reputation: 34447

make a custom layout as shown below

package main

import (
    "fmt"
    "time"
)

func main() {
    t1, e := time.Parse(
        time.RFC3339,
        "2018-10-17T07:26:33Z")
    if e != nil {
        fmt.Println(e)
    }
    //2018-10-17T07:26:33.000Z required
    //Layouts must use the reference time Mon Jan 2 15:04:05 MST 2006
    fmt.Println(t1.Format("2006-01-02T15:04:05.000Z"))

}

playground link (good idea Sunny) https://play.golang.org/p/Y3II7lGZB-D

Upvotes: 8

tharinduwijewardane
tharinduwijewardane

Reputation: 2863

time.Now().Format("2006-01-02T15:04:05.000Z")

Upvotes: 3

Mohammad.Naser
Mohammad.Naser

Reputation: 16

use

t := time.Now()
var fdatevalue string
// outstr = fmt.Sprintf("%02d%02d%02d%02d%02d", t.Month(), t.Day(), t.Hour(),       t.Minute(), t.Second())
fdatevalue=fmt.Sprintf("%02d%02d%2dT%2d:%2d:%2d", t.Year() , t.Month(), t.Day() , t.Hour(),t.Minute(),t.Second)

Upvotes: -3

Related Questions