JavaScript 日期格式化大全

//如果侵权请联系本主删除,尊重知识和版权

原文地址https://www.cnblogs.com/fengwenzhee/p/8733760.html
function today(){
    var today=new Date();
    var h=today.getFullYear();
    var m=today.getMonth()+1;
    var d=today.getDate();
    var hh=today.getHours();
    var mm=today.getMinutes();
    var ss=today.getSeconds();
    m= m<10?"0"+m:m;     
    d= d<10?"0"+d:d;
    hh = hh < 10 ? "0" + hh:hh;
    mm = mm < 10 ? "0" +  mm:mm;
    ss = ss < 10 ? "0" + ss:ss;
    return h+"-"+m+"-"+d+" "+hh+":"+mm+":"+ss;
}

 
 Date.prototype.Format = function (fmt) { //author: meizz
        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()
        };
        var year = this.getFullYear();
        var yearstr = year + '';
        yearstr = yearstr.length >= 4 ? yearstr : '0000'.substr(0, 4 - yearstr.length) + yearstr;
 
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (yearstr + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
 
    //参数:var val = '/Date(1400046388387)/';
    //返回值格式:2014-05-14
    function format2(val) {
        if (val != null) {
            var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
            //月份为0-11,所以+1,月份小于10时补个0
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            return date.getFullYear() + "-" + month + "-" + currentDate;
        }
        return "";
    }
 
    //原文地址  https://blog.csdn.net/themagickeyjianan/article/details/81903041
    //参数:var str = '/Date(1400046388387)/';
    //返回值格式:2014-04-14    在原文中略加修改
    function format1(str) {
        var date = eval('new ' + str.substr(1, str.length - 2)); //new Date()
        var n = date.getFullYear();
        var y = date.getMonth() + 1;
        var r = date.getDate();
        var mytime = date.toLocaleTimeString();
        var mytimes = n + "-" + y + "-" + r + " " + mytime;
        return mytimes;
    }
 
    //原文地址 https://www.cnblogs.com/chenmengyu/p/6479319.html
    //这个比较历害可以显示上午和下午,星期等,本例中我没让其显示
    //参数:var str = '/Date(1400046388387)/';
    //返回值格式:2014-04-14 13:46:28
    function format(str) {
        var date = eval('new ' + str.substr(1, str.length - 2)); //new Date()
        var days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        //获取date的年份
        var year = date.getFullYear();
        //获取date的月份,将月份格式化成两位数保存
        var month = date.getMonth() < 10 ? "0" + date.getMonth() : date.getMonth();
        //获取date的日期,将日期格式化成两位数保存
        var d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        //从days数组中获得date的星期对应位置的星期名
        var day = days[date.getDay()];
        //获得date的小时,保存为两位数
        var hour = date.getHours();
        var am = hour < 12 ? "上午" : "下午";
        //hour = hour < 12 ? hour : hour - 12;//这里判断上午和下午
        hour = hour < 10 ? "0" + hour : hour;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        //return year + "年" + month + "月" + d + "日 " + day + " " + am + " " + hour + ":" + minute + ":" + second;
        return year + "-" + month + "-" + d + " " + hour + ":" + minute + ":" + second;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值