将分钟转换为天,周,月和年 [英] Converts minutes into days, week, months and years

查看:99
本文介绍了将分钟转换为天,周,月和年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数字代表从开始时间到现在的分钟数。

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.

这里有一个例子:

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?

推荐答案

也许是这样的?

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]

}

这篇关于将分钟转换为天,周,月和年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