码迷,mamicode.com
首页 > 其他好文 > 详细

模板自定义函数 template function

时间:2018-01-01 00:26:20      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:color   temp   --   解析   就是   employee   html   oca   自定义函数   

sqlite3中的日期默认是UTC,当日期字段的默认值是CURRENT_TIMESTAMP时,这个日期和北京时间CST少了8小时。
网上建议说数据库里用UTC,读取数据时再转换为当地时间。

web页面中的日期如创建日期通常是需要“格式化”一下的,否则显示出来是这个样子:

2017-08-17 08:50:37 +0000 UTC 

在go template中可以使用管道,自定义一个日期函数即可。
其实这个函数很简单,关键就是要用Local函数:

func formatDate(t time.Time) string { 
  layout := "2006-01-02"
  return t.Local().Format(layout)
}

还要记住一点:给模板中增加的自定义函数要在解析html文件前调用!

funcMap := template.FuncMap{
  "fdate": formatDate, "fdatetime": formatDateTime
}
t, err := template.New("employees.html").Funcs(funcMap).ParseFiles("employees.html")

-- END --

模板自定义函数 template function

标签:color   temp   --   解析   就是   employee   html   oca   自定义函数   

原文地址:https://www.cnblogs.com/ibgo/p/8159374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!