从小时计算剩余年/日/小时/分/秒

时间:2015-05-23 04:59:46

标签: javascript

您好我正在搜索从当前时间到输出小时数的计算器。我找到了几个样本,但他们正在使用日期或Unix时间。

我想要的是,

var output = "72:50:09",
    oneDay = "24:00:00",
    callculate = output / oneDay; //This must output 3 days

var checkRemaining = {
    calculateDiffrence: function() {
       var month,
           text;
       (callculate === 31) ? text = "1 month",
       (callculate === 62) ? text = "2 months",
       (callculate === 365) ? text = "1 year",
       return text;
    }
};
console.log(checkRemaining.calculateDiffrence);

像这样的东西,我想不出如何以正确的方式管理这个。 我希望有人可以帮助我,我会很感激。

计算years/days/hours/minutes/seconds时间output的数量。

2 个答案:

答案 0 :(得分:1)

计算天数:

callculate

现在var min = out[1]; //Will hold the minutes var sec = out[2]; //Will hold the seconds var hrs = out[0]%one[0] //Will hold the hours 将保留天数。

Year/Months

function是您根据InterfaceController计算的内容。

答案 1 :(得分:0)

编辑:您需要做的就是检查小时是否小于25.如果是,则处理数字(无日期/时间方法)。

var output = "00:50:09",
    oneDay = "24:00:00";

var arr = output.split(":");
if (parseInt(arr[0], 10) < 25) {
    // if less than a day then just process numbers
    result = arr[0] + "hrs " + arr[1] + "min " + arr[2] + "sec";
} else {
    // more than 24 hrs so use the Date/Time methods
    var out = new Date(0, 0, 0, parseInt(arr[0], 10),
                                parseInt(arr[1], 10),
                                parseInt(arr[2], 10));
    console.log(out);

    var arr = oneDay.split(":");
    var one = new Date(0, 0, 0, parseInt(arr[0], 10),
                                parseInt(arr[1], 10),
                                parseInt(arr[2], 10));
    console.log(one);

    var dif = new Date(out - one);
    console.log(dif);

    var result = dif.getYear() - 70 + "yr " + 
                 dif.getMonth() + "mth " + 
                 dif.getDate() + "day " + 
                 dif.getHours() + "hrs " + 
                 dif.getMinutes() + "min " + 
                 dif.getSeconds() + "secs";
}
console.log(result);

https://jsfiddle.net/pv0Lzcts/1/