Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converts minutes into days, week, months and years

Tags:

javascript

Let's suppose I have a number which represents the minutes passed from the start time to now.

I wan to create a function which returns the years, months, week and days corresponding to the minutes I am passing to that function.

Here an example:

var minutes = 635052; // 635052 = (24*60)*365 + (24*60)*30*2 + (24*60)*14 + (24*60)*2 + 12;
getDataHR(minutes); // 1 year, 2 months, 2 week, 2 days, 12 minutes

function getDataHR (newMinutes) {
      minutes = newMinutes;
      .......
      return hrData; // 1 year, 2 months, 2 week, 2 days, 12 minutes
}

What is the best way to achieve the result?

like image 423
antonjs Avatar asked Oct 18 '11 19:10

antonjs


3 Answers

Maybe like this?

var units = {
    "year": 24*60*365,
    "month": 24*60*30,
    "week": 24*60*7,
    "day": 24*60,
    "minute": 1
}

var result = []

for(var name in units) {
  var p =  Math.floor(value/units[name]);
  if(p == 1) result.push(p + " " + name);
  if(p >= 2) result.push(p + " " + name + "s");
  value %= units[name]

}
like image 55
georg Avatar answered Oct 24 '22 01:10

georg


Building off of thg435's response above, I created a function that converts seconds to days, hours, minutes, and seconds:

function secondsToString(seconds)
{
   var value = seconds;

   var units = {
       "day": 24*60*60,
       "hour": 60*60,
       "minute": 60,
       "second": 1
   }

   var result = []

   for(var name in units) {
     var p =  Math.floor(value/units[name]);
     if(p == 1) result.push(" " + p + " " + name);
     if(p >= 2) result.push(" " + p + " " + name + "s");
     value %= units[name]
   }

   return result;

}

I also added some spaces to the result to give the commas some room. Output looks like:

1 day, 3 hours, 52 minutes, 7 seconds.
like image 33
skybondsor Avatar answered Oct 24 '22 01:10

skybondsor


I did it like this, because I didn't even know there was a modulo operator in javascript:

var minutes = 635052; // 635052 = (24*60)*365 + (24*60)*30*2 + (24*60)*14 + (24*60)*2 + 12;
getDataHR(minutes); // 1 year, 2 months, 2 week, 2 days, 12 minutes

function getDataHR (newMinutes) {
    MINS_PER_YEAR = 24 * 365 * 60
    MINS_PER_MONTH = 24 * 30 * 60
    MINS_PER_WEEK = 24 * 7 * 60
    MINS_PER_DAY = 24 * 60
    minutes = newMinutes;
    years = Math.floor(minutes / MINS_PER_YEAR)
    minutes = minutes - years * MINS_PER_YEAR
    months = Math.floor(minutes / MINS_PER_MONTH)
    minutes = minutes - months * MINS_PER_MONTH
    weeks = Math.floor(minutes / MINS_PER_WEEK)
    minutes = minutes - weeks * MINS_PER_WEEK
    days = Math.floor(minutes / MINS_PER_DAY)
    minutes = minutes - days * MINS_PER_DAY
    return years + " year(s) " + months + " month(s) " + weeks + " week(s) " + days + " day(s) " + minutes + " minute(s)"
    //return hrData; // 1 year, 2 months, 2 week, 2 days, 12 minutes
}
like image 37
jgritty Avatar answered Oct 24 '22 00:10

jgritty