JS实现时间格式的各种转化记录

//明天也就是当前时间加一天
function formatDate(n, detailed) {
    if (isNaN(parseFloat(n, 10)) || isNaN(n)) return '-';
    let o = new Date(parseInt(n, 10));
    let y = o.getFullYear(),
        d = o.getDate() < 10 ? '0' + o.getDate() : o.getDate(),
        m = (o.getMonth() + 1) < 10 ? '0' + (o.getMonth() + 1) : (o.getMonth() + 1),
        h = o.getHours() < 10 ? '0' + o.getHours() : o.getHours(),
        s = o.getMinutes() < 10 ? '0' + o.getMinutes() : o.getMinutes(),
        e = o.getSeconds() < 10 ? '0' + o.getSeconds() : o.getSeconds();
    if(detailed == 'seconds'){
        return '' + y + '-' + m + '-' + d + '  ' + h + ':' + s + ':' + e
    }
    return !!detailed ? '' + y + '-' + m + '-' + d + '  ' + h + ':' + s : '' + y + '-' + m + '-' + d;
}
var c = new Date();
formatDate(+new Date((c/1000+86400)*1000));
// 将时间格式转化为时分秒为00:00:00的时间戳
function formatTimeRub(date) {
     date.setDate(date.getDate());
     date.setHours(0)
     date.setMinutes(0);
     date.setSeconds(0);
     date.setMilliseconds(0);
    return date.getTime();
}

formatTimeRub(new Date())
// 获取时间对象 Fri Jul 14 2017 16:57:15 GMT+0800 (CST)
new Date()
// 将时间对象转化为时间戳 1500022635427
new Date().getTime()
// 将时间戳(1500022635427)转化为 -> YYYY/MM/DD | YYYY-MM-DD ...
function getDate(time, symbol){ 
    var y=new Date(time).getFullYear();
    var m = new Date(time).getMonth() > 9 ? Number(new Date(time).getMonth()) + 1 : '0' + Number(new Date(time).getMonth() + 1);
    var d = new Date(time).getDate() > 9 ? new Date(time).getDate() : '0' + new Date(time).getDate();
    return y+symbol+m+symbol+d; 
}
// 将20170714转化为 -> YYYY-MM-DD | YYYY/MM/DD ...
function test(time, symbol){
    return time.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1'+symbol+'$2'+symbol+'$3')
}

未完待续 。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值