2014-02-27 40 views
1

我需要一個函數來轉換年,月和日的十進制值。例如:1。5年= 1年,6個月。 2.2527397260273973年= 2年,3個月和1天。Javascript函數將十進制年份值轉換爲年,月和日

我開始使用此功能:

function yearsToYearsMonthsDays(value){ 
var years = Math.floor(value); 
var months = Math.floor((value - years) /(1/12)); 
var days = Math.floor (((value - years) /(1/12) - Math.floor((value - years) /(1/12)))/(1/365)); 
var result = years + " years, " + months + " months, " + days + " days"; 
    Logger.log(result); 
} 

但有時它不工作(例如:0.75不產生9個月)。

任何幫助?

+0

精品工程,據我可以告訴。 http://jsfiddle.net/HaJW4/ –

+0

十進制年份可能會轉換爲一個日期,但它不會準確地轉換爲一個月和幾天的時間段,因爲月份的長度不相等,而且一個月的年份會發生變化。 – RobG

+0

我已經解決了類似的問題:http://stackoverflow.com/questions/29400171/how-do-i-convert-a-decimal-year-value-into-a-date-in-javascript/29400172。它處理的是您在實際年份(即2015.252739或2015年4月3日)進行交易的情況,但具有閏年分析,這意味着它不適合時間跨度(即2.252739或「2年3個月和1天' ) –

回答

3

我覺得最好的辦法是將所有變更爲天,例如:

function yearsToYearsMonthsDays(value) 
{ 
    var totalDays = value * 365; 
    var years = math.floor(totalDays/365); 
    var months = math.floor((totalDays-(years *365))/30); 
    var days = math.floor(totalDays - (years*365) - (months * 30)); 
    var result = years + " years, " + months + " months, " + days + " days"; 
    Logger.log(result); 
} 
+1

如果是閏年呢? – Naga

0

你的腳本在這裏工作得很好(在螢火蟲控制檯中測試過)。

你的函數調用以0.75爲參數生產:

0年,9個月,0天

那你究竟想讓它做?

如果你想讓它去掉「0」值,則需要對其進行測試,不把它們添加到字符串,如果他們是等於0,如:

var result = ""; 
if(years !=0){result += year + " years"} 
0

我的貢獻小數年轉換爲時間單位,從億萬年(十億年)至納秒(一秒鐘的十億),並用逗號格式化輸出略帶健談的風格。

很多方式轉換時間,並推定必須理解。這裏是我的:

這轉換基於1)每個公曆的平均月份爲30.436875天; 2)每4個月平均730.485小時,包括1個閏年; 3)每年8765.82小時(而不是8760)。這些內部常量當然不適合每個應用程序,但應該足夠用於一般用途。例如,我的功能增加了約一分鐘的時間來的OP的例子:

2.2527397260273973年= 2歲3個月零1天

其中

getTimeFromYears(2.2527397260273973); 

輸出「 2年,3個月,1天和57秒「。

一些單位似乎沒有必要,如當你有日,月,和十年當你有多年和幾個世紀,但那些可以很容易地加入到邏輯。運行該代碼段查看示例。

