簡體   English   中英

從小時數計算剩余的年/天/小時/分鍾/秒

[英]calculate remaining years/days/hours/minutes/seconds from hours

嗨,我正在尋找一個可以計算當前時間到輸出小時數的計算器。 我找到了幾個示例,但是它們正在處理日期或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);

像這樣的事情,我無法思考如何正確地管理它。 希望有人能幫助我,我會感激的。

計算output時間有多years/days/hours/minutes/seconds

要計算天數:

var output = "72:50:09",
oneDay = "24:00:00",
out = output.split(":").map(Number),
one = oneDay.split(":").map(Number),
callculate = out[0]/one[0]; //No of days

現在, 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計算的。

編輯:所有您需要做的就是檢查小時是否少於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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM