简体   繁体   English

javascript日期的Date.Format

[英]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 . 从我想在html / template中输出的appengine数据存储区加载的Google可视化时间表 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. 首先,我尝试使用{{.Date.Format "new Date(2006,1,2,15,4,5)"}}直接在模板中设置日期格式,但是html / template对此进行了转义,因此它在html来源。 I then tried formatting the date into a []struct{Date template.JS; Value template.JS} 然后,我尝试将日期格式化为[]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. javascript想要使用表达式template.JS(m.Date.Format("new Date(2006,1,2,15,4,5)")) []struct{Date template.JS; Value template.JS} 。一月为0。我可以让模板生成日期参数的json并编写javascript将其转换为Date对象,或者使用go代码来调整模板输出。 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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