用js格式化时间

    项目中会遇到各种小问题,例如在js中格式化时间,代码如下

//格式化日期,

    function formatDate(date,format){
        var paddNum = function(num){
            num += "";
            return num.replace(/^(\d)$/,"0$1");

        }
        //指定格式字符
        var cfg = {

            yyyy : date.getFullYear() //年 : 4位

            ,yy : date.getFullYear().toString().substring(2)//年 : 2位

            ,M  : date.getMonth() + 1  //月 : 如果1位的时候不补0

            ,MM : paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0

            ,d  : date.getDate()   //日 : 如果1位的时候不补0

            ,dd : paddNum(date.getDate())//日 : 如果1位的时候补0

            ,hh : paddNum(date.getHours()) //时

            ,mm : paddNum(date.getMinutes()) //分

            ,ss : paddNum(date.getSeconds()) //秒

        }
        format || (format = "yyyy-MM-dd hh:mm:ss");
        return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});

    }


在调用的时候只需要

var date = formatDate( new Date (json时间),"yyyy-MM-dd");
var time = formatDate( new Date ( json时间),"hh:mm:ss");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值