Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在mongo和react中使用相同的日期格式?_Javascript_Reactjs_Mongodb_Date_Mongoose - Fatal编程技术网

Javascript 如何在mongo和react中使用相同的日期格式?

Javascript 如何在mongo和react中使用相同的日期格式?,javascript,reactjs,mongodb,date,mongoose,Javascript,Reactjs,Mongodb,Date,Mongoose,我面临一个问题。我正在开发日历应用程序。问题是,在用全年的天数填充mongo db集合后,一切似乎都很好,但在将数据提取到react前端后,我以错误的日期格式和时区结束。我尝试过调试它,在使用Calendar.find({},(err,Calendar))…获取数据时,日历似乎使用了错误的日期时区进行解析。下面是更多 好的,这就是用一年中的天数填充数据库的控制器: exports.generateCalendar = (req, res) => { let days = [];

我面临一个问题。我正在开发日历应用程序。问题是,在用全年的天数填充mongo db集合后,一切似乎都很好,但在将数据提取到react前端后,我以错误的日期格式和时区结束。我尝试过调试它,在使用
Calendar.find({},(err,Calendar))…
获取数据时,日历似乎使用了错误的日期时区进行解析。下面是更多

好的,这就是用一年中的天数填充数据库的控制器:

exports.generateCalendar = (req, res) => {
    let days = [];
    for(let i=1; i<366; i++) {

        const dayOfTheWeek = new Date(new Date().getFullYear(), 0, i).getDay();
        if(dayOfTheWeek === 0 || dayOfTheWeek === 6){
            days.push({day: new Date(new Date().getFullYear(), 0, i), offWork: true, description: ' '});
        }
        else{
            days.push({day: new Date(new Date().getFullYear(), 0, i), offWork: false, description: ' '});
        }
    }

    Calendar.insertMany(days, (err) => {
        res.json('done');
    });
};
下一步是获取整个集合并将其发送到这个简单控制器的前端:

exports.getCalendar = (req, res) => {
    Calendar.find({}, (err, calendar) => {
        if(err){
            res.json('Error while fetching data');
        }else{
            res.json(calendar);
            }

    })
};
但是,获取的输出是:

{ offWork: false,
     _id: 5cefc2e5c6db7f47401ba261,
     day: 2018-12-31T23:00:00.000Z,
     description: ' ',
     __v: 0 },
   { offWork: false,
     _id: 5cefc2e5c6db7f47401ba262,
     day: 2019-01-01T23:00:00.000Z,
     description: ' ',
     __v: 0 },
   { offWork: false,
     _id: 5cefc2e5c6db7f47401ba263,
     day: 2019-01-02T23:00:00.000Z,
     description: ' ',
     __v: 0 },

在抓取mongo时,它会解析包含时区偏移的日期

我创建了一个for循环,将新日期推送到一个新数组:

            let calendarArray = [];

                for(let i=0; i<calendar.length; i++){
                    calendarArray.push({day: calendar[i].day.toString(), offWork: calendar[i].offWork, description: calendar[i].description});
                }
                res.json(calendarArray);

完美的解决方案应该是YYYY-MM-DD格式的数据库,或者在mongo中具有相同的日期格式,并且在抓取之后。

好的,我所做的是安装了moment.js,并在将日期推送到新阵列并将其发送到前端时使用它:


exports.getCalendar = (req, res) => {
    Calendar.find({}, (err, calendar) => {
        if(err){
            res.json('Error while fetching data');
        }else{
            let calendarArray = [];

                for(let i=0; i<calendar.length; i++){
                    calendarArray.push({_id: calendar[i]._id, day: moment(calendar[i].day).format("DD-MM-YYYY"), offWork: calendar[i].offWork, description: calendar[i].description, name: calendar[i].name});
                }

            res.json(calendarArray);
            }
    })
};

exports.getCalendar=(请求、回复)=>{
Calendar.find({},(err,Calendar)=>{
如果(错误){
res.json(“获取数据时出错”);
}否则{
让calendarArray=[];

对于(让i=0;iOk),我安装了moment.js,并在将日期推送到新阵列并将其发送到前端时使用它:


exports.getCalendar = (req, res) => {
    Calendar.find({}, (err, calendar) => {
        if(err){
            res.json('Error while fetching data');
        }else{
            let calendarArray = [];

                for(let i=0; i<calendar.length; i++){
                    calendarArray.push({_id: calendar[i]._id, day: moment(calendar[i].day).format("DD-MM-YYYY"), offWork: calendar[i].offWork, description: calendar[i].description, name: calendar[i].name});
                }

            res.json(calendarArray);
            }
    })
};

exports.getCalendar=(请求、回复)=>{
Calendar.find({},(err,Calendar)=>{
如果(错误){
res.json(“获取数据时出错”);
}否则{
让calendarArray=[];
for(设i=0;i

exports.getCalendar = (req, res) => {
    Calendar.find({}, (err, calendar) => {
        if(err){
            res.json('Error while fetching data');
        }else{
            let calendarArray = [];

                for(let i=0; i<calendar.length; i++){
                    calendarArray.push({_id: calendar[i]._id, day: moment(calendar[i].day).format("DD-MM-YYYY"), offWork: calendar[i].offWork, description: calendar[i].description, name: calendar[i].name});
                }

            res.json(calendarArray);
            }
    })
};