Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Counting days from a string

I’m trying to get a date I put myself in my html. And calculate how many days it has been posted. My loop works until the second date then it returns the same as the first. I can’t seem to find the bug. I also haven’t included the part that then injects it back in to the html tag.

Js

var today = new Date();// date today
var now = today.getDate()+'-'+(today.getMonth()+1)+'-'+today.getFullYear();//order my date in day-month-year

console.log(now);
var oldDatearr = document.querySelectorAll(".olddate");// takes old date from html and puts in array

for (let i = 0;i < oldDatearr.length;i++){

    let oldDate = new Date(oldDatearr[i].innerHTML); // takes the first date from the array
    let oldDateOrder = oldDate.getDate()+'-'+(oldDate.getMonth()+1)+'-'+ oldDate.getFullYear();//order old date with day-month-year

    console.log(oldDateOrder);

    const msPerDay = 24 * 60 * 60 * 1000;//days formule
    let daysLeft = Math.floor(parseInt(now) - parseInt(oldDateOrder) / msPerDay);//new date minus old date

    console.log(daysLeft + " days ago");


}

html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="list-group shadow-lg">
              
                    <a href="#" target="_blank" class="list-group-item list-group-item-action" aria-current="true">
                        <div class=" d-flex w-100 justify-content-between">
                            <h5 class="mb-1">test</h5>
                            <small class="olddate text-muted">11/01/2021</small>
                        </div>
                        <p class="mb-1">test</p>
                    </a>
                    <a href="#" target="_blank" class="list-group-item list-group-item-action sbgrey">
                        <div class="d-flex w-100 justify-content-between">
                            <h5 class="mb-1">test</h5>
                            <small class="olddate text-muted">11/02/2021</small>
                        </div>
                        <p class="mb-1">test</p>
                    </a>
                    <a href="#" target="_blank" class="list-group-item list-group-item-action">
                        <div class="d-flex w-100 justify-content-between">
                            <h5 class="mb-1">test</h5>
                            <small class="olddate text-muted">11/03/2021</small>
                        </div>
                        <p class="mb-1">test</p>
                    </a>
                    <a href="#" target="_blank" class="list-group-item list-group-item-action">
                        <div class="d-flex w-100 justify-content-between">
                            <h5 class="mb-1">test</h5>
                            <small class="olddate text-muted">11/03/2021</small>
                        </div>
                        <p class="mb-1">test</p>
                    </a>
 </div>

>Solution :

  1. Formatting the date as a string is useless because you can’t do math with a date like "10/11/2001". Remove all that formatting code.
  2. Order of operations dictates that you need to put the subtraction in parenthesis before you divide.

This code works:

var today = new Date();
var oldDatearr = document.querySelectorAll(".olddate");

for (let i = 0; i < oldDatearr.length; i++) {
  let oldDate = new Date(oldDatearr[i].innerHTML);
  const msPerDay = 24 * 60 * 60 * 1000;
  let daysLeft = Math.floor((today - oldDate) / msPerDay);
  console.log(daysLeft + " days ago");
}
<div class="list-group shadow-lg">

  <a href="#" target="_blank" class="list-group-item list-group-item-action" aria-current="true">
    <div class=" d-flex w-100 justify-content-between">
      <h5 class="mb-1">test</h5>
      <small class="olddate text-muted">11/01/2021</small>
    </div>
    <p class="mb-1">test</p>
  </a>
  <a href="#" target="_blank" class="list-group-item list-group-item-action sbgrey">
    <div class="d-flex w-100 justify-content-between">
      <h5 class="mb-1">test</h5>
      <small class="olddate text-muted">11/02/2021</small>
    </div>
    <p class="mb-1">test</p>
  </a>
  <a href="#" target="_blank" class="list-group-item list-group-item-action">
    <div class="d-flex w-100 justify-content-between">
      <h5 class="mb-1">test</h5>
      <small class="olddate text-muted">11/03/2021</small>
    </div>
    <p class="mb-1">test</p>
  </a>
  <a href="#" target="_blank" class="list-group-item list-group-item-action">
    <div class="d-flex w-100 justify-content-between">
      <h5 class="mb-1">test</h5>
      <small class="olddate text-muted">11/03/2021</small>
    </div>
    <p class="mb-1">test</p>
  </a>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading