discord.js if date.now() function

Viewed 807

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

})


2 Answers

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