简体   繁体   中英

discord.js if date.now() function

I'm trying to get the current date (day, hour, minute, second) and put it in an if() statement to compare it with the date entered. But how am I able to get the date and ask for it in code?

This is what I got so far:

    bot.on("ready", () => {
    var today = new Date();
    var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
    var time = today.getHours() + ":" + today.getMinutes();

    var currentdate = date + time

    var endDate = new Date('2021-01-21T14:00:00')

    if (endDate = currentdate) {
        console.log('The time has come..')
    } else {
        console.log('The time is coming soon..')
    }

})


you can use 'moment.js' like this:

  const moment = require("moment");

  getDifferenceInYear(date_1, date_2) {
    return moment(date_1).diff(moment(date_2), "years");
  }

  getDifferenceInDay(date_1, date_2) {
    return moment(date_1).diff(moment(date_2), "days");
  }

  getDifferenceInHour(date_1, date_2) {
    return moment(date_1).diff(moment(date_2), "hour");
  }

  getDifferenceInSecond(date_1, date_2) {
    return moment(date_1).diff(moment(date_2), "second");
  }

  isNowBetween(startDate, endDate) {
    return moment(new Date()).isBetween(startDate, endDate);
  }

you can following this code for comparing:

let inputDate = '2021-01-21T14:00:00'

let endDate =  Math.round(new Date(inputDate ) / 1000);
let cuurentDate = Math.round(new Date() / 1000)

if (cuurentDate  == endDate ) {
        console.log('The time has come..')
    }
else {
        console.log('The time is coming soon..')
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM