在写获取系统当前的时间的时候,我们首先要前了解一下JS中的Date对象的用法。

//用于获取年份

1、Date().getFullYear()

//获取当前月份,注意返回值是0-11,需要在后面+1

2、Date().getMonth()

//获取当前日

3、Date().getDate()

//获取当前时刻

4、Date().getHours()

//获取分钟

5、new Date().getMinutes()

//获取秒数

6、new Date().getSeconds()



1.index.js

 getNowDate:function(e){
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y =date.getFullYear();
    //获取月份
    var M =date.getMonth()+1;
    //获取当前天
    var D = date.getDate();
    //获取当前小时
    var H = date.getHours()
    console.log("当前时间:" + Y + '年' + M +'月' + D +'日' + H+'时'); 

  },

onShow: function () {
    this.getNowDate()
  
  },

2.效果图

 

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