日期转换dateFormat-自定义格式‘yyyy-MM-dd HH:mm:ss‘

  1. 日期格式'-'转'/'。

日期通过new Date('2018-08-08')转换会有浏览器兼容问题,最好转换为new Date('2018/08/08')格式。

function dateTypeChange(date){
    if(date){
            date=date.replace(/-/g,'/');
    }
    return date;
};
  1. 日期格式转换方法1:

使用实例:dateFormat(dateTypeChange('2018-08-08 18:09:20'),'yyyy-MM-dd HH:mm:ss');

使用实例:dateFormat('2018/08/08 18:09:20','yyyy-MM-dd HH:mm:ss');

'yyyy-MM-dd hh:mm:ss',可以自己定义。例如:'yyy年MM月dd日 hh时mm分ss秒'。

/**
a:参数格式:new Date(2018/08/08 12:04:05)
b:日期格式'yyyy-MM-dd HH:mm:ss'
*/
function dateFormat(a,b) {
    if (typeof a === 'string')
        return a;
    var o = {
        y: a.getFullYear(),
        m: a.getMonth(),
        d: a.getDate(),
        h: a.getHours(),
        i: a.getMinutes(),
        s: a.getSeconds(),
        w: (a.getDay() || 7)
    };
    return (b || _date_sf).replace('yyyy', o.y).replace('MM', strPad(o.m + 1)).replace('dd', strPad(o.d)).replace('HH', strPad(o.h))
        .replace('mm', strPad(o.i)).replace('ss', strPad(o.s))
        .replace('M', o.m + 1).replace('d', o.d).replace('H', o.h).replace('m', o.i).replace('s', o.s);
};
  1. 日期格式转换方法2:

使用实例:new Date().format("yyyy-MM-dd hh:mm:ss");

'yyyy-MM-dd hh:mm:ss',可以自己定义。例如:'yyy年MM月dd日 hh时mm分ss秒'。

Date.prototype.format = function (fmt) {
    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(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").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;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值