1

I am creating a node app where a user can select a bought date using the HTML input of: <input placeholder="Date Bought" class="textbox-n form-control" type="text" onfocus="(this.type='date')" onblur="(this.type='text')" id="bought" name="bought">

This is then stored to a mongodb using var bought = req.body.bought; and the user inputs a number of months into another HTML input that has name="warranty" I need to convert to a date using the bought date and then warranty months. How can I add these? I have tried this:

  var bought = req.body.bought;
  var warrantyMonths = req.body.warranty;
  var warranty = bought.setMonth(bought.getMonth()+ warrantyMonths); 

As per this guide

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Kirbytech
  • 634
  • 1
  • 5
  • 18

2 Answers2

1

Try using Moment.js

If warranty months is an integer you can:

var bought = moment(req.body.bought);
var warrantyMonths = req.body.warranty;
var warranty = moment(bought).add(warrantyMonths, "month");

Moment is just a wrapper for the JS date object so it's seamless and easy to use. You can install it as an npm package, import it to your page, and pass it different formatted date strings (see the docs) or any valid javascript date object. I can't work with dates without moments anymore, imo it should be part of the standard library.

Robbie Milejczak
  • 5,664
  • 3
  • 32
  • 65
  • I tried that, I installed moment using NPM. I then took and pasted your code in and submitted my form got this as my bought date `"bought" : ISODate("2018-05-01T00:00:00Z"),` and then this as my warranty date `"warranty" : ISODate("2018-05-01T00:00:00Z"),` I used 12 months as my number for warranty date. – Kirbytech Oct 20 '17 at 20:15
  • oops change moment(bought.add(warrantyMonths, "month")) to moment(bought).add(warrantyMonths, "month") thats my bad. The initial line is mutating the bought object but what you want to do is clone it first by calling moment(bought) and then call the add method – Robbie Milejczak Oct 20 '17 at 20:17
  • Thank you very much, I have marked your answer as correct. – Kirbytech Oct 20 '17 at 20:25
0

Go through the code and comments to understand why your code was not working properly

function calculateWarranty() {
  // get the date text and convert into date Object
  var bought = new Date(document.getElementById('bought').value);
  // get the number of warranty months and convert to integer (second arguments ensures that the integer is base 10)
  // if you do not convert the number, it will do String concatenation while calling dateObj.setMonth(getMonth_int + warrant_string )
  // for example: 2017-10-21 will give bought.getMonth() === 9 + "1", which will equal 91 instead of 10
  var warrantyMonths = parseInt(document.getElementById('warranty').value, 10);

  // display in console for debugging
  console.log(bought);
  console.log(warrantyMonths);

  var boughtMonth = bought.getMonth();

  // add bought month and warranty month (both should be integer as stated earlier)
  var warrantyMonth = boughtMonth + warrantyMonths;

  // set the month to bought object
  bought.setMonth(warrantyMonth);

  // result
  console.log(bought);
}

// adding event listeners on the input boxes
document.querySelector('#bought').addEventListener('change', calculateWarranty);
document.querySelector('#warranty').addEventListener('change', calculateWarranty);
<input id="bought" name="bought" class="textbox-n form-control" type="text" placeholder="Date Bought" onfocus="(this.type='date')" onblur="(this.type='text')" />

<input id="warranty" name="warranty" class="textbox-n form-control" type="number" value="1" />

You also need to add some error handling to the code.

  • Thanks for the comments, explained a lot. I do like moment.js because of the simplicity, though your method I think helps me learn more about vanilla js – Kirbytech Oct 20 '17 at 20:40