Javascript日期的Date.Format

时间:2013-06-02 03:06:23

标签: javascript go

我有一个

数组
struct {
    Date         time.Time
    PostedSample int
}

从appengine数据存储区加载,我想在Google Visualization Time Line的html /模板中输出。首先,我尝试使用{{.Date.Format "new Date(2006,1,2,15,4,5)"}}直接在模板中格式化Date,但是html / template将其转义为html源中的带引号的字符串。然后我尝试将日期格式化为[]struct{Date template.JS; Value template.JS},表达式template.JS(m.Date.Format("new Date(2006,1,2,15,4,5)"))几乎可以正常工作,除了月份是一个,javascript希望1月份为0.我可以让模板生成日期参数的json和写javascript将其转换为Date对象或具有调整模板输出的代码。请分享更优雅的解决方案。谢谢。

1 个答案:

答案 0 :(得分:1)

您无需在模板中添加格式化功能。

你可以这样使用你的结构:

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

解决方案可能是这样的:

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