function getTimeFromYears(_years) { 
 
// to omit a unit from the output, multiply its value by zero; see gyears 
 

 
    "use strict"; 
 

 
    var _hoursInEon   = 8765820000000  // eon =   1,000,000,000 years (billion) 
 
     , _hoursInGalacticYear = 2016138600000  // galactic year = 230,000,000 years (230 million) 
 
     , _hoursInEpoch  = 8765820000  // epoch =   1,000,000 years (million) 
 
     , _hoursInMillenium =  8765820  // millenium =   1,000 years 
 
     , _hoursInCentury  =  876582 
 
     , _hoursInYear   =   8765.82 // often cited as 8760, or 730 * 12 // 365.2425 days in year 
 
     , _hoursInMonth  =   730.485 // hours average per month for a 4-year period which includes 1 leap year 
 
                // average month has 30.436875 days, Gregorian calendar 
 
     , _hoursInDay   =   24 
 
     , _hoursInHour   =    1 
 
     , _hoursInMinute  =    0.0166666666666667 
 
     , _hoursInSecond  =    0.000277778 
 
     , _hoursInMillisecond =    0.000000277778  // A millisecond is one-thousandth of a second 
 
     , _hoursInNanosecond =    0.000000000000277778 // A nanosecond is one-billionth of a second 
 

 

 
     , totalHours = _years * _hoursInYear 
 
     
 
     , eons = Math.floor( totalHours 
 
        /_hoursInEon) 
 

 
     , gyears = Math.floor((totalHours - (eons * _hoursInEon)) 
 
        /_hoursInGalacticYear) * 0 
 

 
     , epochs = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear)) 
 
        /_hoursInEpoch) 
 

 
     , mills = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch)) 
 
        /_hoursInMillenium) 
 

 
     , cents = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium)) 
 
        /_hoursInCentury) 
 

 
     , years = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury)) 
 
        /_hoursInYear) 
 
    
 
     , months = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear)) 
 
        /_hoursInMonth) 
 
    
 
     , days = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear) - (months * _hoursInMonth)) 
 
        /_hoursInDay) 
 
    
 
     , hours = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear) - (months * _hoursInMonth) - (days * _hoursInDay)) 
 
        /_hoursInHour) 
 
    
 
     , mins = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear) - (months * _hoursInMonth) - (days * _hoursInDay) - (hours * _hoursInHour)) 
 
        /_hoursInMinute) 
 

 
     , secs = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear) - (months * _hoursInMonth) - (days * _hoursInDay) - (hours * _hoursInHour) - (mins * _hoursInMinute)) 
 
        /_hoursInSecond) 
 

 
     , msecs = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear) - (months * _hoursInMonth) - (days * _hoursInDay) - (hours * _hoursInHour) - (mins * _hoursInMinute) - (secs * _hoursInSecond)) 
 
        /_hoursInMillisecond) 
 

 
     , nsecs = Math.floor((totalHours - (eons * _hoursInEon) - (gyears * _hoursInGalacticYear) - (epochs * _hoursInEpoch) - (mills * _hoursInMillenium) - (cents * _hoursInCentury) - (years * _hoursInYear) - (months * _hoursInMonth) - (days * _hoursInDay) - (hours * _hoursInHour) - (mins * _hoursInMinute) - (secs * _hoursInSecond) - (msecs * _hoursInMillisecond)) 
 
        /_hoursInNanosecond) 
 

 

 
     , _eonsPhrase = (eons < 1 ? '' : eons === 1 ? '1 eon'   : addCommas(eons ) + ' eons'   ) 
 
     , _gyearsPhrase = (gyears < 1 ? '' : gyears === 1 ? '1 galactic year' : addCommas(gyears) + ' galactic years') 
 
     , _epochsPhrase = (epochs < 1 ? '' : epochs === 1 ? '1 epoch'   : addCommas(epochs) + ' epochs'  ) 
 
     , _millsPhrase = (mills < 1 ? '' : mills === 1 ? '1 millenium'  : mills + ' millenia' ) 
 
     , _centsPhrase = (cents < 1 ? '' : cents === 1 ? '1 century'  : cents + ' centuries') 
 
     , _yearsPhrase = (years < 1 ? '' : years === 1 ? '1 year'   : years + ' years' ) 
 
     , _monthsPhrase = (months < 1 ? '' : months === 1 ? '1 month'   : months + ' months' ) 
 
     , _daysPhrase = (days < 1 ? '' : days === 1 ? '1 day'   : days + ' days'  ) 
 
     , _hoursPhrase = (hours < 1 ? '' : hours === 1 ? '1 hour'   : hours + ' hours' ) 
 
     , _minsPhrase = (mins < 1 ? '' : mins === 1 ? '1 minute'  : mins + ' minutes' ) 
 
     , _secsPhrase = (secs < 1 ? '' : secs === 1 ? '1 second'  : secs + ' seconds' ) 
 
     , _msecsPhrase = (msecs < 1 ? '' : msecs === 1 ? '1 millisecond' : addCommas(msecs) + ' milliseconds') 
 
     , _nsecsPhrase = (nsecs < 1 ? '' : nsecs === 1 ? '1 nanosecond' : addCommas(nsecs) + ' nanoseconds' ) 
 

 
     , _phrasePart = new Array(13) 
 
     , _phrasesInUse = 0 
 
     , _phrasesUsed = 0 
 
     , _joiner = ',' 
 
     , _result = '' 
 
    ; 
 

 

 
    //////////////////////////////////////////////////// 
 
    // cosmetic adjustments 
 
    if(eons > 999999) { _joiner = ';'; } 
 
    if(secs > 0 || mins > 0) { _msecsPhrase = _nsecsPhrase = ''; } 
 
    //////////////////////////////////////////////////// 
 

 
    _phrasePart[ 0 ] = _eonsPhrase; 
 
    _phrasePart[ 1 ] = _gyearsPhrase; 
 
    _phrasePart[ 2 ] = _epochsPhrase; 
 
    _phrasePart[ 3 ] = _millsPhrase; 
 
    _phrasePart[ 4 ] = _centsPhrase; 
 
    _phrasePart[ 5 ] = _yearsPhrase; 
 
    _phrasePart[ 6 ] = _monthsPhrase; 
 
    _phrasePart[ 7 ] = _daysPhrase; 
 
    _phrasePart[ 8 ] = _hoursPhrase; 
 
    _phrasePart[ 9 ] = _minsPhrase; 
 
    _phrasePart[ 10 ] = _secsPhrase; 
 
    _phrasePart[ 11 ] = _msecsPhrase; 
 
    _phrasePart[ 12 ] = _nsecsPhrase; 
 

 
    // count the phrase pieces to use 
 
    for(var i = 0; i < _phrasePart.length; i++) { 
 
     if(_phrasePart[ i ].length) { _phrasesInUse++; } 
 
    } 
 

 
    // assemble the output 
 
    for(var i = 0; i < _phrasePart.length; i++) { 
 
     if(_phrasePart[ i ].length) { 
 
      _result += _phrasePart[ i ]; 
 
      _phrasesUsed++; 
 

 
      // only for the last phrase 
 
      if(_phrasesInUse - _phrasesUsed === 1) { 
 
       _result += ' and '; 
 
      
 
      } else 
 
      if(_phrasesInUse - _phrasesUsed > 0) { 
 
       _result += (_joiner + ' '); 
 
      } 
 
     } 
 
    } 
 

 
    return _result; 
 
}; 
 

 

 
function addCommas(t) { 
 
    t += ""; var x = t.split("."), x1 = x[0], x2 = x.length > 1 ? "." + x[1] : ""; 
 
    for (var r = /(\d+)(\d{3})/; r.test(x1);) x1 = x1.replace(r, "$1,$2"); 
 
    return x1 + x2; 
 
}; 
 

 

 
console.log('getTimeFromYears(.75) -> ' + getTimeFromYears(.75)); // 9 months 
 
