969188664
贫民
贫民
  • 最后登录2020-02-13
  • 发帖数2
阅读:6630回复:1

时间的格式 和转换 vue中使用moment.js

楼主#
更多 发布于:2019-11-25 21:58
  当前时间
 
var d = new Date();

//里面不填东西的话是输出当前时间
console.log(d);

// 输出:Wed Feb 13 2019 16:49:31 GMT+0800 (中国标准时间)
var d = new Date().getTime()

// 后面加 getTime()就是把当前的(中国标准时间)转成时间戳输出 console.log(d) // 输出是当前时间戳  1574686929197;
var d = new Date(1574686929197)

console.log(d) //是把你的时间戳转成(中国标准时间) Mon Nov 25 2019 21:02:09 GMT+0800
  时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
function timestampToTime(timestamp) {
var date = new Date(timestamp * 1000);

//时间戳为10位需*1000,时间戳为13位的话不需乘1000

        //上面这句话是将时间戳转换成(中国标准时间)这种格式才能使用对应的方法和属性
var Y = date.getFullYear() + '-';
 var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
 var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
 var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
 var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
 return Y+M+D+h+m+s;

    }

timestampToTime(1529111288);

vue中使用moment.js

1.安装

 2. 在main.js中导入并且做一定配置
  1. import moment from 'moment'   //导入文件

  2. Vue.prototype.$moment = moment;    //赋值使用
  3. moment.locale('zh-cn');  //需要汉化

  4. 组件(.vue)文件中同样需要导入(script标签中)
  5. import moment from 'moment'   //导入文件
  6. 设置过滤器
  7. filters:{
        datefilter:function(value){
          return moment(value).format('MMMM Do YYYY, h:mm:ss a');
        }
      },
  8. html中
  9. <span>pw_nowdate|datefilter</span>
[969188664于2019-11-27 23:57编辑了帖子]
xufanghe
新手
新手
  • 最后登录2019-12-03
  • 发帖数3
沙发#
发布于:2019-11-25 22:05
666666666
游客


返回顶部

公众号

公众号