zoukankan      html  css  js  c++  java
  • moment.js

    moment.js使用

    http://momentjs.cn/

    安装:
    npm install moment --save # npm
    yarn add moment # Yarn

    然后再入口文件 main.js中导入并使用

    
    import moment from 'moment'//导入文件moment.js
    Vue.prototype.$moment = moment;//赋值使用
    //定义一个全局过滤器实现日期格式化
    Vue.filter('datafmt',function (input,fmtstring) {
      // 使用momentjs这个日期格式化类库实现日期的格式化功能
      return moment(input).format(fmtstring);
    })
    
    

    页面使用:
    这个时候,在项目就可以this.$moment来使用moment的一系列方法了。比如说,格式化时间

    this.$moment().format('MMMM Do YYYY, h:mm:ss a'); // 七月 17日 2018, 10:47:33 晚上
    this.$moment().format('dddd');                    // 星期二
    this.$moment().format("MMM Do YY");               // 7月 17日 18
    this.$moment().format('YYYY [escaped] YYYY');     // 2018 escaped 2018
    this.$moment().format();
    
    比如:
    let startTime = this.$moment(new Date()).format( "YYYY-MM-DD HH:mm:ss");  
    //返回 2020-12-12 12:00:00
    

    时间的加减乘除:

    加:this.$moment().add(number,"days");//加number天,后面的day可以换成相应的小时等等
    减:this.$moment().subtract(number,"days")//减number天,同上
    
    

    在div中使用|管道符过滤时间
    比如:

    <span>发布时间: {{item.add_time | datefmt('YYYY-MM-DD HH:mm:ss')}} </span>
    
  • 相关阅读:
    在线压缩与解压
    批处理删除文件del dos cmd
    git教程
    网页图标下载
    vs2010使用git
    配置 authorization deny allow
    mvc4下载
    nuget自动发布脚本
    OAuth2.0
    使用 HttpModel与现有基于共享登录信息( Cookie )的网站进行集成
  • 原文地址:https://www.cnblogs.com/xm0328/p/14188200.html
Copyright © 2011-2022 走看看