js 常用方法
/*
**手机格式验证
*/
const checkMobile = (mobile) => {
let reg = /^1[3456789]\d{9}$/;
return reg.test(mobile);
}
//获取当前日期
const formatData = (date) => {
const year = date.getFullYear();
const month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
return [year, month, day].join('-')
}
//获取前一天日期
const preDate = (date) => {
const preDate = new Date(date.getTime() - 24 * 60 * 60 * 1000);
const preYear = preDate.getFullYear();
const preMonth = (preDate.getMonth() + 1 < 10 ? '0' + (preDate.getMonth() + 1) : preDate.getMonth() + 1);
const preDay = preDate.getDate() < 10 ? '0' + preDate.getDate() : preDate.getDate();
return [preYear, preMonth, preDay].join('-')
}
//获取当前时间
const formatTime = (date) => {
const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return [hour, minute, second].join('😂
}
//获取当前小时数
const formatHour = (date) => {
const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
return hour
}