-1

I'm trying to subtract days by a constant integer in MM/DD format. Here is my current code in javascript.

   var d = new Date();
    var n = d.getDate();
    var m = d.getMonth() +1;


    var q = document.getElementsByClassName('four-days-back');
    for(var i =0; i<q.length; i++){
    var o =4;
    var z = d.setDate(d.getDate()-o);
    q[i].innerHTML = m + "/" + z;
    }

output is: 11/1572387207470.

  • `getElementByClassName` should be `getElementsByClassName` – j08691 Nov 02 '19 at 22:27
  • https://stackoverflow.com/questions/563406/add-days-to-javascript-date might be of some help. Simply pass in a negative number for days to `addDays` – ibrahim mahrir Nov 02 '19 at 22:27
  • Date.prototype.subtractDays= function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } var date = new Date(); q[i].innerHTML = date.subtractDays(5) it gives a valid date, but how can I simplify it to MM/DD? – learningWebDev Nov 02 '19 at 22:39
  • Should do something like `d.setDate(d.getDate() + 10);` or substract if you want. Easy enough. – icecub Nov 02 '19 at 22:45
  • If you want to learn how to format it, just check this question: [Javascript: output current datetime in YYYY/mm/dd hh:m:sec format](https://stackoverflow.com/questions/8362952/javascript-output-current-datetime-in-yyyy-mm-dd-hhmsec-format) – icecub Nov 02 '19 at 22:48
  • @learningWebDev `var d = date.substractDays(5); q[i].textContent = d.getDate() + "/" + d.getMonth();` – ibrahim mahrir Nov 02 '19 at 22:50
  • d.setdate(d.getDate() -10); does not work. – learningWebDev Nov 03 '19 at 01:19
  • Back-end is really not my strong suit, but using php strtotime was helpful in this scenario. – learningWebDev Nov 03 '19 at 02:08

2 Answers2

0

As there is not yet an accepted answer to this post I put together some of the aspects mentioned in the comments and compiled them into a working fiddle. Please do not feel offended if it appears to you that I hijacked your contribution.

One point that remained unclear to me was the origin of the date. Maybe it is only one date (the current one) OP wants to subtract from? This would only require one conversion/subtraction to be done. On the other hand, maybe OP wants the subtraction to be done on a number of "input cells"? In the following example I added some HTML to the question in which I provide input dates in the column preceding the target cells with class "four-days-back".

The script loops over all the target cells and fills them with dates 4 days before the dates given in the column before that.

Since the year is not defined in the date column it needs to be defined nonetheless. The year becomes important because of leap year implications. In my example I assume that the current year applies.

[[ This answer is a "Vanilla-JS" one, using ES2015 arrow notation and the ES2017 element String.prototype.padStart(). Therefore it will not work in IE browsers. It will need to be adapted in order to work in these browsers. ]]

var y = new Date().getFullYear();

[...document.querySelectorAll('.four-days-back')].forEach(el=>{
  var md=el.previousElementSibling.innerText.split('/');
  var mm,dd;
  var dt=new Date(y,md[0]-1,md[1]-4,12);
  el.innerText=(dt.getMonth()+1+'').padStart(2,'0')
             +'/'+(dt.getDate()+'').padStart(2,'0')
});
  
<table>
<tr><th>topic</th><th>date</th><th>4 days before</th></tr>
<tr><td>one</td><td>03/02</td><td class="four-days-back"></td></tr>
<tr><td>two</td><td>08/14</td><td class="four-days-back"></td></tr>
<tr><td>three</td><td>09/03</td><td class="four-days-back"></td></tr>
<tr><td>four</td><td>10/01</td><td class="four-days-back"></td></tr>
</table>

Maybe the line var dt=new Date(y,md[0]-1,md[1]-4,12); deserves some explanation? I set the date to given year, month, date and hour values. The year is the current year, month and date values I take from the preceding column (note that JavaScript month numbering starts at 0) and I use an hour value of 12 in order to avoid any "daylight saving issues".

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
-1

Not Javascript or Jquery, but PHP can be helpful.

e.g

<?php
$one = strtotime("-4 days");
echo "<td>".date("m/d", $one)."</td>"
?>