kathryn kathryn - 7 months ago 6
Javascript Question

How to hide decimals on JS?

I wanted to show the years I've been working through a JS, but I don't know how to hide the decimals. Can anyone help?

var DayDiff = function (start) {
var now = new Date().getTime();
start = new Date(start).getTime();

return Math.floor((now - start)/ 86400000);
};

document.write("" + DayDiff('2000-04-28')/ 365);


Result apears 16.01095890410959 and I just want 16 to appear.

Also, any ideas on how to show a number and add 100 only after 6 months?

Thank you so much in advance!

Answer

Use Math.round, returns the value of a number rounded to the nearest integer.

You are returning floored value in the function but again after division, you will get floating-number. Apply Math.round over the value returned after division.

var DayDiff = function(start) {
  var now = new Date().getTime();
  start = new Date(start).getTime();
  return Math.floor((now - start) / 86400000);
};

document.write("" + Math.round(DayDiff('2000-04-28') / 365));