【问题标题】:storing timestamp in meteor mongo collection and then displaying it将时间戳存储在流星 mongo 集合中,然后显示它
【发布时间】:2015-04-01 14:02:52
【问题描述】:

我有一个简单的表单,提交时会将数据输入到流星 mongo 集合中。然后我将刚刚提交的数据输出到另一个 div 中,有点像提交的推文并在时间轴中显示。

我想将用户提交表单时的时间戳存储到集合中,然后显示按时间戳降序排列的“提要”,因此最新的在前。我目前正在使用 moment.js 记录集合中的时间戳,然后简单地在输出中使用该时间戳,但这似乎不是最有效的方法。

我打算让不同时区的用户提交表单,因此希望能够显示以 UTC 格式提交的时间戳,按最新排序。

目前我有...

Template.form.events({
    'submit form': function(e) {
    e.preventDefault();

    var insertPost = {
        timestamp: moment().format('DD-MM-YY HH:mm:ss ZZ')
        // some other stuff as well
    };

 form._id = Posts.insert(insertPost);

)};

然后这将显示在我的 post_item.html 中:

<template name="post-item">
    <div>
        <ul class="list-inline">
            <li><small>{{timestamp}}</small></li>
            // some other stuff too
        </ul>
    </div>
</template>

这种存储时间戳的方式可以吗?我觉得以毫秒为单位存储它可能会更好,然后在输出中对其进行格式化。

所以我可以更改我的 form.events 代码来存储

timestamp: moment.utc().valueOf(),

自纪元以来以毫秒为单位存储,但我如何在 html 模板中转换它以显示格式良好的时间戳?

【问题讨论】:

标签: javascript meteor momentjs


【解决方案1】:

使用moment js以各种格式显示日期和时间,大气包安装量大。

http://momentjs.com

https://atmospherejs.com/momentjs/moment

【讨论】:

  • 对不起,我没有正确阅读问题。我总是只使用带有 new Date() 的 Date 对象的实例,并在前端使用 moment 对其进行格式化。还有一个用于在插入之前和更新之前为集合添加时间戳的包,称为 github.com/Sewdn/meteor-collection-behaviours
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2018-05-03
  • 2013-01-16
  • 1970-01-01
相关资源
最近更新 更多