0

Say I have something like this:

var time01 = new Date();
var time02 = new Date(2015, 0, 0, 0, 0, 0);
var time03 = new Date(2022, 0, 0, 0, 0, 0);

If I want to calculate the time passed from time02 till today, and how much time remained till the time03 from now, I should do this:

var passed01 = time01.getTime() - time02.getTime();
var passed02 = time03.getTime() - time01.getTime();

And then, I want to make it readable, and I do:

var readable01 = new Date(passed01);
var readable02 = new Date(passed02);

But it gives me a year of some ~1970... How can I get an output of something like: "3 years" or "3 year and 15 days and 17 minutes and 23 seconds" or something similar to that?

ecg8
  • 1,362
  • 1
  • 12
  • 16
bokuyaza
  • 13
  • 2
  • On the first line you're subtracting the current time out of the year 2015, so I guess javascript would indeed return a odd date, considering there's no "negative" date in javascript afaik. – Tiramonium May 28 '18 at 16:35
  • @Tiramonium - There are negative dates in JS; let d = new Date(-1); console.log(d); output: 1969-12-31T23:59:59.999Z – terary May 28 '18 at 16:39
  • Sorry about that :) You're right... I was just typing it from a memory. But it is still the same... I will update the code. – bokuyaza May 28 '18 at 16:41
  • Ok, what I meant was actually "why would you want a negative date to begin with", but never mind that. Try taking a look at this answer: https://stackoverflow.com/a/3067896/1361831 – Tiramonium May 28 '18 at 16:42
  • @bokuyaza - I would recommend searching a little more for js time/date functions. What you are trying to do is fairly simple and done in the past. I would recommend converting date to second, do your calculations then convert to readable format: [link]https://stackoverflow.com/questions/45124401/javascript-convert-date-format-to-seconds let d = new Date().getTime() – terary May 28 '18 at 16:44

1 Answers1

1

To answer your question, the reason your code gives a result of 1970-something is because of the way you are using new Date(). New Date is a JS function that returns the UNIX date of whatever number of time you send to it. Since you are sending only one argument, that argument is treated as milliseconds and it is returning the time of that number of milliseconds after the UNIX epoch on January 1, 1970.

https://www.w3schools.com/js/js_dates.asp

There are lots of ways to convert milliseconds into days, hours, minutes, and seconds. To convert to months or years the maths get wonky because of the fact that not all months have the same number of days, leap years, etc. Here is one function that I picked up and use (I forgot the source to give proper attribution)

function convertMS(ms) {
  var d, h, m, s;
  s = Math.floor(ms / 1000);
  m = Math.floor(s / 60);
  s = s % 60;
  h = Math.floor(m / 60);
  m = m % 60;
  d = Math.floor(h / 24);
  h = h % 24;
  return d + " days, " + h + " hours, " + m + " minutes, " + s + " seconds.";
};

document.getElementById("time").innerHTML = convertMS(8000000000);
<div id="time"></div>
ecg8
  • 1,362
  • 1
  • 12
  • 16