console.log('getTimeFromYears(9/12) -> ' + getTimeFromYears(9/12)); // 9 months 
 
console.log('getTimeFromYears(1/365.2425) -> ' + getTimeFromYears(1/365.2425)); // 1 day 
 
console.log('getTimeFromYears(1/365.2425 * 14) -> ' + getTimeFromYears(1/365.2425 * 14)); // 14 days 
 
console.log('getTimeFromYears(1/365.2425/24) -> ' + getTimeFromYears(1/365.2425/24)); // 1 hour 
 
console.log('getTimeFromYears(1/365.2425/24 * 730.485) -> ' + getTimeFromYears(1/365.2425/24 * 730.485)); // 1 month 
 
console.log('getTimeFromYears(1/365.2425 * 0.0000000000000115741) -> ' + getTimeFromYears(1/365.2425 * 0.0000000000000115741)); // 1 nanosecond 
 
console.log('getTimeFromYears(1/365.2425/24 * 0.000000000000277778) -> ' + getTimeFromYears(1/365.2425/24 * 0.000000000000277778)); // 1 nanosecond 
 
console.log('getTimeFromYears(1/365.2425/24 * 0.000000000000277778 * 12000) -> ' + getTimeFromYears(1/365.2425/24 * 0.000000000000277778 * 12000)); // 12,000 nanoseconds 
 
console.log('getTimeFromYears(1/365.2425/24 * 0.000000000000277778 * 1000000) -> ' + getTimeFromYears(1/365.2425/24 * 0.000000000000277778 * 1000000)); // 1 millisecond 
 
console.log('getTimeFromYears(1/365.2425/24 * 0.000000000000277778 * 12345678) -> ' + getTimeFromYears(1/365.2425/24 * 0.000000000000277778 * 12345678)); // 12 milliseconds and 345,678 nanoseconds 
 
console.log('getTimeFromYears(123456789) -> ' + getTimeFromYears(123456789)); // 123 epochs, 456 millenia, 7 centuries, 88 years, 11 months, 30 days, 10 hours, 29 minutes and 5 seconds 
 
console.log('getTimeFromYears() -> ' + getTimeFromYears()); // 9 eons, 876 epochs, 543 millenia, 2 centuries, 10 years and 11 seconds 
 
console.log('getTimeFromYears(2.2527397260273973) -> ' + getTimeFromYears(2.2527397260273973)); // 2 years, 3 months, 1 day and 57 seconds

相關問題