JavaScript将时间格式化为yyyy-MM-dd hh:mm:ss

=================================================================

/*将时间格式化为yyyy-MM-dd hh:mm:ss
_num=1:显示带 时/分/秒
_num=0:只显示 年/月/日
_num=2:只显示 时/分/秒
*/

=================================================================

function GetDataTime(_num) {
    var today = new Date();
    var years = today.getFullYear();
    var months = (today.getMonth() + 1) < 10 ? ("0" + (today.getMonth() + 1).toString()) : today.getMonth() + 1;
    var day = today.getDate() < 10 ? ("0" + today.getDate().toString()) : today.getDate();

    var h = today.getHours() < 10 ? ("0" + today.getHours().toString()) : today.getHours();
    var m = today.getMinutes() < 10 ? ("0" + today.getMinutes().toString()) : today.getMinutes();
    var s = today.getSeconds() < 10 ? ("0" + today.getSeconds().toString()) : today.getSeconds();
    if (_num == 1) {
        return years + "-" + months + "-" + day + " " + h + ":" + m + ":" + s;
    }
    if (_num == 0) {
        return years + "-" + months + "-" + day;
    }
    if (_num == 2) {
        return h + ":" + m + ":" + s;
    }
    //document.write(years + "-" + months + "-" + day + " " + h + ":" + m + ":" + s);
}

=================================================================

posted @ 2013-10-31 14:42  xinhua_wen  阅读(255)  评论(0编辑  收藏  举报