作者:uka9032934 | 来源:互联网 | 2023-08-30 06:02
Vuevue-routerquery:this.$route.query.nameparams:this.$route.params.nameJs时间时间格式转换时间格式·两位数f
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;
}
}