zoukankan      html  css  js  c++  java
  • 时间戳转换



    Date.prototype.capacityTime = function(precision) {
    let now = Utils.ajax.sysTime || new Date().getTime();
    let sec = parseInt((now - this.getTime())/1000),result;
    let timeFmt = new Date(now).format("HH:mm"),dire = sec > 0,direDesc = dire ? "前" :"后";
    let ONE_MINUTE = 60,ONE_HOUR = 3600,ONE_DAY = 86400,ONE_WEEK = 604800;
    let calcDiffNum = (diffNum)=>Math.round(diffNum <= 0 ? 1 : diffNum);
    sec = Math.abs(sec);

    if (sec < 15) {
    result = "刚刚";
    } else if (sec < ONE_MINUTE) {
    result = sec + "秒" + direDesc;
    } else if (sec < 45 * ONE_MINUTE) {
    result = calcDiffNum(sec / 60) + "分钟" + direDesc;
    } else if (sec < 24 * ONE_HOUR) {
    var diffNum = calcDiffNum(sec / 60 / 60) ;
    var isSameDay = new Date( now + sec * 1000 * (dire ? -1:1)).format("dd") == new Date(now).format("dd");
    result = isSameDay ? (diffNum + "小时" + direDesc) : (dire ? "昨天" :"明天");
    } else if (sec < 48 * ONE_HOUR) {
    result = (dire ? "昨天 " :"明天 ") + timeFmt;
    } else if (sec < 30 * ONE_DAY) {
    result = calcDiffNum(sec / 60 / 60 / 24) + "天" + direDesc;
    } else if (sec < 12 * 4 * ONE_WEEK) {
    result = calcDiffNum(sec / 60 / 60 / 24 / 30) + "月" + direDesc;
    } else {
    result = calcDiffNum(sec / 60 / 60 / 24 / 365) + "年" + direDesc;
    }
    if (precision === "dd" ) {
    result = /[天月日]/g.test(result) ? result : "今天";
    }
    return result;
    };
  • 相关阅读:
    C语言中for循环的使用
    详解C语言的main函数
    计算机语言的发展(the history of computer's language)
    hdu 1001
    hoj 1002
    hdu 1000
    POJ 1000(水题)
    hoj 1001
    code hunt题解(1)
    《C和指针》学习笔记(3)
  • 原文地址:https://www.cnblogs.com/chengyalin/p/9559502.html
Copyright © 2011-2022 走看看