vue过滤器将后台传的时间戳转化为时间格式


getTime(time){
 			var times = new Date(time);
 	return times.getFullYear()+"年"+times.getMonth()+1+"月"+times.getDate()+"日";
	}
function getTime() {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let monthDay = date.getDate();
        let weekDay = date.getDay();
        let hour = date.getHours();
        let min = date.getMinutes();
        let second = date.getSeconds();
        let milSecond = date.getMilliseconds();
        let nowTime = year + "-"
            + (month < 10 ? "0" + month : month) + "-"
            + (monthDay < 10 ? "0" + monthDay : monthDay) + "  "
            //+ weekDay + " "
            + (hour < 10 ? "0" + hour : hour) + ":"
            + (min < 10 ? "0" + min : min);
            //+ ":" + (second < 10 ? "0" + second : second)
            //+ ":" + milSecond;
    return nowTime
}

将字符串转成时间

stringToDate(dateStr,separator){
    if(!separator){
      separator="-";
    }
    var dateArr = dateStr.split(separator);
    var year = parseInt(dateArr[0]);
    var month;
    if(dateArr[1].indexOf("0") == 0){
      month = parseInt(dateArr[1].substring(1));
    }else{
      month = parseInt(dateArr[1]);
    }
    var day = parseInt(dateArr[2]);
    var date = new Date(year,month -1,day);
    return date;
  },

//new Data转时间格式

function formatDateTime(date) {
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h=h < 10 ? ('0' + h) : h;
    let minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    let second=date.getSeconds();
    second=second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};

你可能感兴趣的:(Vue.js)