猿问

如何将天数添加到时间戳?

我有一个开始日期时间戳和一个持续时间(天数),我需要得到结束日期,我厌倦了这段代码给出了错误的结束日期时间戳


exports.terminateStoreAd = functions.https.onRequest(async(req, res) => {

        try {

            const snapshot =await admin.database().ref("StoreAds").once("value");

            if (snapshot.exists()) {

                snapshot.forEach(snapData => {

                    if (snapData.exists()) {

                        const endDate=new Date(snapData.val().startDate).getTime()+(snapData.val().duration*24*60*60*1000);

                        res.send(""+endDate);

                    }

                });

                res.send("done")

            }

        } catch (error) {

            console.log("terminateStoreAd error :" + error.message); 

        }

    });

我的开始日期是:1559449773


持续时间:5


结束日期:1991449773 :(


提前致谢。


开满天机
浏览 246回答 3
3回答

幕布斯7119047

const endDate = snapData.val().startDate + snapData.val().duration*24*60*60*1000;足以以毫秒为单位获取所需的日期(如果startDate以毫秒为单位)否则如果startDate是日期字符串,const endDate = (new Date(snapData.val().startDate)).getTime() + snapData.val().duration*24*60*60*1000;假设您需要endDate以毫秒为单位。

慕容森

最后我得到了解决方案exports.terminateStoreAd = functions.https.onRequest(async(req, res) => {try {&nbsp; &nbsp; const snapshot =await admin.database().ref("StoreAds").once("value");&nbsp; &nbsp; const promises = [];&nbsp; &nbsp; if (snapshot.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; snapshot.forEach(childSnapshot => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const endDate=childSnapshot.val().startDate + childSnapshot.val().duration&nbsp; * 86400;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const today=Math.round(new Date().getTime()/1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (endDate <= today) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; promises.push(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; admin.database().ref("StoreAdsHistory").child(childSnapshot.key).set(childSnapshot.val()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; childSnapshot.ref.remove(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.send()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;await Promise.all(promises);&nbsp; &nbsp; }catch (error) {}});

慕慕森

给定开始日期 2019-05-01 然后 5 天后是使用参数化版本创建新日期的简单问题。注意月份是零索引,所以五月,第 5 个月是索引 4。因为我想要 5 月 1 日之后的 5 天,所以我使用1+5作为日期:const startDate = new Date(2019, 4, 1);const endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()+5);console.log(`Start Date: ${startDate}, End Date: ${endDate}`);console.log(`Start Date: ${startDate.valueOf()}, End Date: ${endDate.valueOf()}`);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答