MongoDB的奇怪日期行为

Chr*_*ris 2 mongoose mongodb node.js express reactjs

我正在编写一个使用MongoDB作为其数据库的React应用程序.从数据库保存和检索日期时,似乎存在一些奇怪的行为,特别是在更新特定文档中的日期时.

当我创建一个新文档时,一切都很好.但是,如果我尝试使用ajax调用编辑该文档上的日期,则MongoDB中存储的日期比我选择的日期早一天以及浏览器中显示的日期.下面的代码解释一下.

使用HTML5 <input type='date' />元素选择日期.这是一些代码.我已在各个点包含控制台日志以显示输出.我们假设我选择'2016年10月30日'作为日期.日期和年份在其他地方被拆分用于显示目的,但在发送到服务器之前以JS Date对象的形式连接在一起(参见下面的代码)

反应组件方法:

saveChanges(e, cancel){
        e.preventDefault();
        const cancelled = cancel ? true : false

        console.log(this.state.date); // 30 Oct
        console.log(this.state.year); // 2016

        const saveData = {
            id: this.props.data.id,
            venue: this.state.venue,
            unitNumber: this.state.unitNumber,
            unitName: this.state.unitName,
            date: this.state.date,
            year: this.state.year,
            day: this.state.day,
            tutorID: this.state.tutorID,
            cancelled: cancelled
        }

        editWorkshop(saveData, (data) => {
            this.props.getWorkshopDetails();
            this.props.workshopDetailsSaved(data);
        });

    }
Run Code Online (Sandbox Code Playgroud)

上面的方法editWorkshop.js使用axios 将数据发送到外部ajax调用:

import axios from 'axios';

export default function editWorkshop(input, callback){

    const date = new Date(input.date + ' ' + input.year) // Rejoin date and year and convert to Date object
    console.log(date); // Sun Oct 30 2016 00:00:00 GMT+0100 (BST)
    const data = {
        id: input.id,
        venue: input.venue,
        unitNumber: input.unitNumber,
        unitName: input.unitName,
        date: date,
        day: input.day,
        tutorID: input.tutorID,
        cancelled: input.cancelled
    }

    axios.post('/editWorkshop', data).then(function(res){
        callback(res.data); 
        })
    }
Run Code Online (Sandbox Code Playgroud)

最后是处理ajax调用的快速路由

const express = require('express');
const Workshop = require('../data/models/Workshop');

module.exports = function(req, res){

    const data = req.body
    console.log(data.date); // 2016-10-29T23:00:00.000Z - here is where it seems to go wrong - notice that the date has changed to 2016-10-29 instead of 10-30. This now gets written to the database
    Workshop.update({ _id: data.id }, {
        $set: {
            unitNumber: data.unitNumber,
            date: data.date,
            venue: data.venue,
            tutor: data.tutorID,
            session: data.day,
            cancelled: data.cancelled
        }
    }, function(err){
        if (err) {
            res.send(err);
            return
        }
        var message = 'Workshop updated'
        res.send({
            success: true,
            message: message
        });
    })

}
Run Code Online (Sandbox Code Playgroud)

真正奇怪的是,当我从应用程序的其他地方检索数据库时,它会在浏览器中显示正确的日期 - 2016年10月30日.

可以说这不是一个问题,因为显示了正确的日期,但我对此并不满意,因为这些日期是应用程序的基本部分,我担心将来可能存在漏洞.

任何人都可以对此有所了解吗?

4J4*_*J41 5

2016-10-29T23:00:00.000Z和...一样Sun Oct 30 2016 00:00:00 GMT+0100 (BST).

2016-10-29T23:00:00.000ZUTC(GMT)时区.如果将其转换为BST,您将获得相同的值.

MongoDB将日期值保存为自Epoch以来的UTC毫秒.

来自文档:

MongoDB默认以UTC格式存储时间,并将任何本地时间表示转换为此形式.必须操作或报告某些未修改的本地时间值的应用程序可以将时区与UTC时间戳一起存储,并在其应用程序逻辑中计算原始本地时间.