1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <script src="./lib/vue.js"></script>
    9. </head>
    10. <body>
    11. <div id="app">
    12. <p>{{ currentDate|dateformat("11") }}</p>
    13. </div>
    14. <script>
    15. // 定义一个过滤器,名字叫做dateformat,可以接受一个参数,判断参数要什么格式,如果是yyyy-mm-dd,就会显示年月日,否则显示具体时间.
    16. Vue.filter("dateformat",function(datestr,patten=""){
    17. dt = new Date(datestr);
    18. y = dt.getFullYear();
    19. m = dt.getMonth()+1
    20. d = dt.getDate()
    21. if(patten.toLocaleLowerCase === 'yyyy-mm-dd'){
    22. return `${y}-${m}-${d}`
    23. } else {
    24. hh = dt.getHours()
    25. mm = dt.getMinutes()
    26. ss = dt.getSeconds()
    27. return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    28. }
    29. })
    30. var vm=new Vue({
    31. el:'#app',
    32. data:{
    33. currentDate: new Date()
    34. },
    35. methods:{}
    36. });
    37. </script>
    38. </body>
    39. </html>

    image.png