Vue中 es6方法padStart结合过滤器 格式化时间


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
head>
<body>
    <div id="lcp">
        {
    {date|time}}
    div>
body>
html>
<script src="../day01/vue.js">script>
<script>
    //全局过滤器
    Vue.filter('time',function(val){
      
        //将时间转换为对象
        //获取到年月日时分秒,然后根据需求拼杰字符串
        var date = new Date(val)
        var Y = date.getFullYear();
        var M = date.getMonth();
        var D = date.getDate();
        var h = date.getHours();
        var f = date.getMinutes()+'';
            f = f.padStart(2,0)
        var m = date.getSeconds()+'';
            m = m.padStart(2,0)
        return `${
        Y}/${
        M}/${
        D} ${
        h}:${
        f}:${
        m}`
        //padStart(目标位数,"填充的字符") 不够目标位数  用填充的字符来填充
    })
    new Vue({
      
        el:'#lcp',
        data:{
      
            date:new Date(),
        }
    })
script>

你可能感兴趣的:(vue.js)