在vue、html中手动写日期格式化转换为“yyyy-MM-dd hh:mm:ss”

作前端页面的时候,貌似日期格式化是没有预置的,须要本身写(固然大部分都是直接C+V了),可是以为那些方法不容易看懂,并且对于小白来讲,用起来也不是那么地方便,因而本身写了一个简单的日期格式化函数。前端

在实际应用中,我我的以为“yyyy-MM-dd hh:mm:ss”2018-05-17 09:27:50 一种格式就能够了,不须要那么多花样,这种格式看起来比较整齐美观,固然,这段代码很简单,你随时能够本身修改为本身想要的格式。
vue

废话很少说,在vue的methods中,我定义了这么一个名为dateFormat的函数:函数

//时间格式化函数,此处仅针对yyyy-MM-dd hh:mm:ss 的格式进行格式化
dateFormat:function(time) {
    var date=new Date(time);
    var year=date.getFullYear();
    /* 在日期格式中,月份是从0开始的,所以要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
    var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
    var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
    var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
    var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
    // 拼接
    return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
}

在使用的时候,直接传参便可。spa

{{dateFormat(create_time)}}


卖家秀(效果图)

使用前:orm

使用后:blog


是否是感受舒服了好多呢?get