InspectionCleaning/utils/common.js

88 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//获取当前时间格式YYYY-MM-DD HH:MM:SS
const GetNowTime = time => {
var date = time,
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
// var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
var timer = year + '-' + month + '-' + day;
return timer;
}
// 格式化电话号码
const GetPhone = phone => {
let tel = phone.slice(0, 3) + '****' + phone.slice(7, 11);
return tel;
}
//返回日期和周几数组
function weekDate() {
const myDate = new Date();
const padZero = num => String(num).padStart(2, '0'); // 补零工具函数
const dayList = [];
// 生成当天数据(补零)
dayList.push({
day: padZero(myDate.getDate()),
month: padZero(myDate.getMonth() + 1),
week: toWeekDay(myDate.getDay()),
year: myDate.getFullYear()
});
// 生成后续10天数据
for (let i = 0; i < 10; i++) {
myDate.setDate(myDate.getDate() + 1);
dayList.push({
day: padZero(myDate.getDate()),
month: padZero(myDate.getMonth() + 1),
week: toWeekDay(myDate.getDay()),
year: myDate.getFullYear()
});
}
// 返回标准化日期格式
return {
dayList,
StartDate: `${dayList[0].year}-${dayList[0].month}-${dayList[0].day}`,
EndDate: `${dayList[dayList.length - 1].year}-${dayList[dayList.length - 1].month}-${dayList[dayList.length - 1].day}`
};
}
function toWeekDay(weekDay) { // 传入数据 讲一周的某一天返回成中文状态下的字符
switch (weekDay) {
case 1:
return '一';
break;
case 2:
return '二';
break;
case 3:
return '三';
break;
case 4:
return '四';
break;
case 5:
return '五';
break;
case 6:
return '六';
break;
case 0:
return '日';
break;
default:
break;
}
return '传入未知参数';
}
module.exports = {
GetNowTime,
GetPhone,
weekDate
}