首先在moment中引入moment时间插件
在main.ts里面
import moment from 'moment';
Vue.prototype.moment = moment;
然后在写过滤文件中引入moment
import moment from 'moment';
//多久之前
export const timeTransform = dateTimeStamp => {
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const month = day * 30;
const now = new Date().getTime();
const diffValue = now - dateTimeStamp;
if (diffValue < 0) {
return;
}
const monthC = diffValue / month;
const weekC = diffValue / (7 * day);
const dayC = diffValue / day;
const hourC = diffValue / hour;
const minC = diffValue / minute;
if (monthC >= 1) {
return '' + Math.floor(monthC) + '月前';
} else if (weekC >= 1) {
return '' + Math.floor(weekC) + '周前';
} else if (dayC >= 1) {
return '' + Math.floor(dayC) + '天前';
} else if (hourC >= 1) {
return '' + Math.floor(hourC) + '小时前';
} else if (minC >= 1) {
return '' + Math.floor(minC) + '分钟前';
} else {
return '刚刚';
}
};
//转换时间格式
export const dateStrToFormat = (dataStr, format = 'YYYY-MM-DD HH:mm:ss') => {
if (!dataStr) {
return '';
}
return moment(dataStr).format(format);
};
//剩余时长
export const filterRemaining = (data) => {
const timer = new Date().getTime();
if (!data) {
return '';
}
const dayNum: number = Math.floor ((data.disableDate - timer) / 86400000 ) ;
let value ;
if ( dayNum > 364) {
value = '1年以上';
} else {
value = dayNum + '天' ;
}
// tslint:disable-next-line: radix
return '剩余' + value + '到期';
};