Utilizing Discord.js and the date.now() Function

Feedback

Question:

I’m attempting to obtain the present date (including day, hour, minute, and second) and utilize it in an if() statement to compare it with the entered date. However, I’m unsure of how to retrieve the date and include it in the 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..')
    }
})



Solution 1:

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);
  }


Solution 2:


To compare, you can use the following code.

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..')
    }

Frequently Asked Questions