【问题标题】:MomentJS Returning Date with TimestampMomentJS 返回带有时间戳的日期
【发布时间】:2016-06-26 21:49:21
【问题描述】:

我有一个问题,提交的日期字段返回带有时间戳的日期,而不仅仅是时间戳设置为 00:00:00 的日期。此代码适用于我试图实现此过程的其他实例,但由于某种原因它不起作用,我很好奇转换是否必须在我的视图中发生在我的表单中,或者应该在我的后端发生路线。

路线:

.post(function(req, res){


        models.Creator.findAll({
            order: 'createDate DESC',
            where: {
                dataDateStart: {
                    $gte: moment(req.body.startDate).utc().format("YYYY-MM-DD")
                },
                dataDateEnd: {
                    $lte: moment(req.body.endDate).utc().format("YYYY-MM-DD")
                }
            },
            include: [{
                model: models.User,
                where: { 
                    organizationId: req.user.organizationId,
                },
                attributes: ['organizationId', 'userId']
            }],
            limit: 10
        }).then(function() { 
            res.redirect('/app');
        }).catch(function(error){
            res.send(error);
        })
    });

输出 where 子句:

WHERE `creator`.`data_date_start` >= '2016-06-07 04:00:00' AND `creator`.`data_date_end` <= '2016-06-11 04:00:00' ORDER BY createDate DESC LIMIT 10;

如您所见,问题是出现04:00:00 而不是00:00:00

查看:

<div class="row">
    <div class="creator-search-form col-md-6 col-md-offset-3">
        <h1 id="search-header">Filter Feed</h1>
        <form action="/app" method="post" class="creator-filter-fields">
            <p>Date Range:</p>
            <input type="date" name="startDate">
            <input type="date" name="endDate">
            <button type="submit" id="creator-filter-submit">Submit</button>
        </form>
    </div>
</div>

【问题讨论】:

    标签: javascript momentjs


    【解决方案1】:

    我希望您的情况是 Moment 下游的某个地方,您没有时间的日期被解释为本地时间,然后转换为 UTC。我猜您的服务器设置为 UTC-4(美国东部夏令时间?),这就是您看到自己所做的事情的原因。

    当您从使用 .format('YYYY-MM-DD') 更改为仅使用 .format() 时,生成的字符串包括所有时间部分和偏移量,如下所示:

    moment.utc('2016-01-01').format()
    "2016-01-01T00:00:00Z"
    

    因为上述日期是完全明确的,所以无论您在堆栈中更改时间的任何内容都不会对如何解释它做出任何奇怪的决定,并且一切正常。

    【讨论】:

      【解决方案2】:

      如果从日期元素中查询到的值是格式为YYYY-MM-DD的字符串,则可以通过

      moment.utc(value).format("YYYY-MM-DD")
      

      其中值是日期字符串。

      我希望这会有所帮助。

      【讨论】:

      • 感谢您的评论,但您的解决方案提供的输出与我已经得到的完全相同。仍然没有产生日期字符串
      • 不应该这样。您可以在浏览器的控制台窗口上试用它(假设存在 moment.js 库)。 moment.utc('2015-01-01 00:00:00').format("YYYY-MM-DD") ==>>> "2015-01-01"。问题可能出在其他地方。请 console.log req.body.startDate 和 req.body.endDate。
      • 你说得对,我加了console.log('Unformatted: ' + req.body.startDate); console.log('Formatted: ' + moment.utc(req.body.startDate).format("YYYY-MM-DD"));,收到了Unformatted: 2016-06-13 Formatted: 2016-06-13。这似乎表明它可能是 Sequelize,我的 ORM 附加了04:00:00。你同意吗?
      • 我也认为 04:00:00 与当地时间有关。如果我运行 This is the new Date(date);,则输出格式为 Thu Jun 09 2016 20:00:00 GMT-0400 (EDT) 出于某种原因,我认为将格式设置为日期仍会考虑本地时间,并通过将其设置为凌晨 4 点来抵消时间。
      • 我通过从.format 中删除"YYYY-MM-DD" 解决了这个问题。出于某种原因,我将在回答中解释此修改将恢复为当地时间
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2022-01-14
      相关资源
      最近更新 更多