最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Converts minutes into days, week, months and years - Stack Overflow

matteradmin12PV0评论

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?

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?

Share Improve this question edited Oct 18, 2011 at 20:22 antonjs asked Oct 18, 2011 at 19:45 antonjsantonjs 14.3k15 gold badges69 silver badges91 bronze badges 4
  • 1 Do you use some reference date for 0 minute? It might be needed for calculating the correct month length. – biziclop Commented Oct 18, 2011 at 19:47
  • Yes, the reference for 0 minutes is the current date. So var minutes = 2; means 2 minutes from now. – antonjs Commented Oct 18, 2011 at 19:56
  • You will have to be a lot more precise to get a decent answer. What do you mean by "months, weeks, and days?" Today is October 18th. How many months, weeks, and days are there between now and the same time on November 29th (35 days away)? How many days are there between 11:59PM today and 12:01AM tomorrow? Zero? One? – kevin cline Commented Oct 18, 2011 at 20:10
  • Use the JavaScript native Date object. – GAgnew Commented Oct 18, 2011 at 20:13
Add a comment  | 

5 Answers 5

Reset to default 13

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]

}

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.

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
}

You need to use division and modulus:

function getDataHR (newMinutes) {
    var hrData = "";

    var years = minutes / YEAR_IN_MINUTES; // int division = no remainder
    hrData += years + "years";
    minutes = minutes % YEAR_IN_MINUTES;

    // ... continue for months, weeks, days, hours, etc. in that order

    return hrData; // 1 year, 2 months, 2 week, 2 days, 12 minutes
}

Hey I have implemented some code that would some work for someone

const MINS_PER_YEAR = 24 * 365 * 60,
  MINS_PER_MONTH = 24 * 30 * 60,
  MINS_PER_WEEK = 24 * 7 * 60,
  MINS_PER_DAY = 24 * 60;

const hours = (minutes) => {
  const hour = Math.floor(minutes / 60);
  const remainingMinutes = minutes - hour * 60;
  return `${hour} hours ${remainingMinutes} minutes`;
};

const days = (minutes) => {
  const days = Math.floor(minutes / (24 * 60));
  const remainingMinutes = minutes - days * 24 * 60;
  const hour = hours(remainingMinutes);

  return `${days} days ${hour}`;
};

const weeks = (minutes) => {
  const weeks = Math.floor(minutes / (24 * 7 * 60));
  const remainingMinutes = minutes - weeks * (24 * 7 * 60);
  if (remainingMinutes <= MINS_PER_DAY) return `${weeks} weeks ${hours(remainingMinutes)}`;
  else return `${weeks} weeks ${days(remainingMinutes)}`;
};

const months = (minutes) => {
  const months = Math.floor(minutes / MINS_PER_MONTH);
  const remainingMinutes = minutes - months * MINS_PER_MONTH;
  if (remainingMinutes <= 60) return `${months} months ${hours(remainingMinutes)}`;
  else if (remainingMinutes <= MINS_PER_DAY) return `${months} months ${days(remainingMinutes)}`;
  else return `${months} months ${weeks(remainingMinutes)}`;
};

export const convertMinutes = (minutes) => {
  if (minutes <= 60) return `${minutes} minutes`;
  else if (minutes <= MINS_PER_DAY) return hours(minutes);
  else if (minutes <= MINS_PER_WEEK) return days(minutes);
  else if (minutes <= MINS_PER_MONTH) return weeks(minutes);
  else return months(minutes);
};
Post a comment

comment list (0)

  1. No comments so far