js,將日期時分秒等格式化和轉化


 

1.將js Date對象格式化為指定格式,添加一個原型方法

/**
 * 返回指定format的string
 * format eg:'yyyy-MM-dd hh:mm:ss'
 **/
Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

使用如:
new Date().format('yyyy-MM-dd');    // echo: '2015-12-01'

 

2.得到昨天 (永遠相對於今天的前一天)

// 得到昨天的日期 返回昨天
function yesterday() {
    var today = new Date();
    var the_yesterday = today.setDate(today.getDate() - 1);
    return the_yesterday;
}

 

3. 得到上一個月的今天

// 得到近一個月 返回上一個月的今天
function lastMonth() {
    var today = new Date();
    var month = today.getMonth();
    var the_last_month;
    if (month == 0) {
        the_last_month = today.setMonth(11);
    } else {
        var the_last_month = today.setMonth(month - 1);
    }
    the_last_month =  new Date(the_last_month).format('yyyy-MM-dd');
    return the_last_month;
}

 

4.得到指定格式的最近一周 返回一個數組

// 得到近一周 返回數組   (note: 這里的format使用了上面的用來format原型方法)
function lastWeek(format) {
    var today = new Date();
    var the_week = [];
    var format = format || 'yyyy-MM-dd';
    for (var i = 1; i < 8; i++) {
        var temp = today.setDate(today.getDate()-1);
        temp = new Date(temp).format(format);
        the_week.unshift( temp );
    };
    return the_week;
}

 

5.得到近n天 返回一個數組

// 得到近n天 返回數組
function lastNDay(days,format) {
    var today = new Date();
    var the_days = [];
    var format = format || 'yyyy-MM-dd';
    for (var i = 1; i < days+1; i++) {
        var temp = today.setDate(today.getDate()-1);
        temp = new Date(temp).format(format);
        the_days.unshift( temp );
    };
    return the_days;
}

//使用如
lastNDay(7);  // 返回最近一周的日期數組 format參數可選

// [ '2011-11-01', '2011-11-02','2011-11-03','2011-11-04','2011-11-05','2011-11-06','2011-11-07' ]

 

6.把毫秒轉換成時長

// 把毫秒轉換成時長
// 返回 1.若小於等於60秒,顯示秒數
//     2.若大於1分鍾小於1小時,顯示分鍾
//     3.若大於1小時,顯示x小時x分鍾
function MillisecondToTime(msd) {
    var time = parseInt(msd) / 1000;
    if (time <= 60) {
        time = time + '秒';
        return time;
    } else if (time > 60 && time < 60 * 60) {
        time = parseInt(time / 60) + "分鍾";
        return time;
    } else {
        var hour = parseInt(time / 3600) + "小時";
        var minute = parseInt(parseInt(time % 3600) / 60) + "分鍾";
        time = hour + minute;
        return time;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM