简体   繁体   中英

Date.Format for javascript date

I have an array of

struct {
    Date         time.Time
    PostedSample int
}

loaded from the appengine datastore which I want to output in a html/template for the Google Visualization Time Line . First I tried formatting the Date directly in the template with {{.Date.Format "new Date(2006,1,2,15,4,5)"}} but html/template escapes this so it appears as a quoted string in the html source. I then tried formatting the date into a []struct{Date template.JS; Value template.JS} []struct{Date template.JS; Value template.JS} with the expression template.JS(m.Date.Format("new Date(2006,1,2,15,4,5)")) which almost works except the month is off by one, javascript wants January as 0. I could have the template generate a json of date parameters and write javascript turn that into Date objects or have go code which adjusts the template output. Please share a more elegant solution. Thank you.

You don't need to add a format function to the templates.

You can use your struct like so:

{{.Date.Format "Mon 2 Jan 2006"}}

The solution might be something like this:

var date = new Date(parseInt({{.Date.Nanosecond }} /1000));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM