emilyk
emilyk

Reputation: 31

How can I use js (or moment.js) to check if a date is past the current date?

So now I'm just trying to get the dates from the para class to print to the console. Jquery and moment are both working fine. Right now this logs nothing to the console. I need to see if the current date is x days after any of the dates with a class of para. At this point I would be happy with just logging the class dates to the console.

js

        const dates = $('.para');

        dates.each((i, elem) => {
         let parts = $(elem).text().split('/');
           let dt = new Date(parts[2] ,parts[1], parts[0]).getTime();
           let diff = Math.floor((dt - new Date().getTime()) / (86400 * 
         1000));
       console.log('date:', $(elem).text(), 'days diff vs today:', diff);
      });

html

    <!DOCTYPE html>
      <html lang="en" dir="ltr">
       <head>
     <meta charset="utf-8">
         <title></title>
     <script src="moment.js"></script>

       <script 
      src="https://cdnjs.cloudflare.com/
  ajax/libs/jquery/3.3.1/jquery.slim.min.js" integrity="sha256- 
   3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"> 
   </script>
   <script src="forrealz.js"></script>



        </head>
      <body>

     <p class="para">
    11/18/2021
      </p>
    <p class="para">
  09/12/1999
     </p>

  </body>
  </html>

Update: Tried this in code pen instead of chrome and it works but the math is wrong. "date:" " 09/18/2018 " "days diff vs today:" 284 "date:" " 09/12/1999 " "days diff vs today:" -6837

Upvotes: 0

Views: 492

Answers (3)

benvc
benvc

Reputation: 15120

EDIT:

The following gets the date text from your html, parses it into month, day, and year, and then converts it to a js date object to be compared to the current date. The date difference is converted from milliseconds to days for output.

const dates = $('.para');
dates.each((i, elem) => {
  const [m, d, y] = $(elem).text().split('/');
  const dt = new Date(y, m - 1, d);
  const diff = Math.floor((dt - new Date()) / (86400 * 1000));
  console.log('date:', $(elem).text(), 'days diff vs today:', diff);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="para">05/23/2021</p>
<p class="para">12/19/1999</p>

ORIGINAL (before edits that removed moment.js code and added the html to the question):

You can get the date difference in days using vanilla js by parsing the date text from your html and converting to js date objects so you can do a little math to convert the difference in milliseconds to days (rounded to whole days in the example below).

The dates array in the example below is a placeholder for the collection of date elements you would select from your html (wasn't sure from your question / comments if you are working with a table or with p elements). Working snippet to illustrate:

const dates = ['05/23/2021', '12/19/1999'];
for (const date of dates) {
  const [m, d, y] = date.split('/');
  const dt = new Date(y, m - 1, d);
  const diff = Math.floor((dt - new Date()) / (86400 * 1000));
  console.log('date:', date, 'days diff vs today:', diff);
}

Upvotes: 1

NAVIN
NAVIN

Reputation: 3317

You can use moment.diff function. Follow these docs for moment difference, for better understanding.

Sample JS:

let d = moment(inputDate, 'M/D/YYYY');

if ( d.isValid() ) {
    let differenceInDays = moment().diff(d, 'days', true);  // moment() gives current date time.

    if (differenceInDays > 0) {
        console.log("Past current Date");
    }

}

Upvotes: 0

Seva Kalashnikov
Seva Kalashnikov

Reputation: 4392

Momentjs diff is the key function here:

var dayDifference = 10;

var d = new Date();
var momentDate = moment(d);

$('p.para').each(function (index, element) {
    var date = moment($(this).text().trim(), 'M/D/YYYY');
    if (momentDate.diff(date, 'days') < dayDifference) {
        // display alert
    }
}

This can be applied to html example you provided:

<p class="para"> 23/05/2021 </p>
<p class="para"> 19/12/1999 </p> 

Upvotes: 0

Related Questions