JavaScript-秒到可读格式?

JavaScript-秒到可读格式?,javascript,Javascript,我有一个带有数字的变量,例如1235752564445799865346-我如何将其转换为可读格式,例如,如何将其显示为4万亿世纪,或实际等效的格式 因此,例如29030400000秒将显示为1millennium?我将使用两个缩放函数。这是一个通过不同比例因子的数组应用“尺蠖”的方法。YMMV var p = function (n) { return Math.pow(10, n) } // Searching a sorted of array of "scaling factors"

我有一个带有数字的变量,例如
1235752564445799865346
-我如何将其转换为可读格式,例如,如何将其显示为
4万亿世纪
,或实际等效的格式


因此,例如
29030400000
秒将显示为
1millennium

我将使用两个缩放函数。这是一个通过不同比例因子的数组应用“尺蠖”的方法。YMMV

var p = function (n) { return Math.pow(10, n) }

// Searching a sorted of array of "scaling factors" to find the
// first scale such that x/scale >= 1 and x/scale < limit, handling
// cases where these conditions cannot be met.
//
// Returns { scaled: number, name: scalingName }
function scaleInto(x, limit, es) {
    var beste = es[0];   
    for (var i = 1; i < es.length; i++) {
        var e = es[i];
        var scaled = x / e.scale;
        // Scaling fits, or last scale
        if (scaled >= 1
            || i == (es.length - 1)) {
            beste = e;
        }
        if (scaled < limit) {
            break;
        }
    }

    return {
        scaled: x / beste.scale,
        name: beste.name
    }
}

var timeScale = [
    {name: "year", scale: 1 },
    {name: "decade", scale: p(1) },
    {name: "century", scale: p(2) },
    {name: "millenium", scale: p(3) }
]

/* short-scale */
var modScale = [
    {name: "", scale: 1 },
    {name: "hundred", scale: p(2) },
    {name: "thousand", scale: p(3) },
    {name: "million", scale: p(6) },
    {name: "billion", scale: p(9) },
    {name: "trillion", scale: p(12) },
    {name: "quadrillion", scale: p(15) }
]

function humanizeYears(years, ts, ms) {
    if (years < 1) {
        return "less than a year"
    }
    var r1 = scaleInto(years, 100, ts || timeScale);
    var r2 = scaleInto(r1.scaled, 100, ms || modScale)
    if (r2.name) {
        return r2.scaled.toFixed(0) + " " + r2.name + " " + r1.name
    } else {
        return r2.scaled.toFixed(0) + " " + r1.name
    }
}

function humanizeSeconds(seconds, ts, ms) {
    var years = seconds / (3600 * 24 * 365.25)
    console.log("years: " + years)
    return humanizeYears(years, ts, ms);
}
var p=function(n){return Math.pow(10,n)}
//搜索已排序的“比例因子”数组以查找
//第一个刻度,使x/scale>=1和x/scale<极限,处理
//无法满足这些条件的情况。
//
//返回{scaling:number,name:scalingName}
函数scaleInto(x,limit,es){
var beste=es[0];
对于(变量i=1;i=1
||i==(es.length-1)){
beste=e;
}
if(标度<极限){
打破
}
}
返回{
比例:x/最佳比例,
姓名:beste.name
}
}
var时间刻度=[
{名称:“年”,比额表:1},
{名称:“十年”,比额表:p(1)},
{名称:“世纪”,比例尺:p(2)},
{名称:“千年”,比例尺:p(3)}
]
/*短期*/
var modScale=[
{名称:'',比例:1},
{名称:“百”,比例:p(2)},
{名称:“千”,比额表:p(3)},
{名称:“百万”,比额表:p(6)},
{名称:“十亿”,比例:p(9)},
{名称:“万亿”,比额表:p(12)},
{名称:“万亿”,比例:p(15)}
]
功能(年、ts、ms){
如果(年数<1){
返回“不到一年”
}
var r1=标度单位(年,100,ts | |时标);
var r2=比例输入(r1.scaled,100,ms | | modScale)
如果(r2.名称){
返回r2.scaled.toFixed(0)+“”+r2.name+“”+r1.name
}否则{
返回r2.scaled.toFixed(0)+“”+r1.name
}
}
功能秒(秒、秒、毫秒){
变量年=秒/(3600*24*365.25)
console.log(“年:+年)
返回时间(年、ts、ms);
}
使用和输出:

// -> 92 decade
console.log(humanizeSeconds(29030400000))             
// -> 39 billion millenium
console.log(humanizeSeconds(1.2357525644457998e+21))

var centuriesOnly = timeScale.filter(function (v) {
    return v.scale <= p(2);
});
// -> 392 billion century 
console.log(humanizeSeconds(1.2357525644457998e+21, centuriesOnly))
/->92十年
控制台日志(秒(29030400000))
//->390亿千禧年
控制台日志(秒(1.2357525644457998e+21))
var centuriesOnly=timeScale.filter(函数(v){
回归规模:3920亿世纪
console.log(人性化秒(1.2357525644457998e+21,仅限centuriesOnly))

JS中没有任何内置的东西可以让您进行此转换。您需要找到一个库或编写自己的实现。到目前为止您尝试了什么?我相信人们会非常有帮助地从您已经开始的基础上获得一些东西。基本上:
var s=seconds;if(s>60){s=s/60;//minutes}if(s>60){s=s/60;//hours}if(s>24){s=s/24;//days}
etc.。这是一个比JavaScript不使用特殊类就可以处理的更大的整数。(MaxInt=9007199254740992)。如果处理的是这样大小的数字,则loose precision或b)需要一个特殊的BigInteger/BigNumber类。@JeremyJStarcher JavaScript可以处理这个数字-只是不能作为离散整数处理。虽然moment.js确实有“人性化”功能,但我不知道有任何datetime库支持如此荒谬的范围。。