doushouj966020 2013-09-03 17:49
浏览 152
已采纳

使用html / template在切片中执行格式化的时间

I'm making this simple webserver that can host my blog, but whatever I do; I can not execute a proper formatted time into my html/template.

Here's what I do:

I've created this struct:

type Blogpost struct {
    Title   string
    Content string
    Date    time.Time
}

Next up I've created this little func that retrieves the blogposts with corresponding title/dates from the Appengine Datastore and return that as a slice:

func GetBlogs(r *http.Request, max int) []Blogpost {
    c := appengine.NewContext(r)
    q := datastore.NewQuery("Blogpost").Order("-Date").Limit(max)
    bp := make([]Blogpost, 0, max)
    q.GetAll(c, &bp)
    return bp
}

Finally, in the blogHandler I create a slice based on the retrieved data from the Appengine Datastore using:

blogs := GetBlogs(r, 10)

Now when I Execute my template called blog like this, the dates of the blogs are being parsed as default dates:

blog.Execute(w, blogs) // gives dates like: 2013-09-03 16:06:48 +0000 UTC

So, me, being the Golang n00b that I am, would say that a function like the following would give me the result I want

blogs[0].Date = blogs[0].Date.Format("02-01-2006 15:04:05") // Would return like 03-09-2013 16:06:48, at least when you print the formatted date that is.

However that results in a type conflict ofcourse, which I tried to solve using:

blogs[0].Date, _ = time.Parse("02-01-2006 15:04:05", blogs[0].Date.Format("02-01-2006 15:04:05")) // returns once again: 2013-09-03 16:06:48 +0000 UTC

It is probably some n00b thing I oversaw once again, but I just can't see how I can't override a time.Time Type in a slice or at least print it in the format that I want.

  • 写回答

3条回答 默认 最新

  • doucheng3407 2013-09-03 19:24
    关注

    Your Date field has type time.Time. If you format it as a string, and parse it back, you'll once again get a time.Time value, which will still print the default way when the template execution calls its String method, so it's not really solving your problem.

    The way to solve it is by providing the template with the formatted time string itself instead of a time value, and you can do that in multiple ways. For example:

    • Add a method to your blog post type named FormattedDate or similar, which returns a string properly formatted in the style of your preference. That's the easiest, and probably the nicest way if you don't have a fancy use case.

    • Add a string field to your blog type named FormattedDate or similar; that's very similar to the above option, but you have to be careful to set and update the field whenever necessary, so I'd prefer the method option instead.

    • If you'd like to format time values in multiple ways within the template, you might also opt to define a template formatter function, so that you might do something like {{post.Date | fdate "02-01-2006 15:04:05"}}. See the documentation on Template.Funcs, the FuncMap type, and this example for details on how to do that.

    Update: Sample code for the first option: http://play.golang.org/p/3QYdrDQ1YO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成