Vue
vue-router
query: this.$route.query.name
params: this.$route.params.name
Js
时候
时候花样转换
// 时候花样·两位数
function ToDoubleDigit(num) {
return num < 10 ? "0" + num : num;
}
// 猎取time对应的时候数据
function TimeToData(date) {
const Y = date.getFullYear();
const M = ToDoubleDigit(date.getMonth() + 1);
const D = ToDoubleDigit(date.getDate());
const h = ToDoubleDigit(date.getHours());
const m = ToDoubleDigit(date.getMinutes());
const s = ToDoubleDigit(date.getSeconds());
return {
Y,
M,
D,
h,
m,
s
};
}
// 返回所需时候花样
function ToTimeFormat(time, type) {
let date; // js日期花样 getMonth()
if (
String(time).indexOf("-") === -1 &&
String(time).indexOf("/") === -1 &&
String(time).length === 10
) {
// 时候戳转化
date = new Date(Number(time) * 1000);
} else {
// 文本时候花样转化
const stringTime = String(time).replace(/-/g, "/"); // ios花样支撑题目
date = new Date(stringTime);
}
const { Y, M, D, h, m, s } = TimeToData(date); // 猎取time对应的时候数据
if (type === 1) {
return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
}
if (type === 2) {
const between = Date.now() / 1000 - new Date(time).getTime();
const nowDate = TimeToData(new Date());
const nowY = nowDate.Y;
const nowM = nowDate.M;
if (nowY === Y) {
if (nowM === M) {
if (between < 60) {
return "方才";
} else if (between < 3600) {
return pluralize(~~(between / 60), " 分钟前");
} else if (between < 86400) {
return pluralize(~~(between / 3600), " 小时前");
} else {
return pluralize(~~(between / 86400), " 天前");
}
} else {
return M + "/" + D + " " + h + ":" + m;
}
}
return Y + "/" + M + "/" + D + " " + h + ":" + m;
}
}