Get the current date or format the current date method encapsulation yyyy-MM-dd
Convert the date format of the current time, yyyy/MM/dd, yyyy.MM.dd, and yyyy-MM-dd as separators to yyyy-MM-dd
1. Determine whether there is a value passed in, and if there is no value passed in, get the current time
2. Whether the value is passed in to judge whether it is a date format (here is not completely judged whether the day and month are correct, only a preliminary judgment is made)
3. Convert date format
function dateFormat(oldDate){
const re = /^(\d{4})(\/|\.|-)(\d{1,2})(\/|\.|-)(\d{1,2})$/
if(!oldDate){
oldDate = new Date().toLocaleDateString()
}else if(!re.test(oldDate)){
console.log('日期格式有误')
return
}
const date = new Date(oldDate)
let year = date.getFullYear().toString()
let month = (date.getMonth() + 1).toString()
let day = date.getDate().toString()
month = month.padStart(2, '0')
day = day.padStart(2, '0')
const dataFormat = `${year}-${month}-${day}`
return dataFormat
}
Input Content Restrictions
export function limitInput(input){
if(!input){
return ''
}
const reg = new RegExp("[^0-9a-z-A-Z]") // 只能输入数字和英文
let reflowSpecial = ''
for(let i = 0; i < input.length; i++){
reflowSpecial = reflowSpecial + input.substr(i, 1).replace(reg, '')
}
return reflowSpecial
}