首页 > 解决方案 > Mongoose 的日期未按预期运行

问题描述

正如标题所说,我已经在这个问题上工作了几天,但仍然无法解决它。基本上我想做的很简单。我正在尝试构建一个不和谐的机器人获取某事开始的日期和该事件结束的日期。特别是当用户键入“开始”时,我应该在写入时注册,然后注册“结束”命令的时间。

这是start的代码。

if (args == 'start') {

            if (!message.member.voice.channel) return message.channel.send(`<@${message.member.id}> you have to join a **voice channel** so we can catch your suspicious attempts lol`);

            if (user.studystarted) return message.channel.send(`<@${message.member.id}> Your study has started! whisper: *stop procrastinating*`);

            await profileModel.updateOne({
                userID: message.author.id,
            }, {
                $set: {
                    studystarted: true,

                },
                $currentDate: {
                    studystart: true,
                }


            }, {
                upsert: true,
            });

这是end的代码;

    if (args == 'end') {

            if (!user.studystarted) return message.channel.send(`<@${message.member.id}> how are you going to end it if you haven't started?`);

            await profileModel.updateOne({
                userID: message.author.id,
            }, {

                $currentDate: {
                    studyend: true,
                }
            }, { upsert: true });


            let starttime = user.studystart;
            let endtime = user.studyend;

            console.log("start time: " + starttime);
            console.log("end time: " + endtime);

            let diffTime = (endtime.getTime() - starttime.getTime()) / 1000;

            await profileModel.updateOne({
                userID: message.author.id,
            }, {
                $inc: {
                    studytotal: diffTime,
                    weeklytime: diffTime
                },
                $set: {
                    studystarted: false,
                }
            });

我已经尝试了很多方法来设置日期,但我看不到任何一种方法。它几乎总是以结束时间写在前面而开始时间写在后面,这不应该是它的工作方式。

任何建议表示赞赏。我是初学者

标签: javascriptdatemongoosediscord.js

解决方案


推荐阅读