Chanky Mallick
Chanky Mallick

Reputation: 587

How to Convert Seconds into Year, Month, Days, Hours, Minutes respectively?

I implemented this logic where I am getting difference between 2 dates and times from Moment.js as seconds, and I need to convert these seconds into 1 Year 3 Months 10 Days 5 Hours 45 Minutes format.

var numyears = Math.floor(seconds / 31536000);
var nummonths = Math.floor((seconds % 31536000) / 2628000);
var numdays = Math.floor(((seconds % 31536000) % 2628000) / 86400);
var numhours = Math.floor((((seconds % 31536000) % 2628000) % 86400)/3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);

I am getting the correct values up to days, but it's having problem from hours onwards.

Upvotes: 3

Views: 11976

Answers (3)

Ahmed Dghaies
Ahmed Dghaies

Reputation: 71

function getDateDiff(expires_in) {
let result = {
    years_diff: 0,
    months_diff: 0,
    days_diff: 0,
    hours_diff: 0,
}
if (expires_in) {
    result.years_diff = Math.floor(expires_in / 31536000);
    result.months_diff = Math.floor((expires_in % 31536000) / 2628000);
    result.days_diff = Math.floor(((expires_in % 31536000) % 2628000) / 86400);
    result.hours_diff = Math.floor((((expires_in % 31536000) % 2628000) % 86400) / 3600);
}
return result }

Here's how i approached this, running some unit tests i discovered that @Aun Shanbaz Awan solution gives wrong hours difference

Upvotes: 3

Aun Shahbaz Awan
Aun Shahbaz Awan

Reputation: 785

function TimeCalculator(seconds) {
  let y = Math.floor(seconds / 31536000);
  let mo = Math.floor((seconds % 31536000) / 2628000);
  let d = Math.floor(((seconds % 31536000) % 2628000) / 86400);
  let h = Math.floor((seconds % (3600 * 24)) / 3600);
  let m = Math.floor((seconds % 3600) / 60);
  let s = Math.floor(seconds % 60);

  let yDisplay = y > 0 ? y + (y === 1 ? " year, " : " years, ") : "";
  let moDisplay = mo > 0 ? mo + (mo === 1 ? " month, " : " months, ") : "";
  let dDisplay = d > 0 ? d + (d === 1 ? " day, " : " days, ") : "";
  let hDisplay = h > 0 ? h + (h === 1 ? " hour, " : " hours, ") : "";
  let mDisplay = m > 0 ? m + (m === 1 ? " minute " : " minutes, ") : "";
  let sDisplay = s > 0 ? s + (s === 1 ? " second" : " seconds ") : "";
  return yDisplay + moDisplay + dDisplay + hDisplay + mDisplay + sDisplay;
}

Upvotes: 1

Jason Crickmer
Jason Crickmer

Reputation: 41

Dates and calendars are political, and don't always match up to simple arithmetic. Most languages that I have worked in solve this problem for you, and to my knowledge, JavaScript does this. (Note that we will have another leap second added to our calendar on June 30, 2015 - http://en.wikipedia.org/wiki/Leap_second.)

Given all of that, this answer is very much an approximation, as it does not take into account lengths of months, leap days, or leap seconds. But, it is something:

var x = new Date('2015-02-26 12:32:54-0600'); // or if you have milliseconds, use that instead
var y = new Date('2015-03-19 01:22:09-0600');
var z = new Date(y-z);
z;
// returns "Wed Jan 21 1970 06:49:15 GMT-0600 (CST)"
// now compare this with epoch
var epoch = new Date('1970-01-01 00:00:00-0600');
var diff_years = z.getYear() - epoch.getYear();
var diff_month = z.getMonth() - epoch.getMonth();
var diff_days = z.getDate() - epoch.getDate();
var diff_hours = z.getHours() - epoch.getHours();
var diff_minutes = z.getMinutes() - epoch.getMinutes();

Upvotes: 3

Related Questions