1,如上图,想要实现时间实时变化
 

    //vue  methods函数中函数,取nowTime值放在页面中即可看见效果,如果是web页面自己转成函数即可

    nowTimes(){
      this.timeFormate(new Date());
      setInterval(this.nowTimes,1000);
      this.clear()
    },
    // 显示当前时间(年月日时分秒)
    timeFormate(timeStamp) {
      let year = new Date(timeStamp).getFullYear();
      let month =new Date(timeStamp).getMonth() + 1 < 10? "0" + (new Date(timeStamp).getMonth() + 1): new Date(timeStamp).getMonth() + 1;
      let date =new Date(timeStamp).getDate() < 10? "0" + new Date(timeStamp).getDate(): new Date(timeStamp).getDate();
      let hh =new Date(timeStamp).getHours() < 10? "0" + new Date(timeStamp).getHours(): new Date(timeStamp).getHours();
      let mm =new Date(timeStamp).getMinutes() < 10? "0" + new Date(timeStamp).getMinutes(): new Date(timeStamp).getMinutes();
      let ss =new Date(timeStamp).getSeconds() < 10? "0" + new Date(timeStamp).getSeconds(): new Date(timeStamp).getSeconds();
      this.nowTime = year + "年" + month + "月" + date +"日"+" "+hh+":"+mm+':'+ss ;
    },
    clear(){
      clearInterval(this.nowTimes)
      // this.nowTimes = null;
    },

 

2,如上图,将时间戳转化为可用的 (6/28)格式,nS是后台返回的时间戳 

 

  //此函数用于vue项目,如在web端使用转换一下即可使用

myTime(nS) {
      if (nS == "" || nS == null) {
        var noday = "0";
        return noday;
      } else {
        var mydate = new Date(parseInt(nS));
        var days = mydate.getDate();
        var today = mydate.getMonth() + 1; //返回月份,因为返回值是0开始,表示1月,所以做+1处理
        return today + "/" + days;
      }
    }

 

 

3,如上图剩余时间的计算 ,需填写登记时间和使用年限,然后自动计算出设备剩余的使用时间

//  this.addForm.eregisterTime 表示登记时间   this.addForm.edurableYears表示使用年限

computed: {
    // 计算剩余时间
    residueTime() {
      if (this.addForm.eregisterTime && this.addForm.edurableYears) {
        var dateSpan, tempDate, iDays;
        var sDate1 = new Date().getTime();
        var eregisterTime = new Date(this.addForm.eregisterTime)
          .toLocaleString()
          .replace(/\//g, "-")
          .replace(/上午/g, "")
          .replace(/下午/g, "");
        var sDate2 =
          parseInt(eregisterTime.slice(0, 4)) +
          parseInt(this.addForm.edurableYears) +
          "" +
          eregisterTime.slice(4);
        console.log(sDate1);
        console.log(sDate2);
        console.log(new Date(sDate2).getTime());
        dateSpan = new Date(sDate2).getTime() - sDate1;
        console.log(dateSpan);
        dateSpan = dateSpan;
        iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
        console.log(iDays);
        return Math.floor(iDays / 365) + "年" + iDays % 365 + "天";
      } else {
        return "未知";
      }
    }
  },

 

4,过滤时间戳为年月日 或者 月/日 星期几

/**

* 过滤时间戳为年月日 或者 月/日 星期几

*/

getDateFilter: function(time, splitStr) {

 

if (!time) return '';

 

var date = getDate(time);

var y = date.getFullYear();

var M = date.getMonth() + 1;

var d = date.getDate();

var week;

switch (date.getDay()) {

case 0:

week = "星期天";

break;

case 1:

week = "星期一";

break;

case 2:

week = "星期二";

break;

case 3:

week = "星期三";

break;

case 4:

week = "星期四";

break;

case 5:

week = "星期五";

break;

case 6:

week = "星期六";

}

 

if (M < 10) M = "0" + M;

if (d < 10) d = "0" + d;

 

if (splitStr)

return y + splitStr + M + splitStr + d;

else

return M + '/' + d+' '+week;

},

 

//获取当前时间,格式YYYY-MM-DD

const getNowFormatDate = function () {

var date = new Date();

var seperator1 = "-";

var year = date.getFullYear();

var month = date.getMonth() + 1;

var strDate = date.getDate();

if (month >= 1 && month <= 9) {

month = "0" + month;

}

if (strDate >= 0 && strDate <= 9) {

strDate = "0" + strDate;

}

var currentdate = year + seperator1 + month + seperator1 + strDate;

return currentdate;

}

 

//将时间戳转化普通日期格式(以毫秒转)

function getLocalTime(nS) {

//return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');

if (nS == "" || nS == null) {

var noday = "/";

return noday;

return false;

} else {

var mydate = new Date(parseInt(nS));

var getminutes = mydate.getMinutes();

if (getminutes < 10) {

getminutes = '0' + getminutes;

}

var today = '';

today += mydate.getFullYear() + '-'; //返回年份

today += mydate.getMonth() + 1 + '-'; //返回月份,因为返回值是0开始,表示1月,所以做+1处理

today += mydate.getDate() + ' '; //返回日期

today += mydate.getHours() + ':';

today += getminutes;

return today;

}

}

 

JS将Unix时间戳转换为英文日期格式

function enFormatDate(millinSeconds) {

var date = new Date(millinSeconds);

var monthArr = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

// var suffix = new Array("st", "nd", "rd", "th");

 

var year = date.getFullYear(); //年

var month = monthArr[date.getMonth()]; //月

var ddate = date.getDate(); //日

 

// if (ddate % 10 < 1 || ddate % 10 > 3) {

// ddate = ddate + suffix[3];

// } else if (ddate % 10 == 1) {

// ddate = ddate + suffix[0];

// } else if (ddate % 10 == 2) {

// ddate = ddate + suffix[1];

// } else {

// ddate = ddate + suffix[2];

// }

return ddate + " " + month + " " + year;

}

Logo

前往低代码交流专区

更多推荐