- 获取日期
Vue.prototype.getDate = function(item) {
let date = new Date();
if(typeof item === 'string') {
date = new Date(item);
} else if (typeof item === 'object') {
date = item
}
let seperator1 = "-";
let year = date.getFullYear();
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
},
Vue.prototype.getYear = function() {
let date = new Date();
let year = date.getFullYear();
return year;
},
Vue.prototype.getMon = function() {
let date = new Date();
let month = date.getMonth() + 1;
return month;
},
Vue.prototype.getDay = function() {
let date = new Date();
let day = date.getDate();
return day;
}
- 时间戳转化成日期格式
export function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
return fmt;
};
function padLeftZero(str) {
return ('00' + str).substr(str.length);
}