Need a function or code to format a golang time.Time into a UTC string or object that can be passed new Date() in Javascript

1,334 views
Skip to first unread message

Gregg Murray

unread,
May 21, 2016, 11:28:05 AM5/21/16
to golang-nuts
Surely this is a common request?  
Just looking for a quick and dirty solution.  
Does anyone have a function or the code to do this?

==========================================
----Server Code (Golang)-----

d := time.Now()

//x :=   <<<<<This is the code I need>>>>

j, err := json.Marshal(x)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(j)

------Client Code (JavaScript)------
//....Ajax.....  retrieve j

myDate = JSON.parse(j)

d = new Date(myDate)

==========================================

Thanks in advance to anyone who can provide the simplest way to do this without new imports, 
visits to github, or a lesson in date formatting (already been to the dentist this month).

-G

Ain

unread,
May 21, 2016, 11:45:31 AM5/21/16
to golang-nuts

On Saturday, May 21, 2016 at 6:28:05 PM UTC+3, Gregg Murray wrote:
Surely this is a common request?  
Just looking for a quick and dirty solution.  
Does anyone have a function or the code to do this?

==========================================
----Server Code (Golang)-----

d := time.Now()

//x :=   <<<<<This is the code I need>>>>

I guess you want something along the lines

type DateType struct{ time.Time }

func (this *DateType) MarshalText() (result []byte, err error) {
    fmted := this.Format("2006-01-02") // here use the format required by JS parser
    return []byte(fmted), nil
}

type SendTOJS struct {
   D DateType `json:"D"`
}

var x SendTOJS
x.D = time.Now()

 
j, err := json.Marshal(x)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(j)



HTH
ain

Frits van Bommel

unread,
May 21, 2016, 11:55:33 AM5/21/16
to golang-nuts
Well, Javascript accepts the number of milliseconds since the Unix epoch as one of the options, so

x := d.UnixNano() / int64(time.Millisecond / time.Nanosecond)       // time.Nanosecond == 1, so dividing by it is redundant but more clearly correct.

?
If you need to support dates over 2 centuries in the future (or just want a readable string instead of an opaque number) the Mozilla documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse allows a format that looks suspiciously like time.RFC1123Z (though they call it something different), so

x := d.Format(time.RFC1123Z)

would probably work as well.

Gregg Murray

unread,
May 21, 2016, 12:26:01 PM5/21/16
to golang-nuts
Thank you!  Last question, will that method allow for java script to display the time as local to the browser user?  This is a multi-time zone app.  IE:  Does time.Now().UnixNano() give local server time or UTC?  


Frits van Bommel

unread,
May 21, 2016, 1:26:17 PM5/21/16
to golang-nuts
Unix time is specified as time since 1 jan 1970 UTC (by default in seconds but javascript uses milliseconds instead), which is also what the javascript Date object expects to get if you pass it a number.
If you use my second suggestion of d.Format(time.RFC1123Z), that includes a timezone but I'm not sure if javascript will keep track of that or simply convert to whatever time zone it uses internally.

Either way, getting local time out of it is then purely a javascript matter, so I'd look here and experiment a little (for instance, the toString() docs on that site don't specify which locale it uses, but given that there's also a toUTCString() I'd say the odds were good it uses local time).
However, this isn't really the right place for javascript questions so if you need help with that I'd suggest taking the question(s) to a more appropriate group.
Message has been deleted

C Banning

unread,
May 21, 2016, 5:55:01 PM5/21/16
to golang-nuts
Reply all
Reply to author
Forward
0 new messages