前端常用方法

本文介绍了JavaScript中用于处理时间戳的函数,如格式化日期、计算时间差以及与HTMLinput组件交互的方法,包括数字格式化、数组操作和输入验证规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 获取当前时间
const currentTime = () => {
    var date = new Date();
    var year = date.getFullYear(); //月份从0~11,所以+1
    let month = date.getMonth();
    console.log("month", month);
    var currentDateArr = [
        date.getMonth() + 1,
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
    ];
    //如果格式是MM则需要此步骤,如果是M格式则此循环注释掉
    for (var i = 0; i < currentDateArr.length; i++) {
        if (currentDateArr[i] >= 1 && currentDateArr[i] <= 9) {
            currentDateArr[i] = "0" + currentDateArr[i];
        }
    }
    var currentDate =year +"-" +currentDateArr[0] + "-" + currentDateArr[1]+" " +currentDateArr[2] +":" +currentDateArr[3] +":" +currentDateArr[4];
    console.log("currentDate", currentDate);
};

// 时间戳转化为年月日
const timeFormate = (timeStamp) => {
    let year = new Date(timeStamp).getFullYear();
    let month = new Date(timeStamp).getMonth() + 1 < 10 ? "0" + (new Date(timeStamp).getMonth() + 1) : new Date(timeStamp).getMonth() + 1;
    let date = new Date(timeStamp).getDate() < 10 ? "0" + new Date(timeStamp).getDate() : new Date(timeStamp).getDate();
    let hh = new Date(timeStamp).getHours() < 10 ? "0" + new Date(timeStamp).getHours() : new Date(timeStamp).getHours();
    let mm = new Date(timeStamp).getMinutes() < 10 ? "0" + new Date(timeStamp).getMinutes() : new Date(timeStamp).getMinutes();
    let ss = new Date(timeStamp).getSeconds() < 10 ? "0" + new Date(timeStamp).getSeconds() : new Date(timeStamp).getSeconds();
    return year + "-" + month + "-" + date + "" + " " + hh + ":" + mm + ":" + ss
}


//时间戳转为几天-几个小时-几分钟-几秒
    getTimeLongDisc(timeMS) {
        if(timeMS<=0){
            return "0秒";
        }

        const days = timeMS / 1000 / 60 / 60 / 24;
        const daysRound = Math.floor(days);
        const hours = timeMS/ 1000 / 60 / 60 - (24 * daysRound);
        const hoursRound = Math.floor(hours);
        const minutes = timeMS / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
        const minutesRound = Math.floor(minutes);
        const seconds = timeMS/ 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
        const secondsRound = Math.floor(seconds);

        let resultDisc="";
        if(daysRound > 0){
            resultDisc+=daysRound+"天";
            if(hoursRound>0){
                resultDisc+=hoursRound+"时";
            }
        }else {
            if(hoursRound>0){
                resultDisc+=hoursRound+"时";
                if(minutesRound>0){
                    resultDisc+=minutesRound+"分";
                }
            }else {
                if(minutesRound>0){
                    resultDisc+=minutesRound+"分";
                }
                resultDisc+=secondsRound+"秒";
            }
        }

        return resultDisc;
    }

// 保留几位小数
export function formatDecimalFraction(num, decimalPlaces) {
    // 如果是整数,保持原状
    if (Number.isInteger(num)) {
        return num.toString();
    } else {
        // 如果是小数,根据传入的小数位数保留相应位数
        if (typeof decimalPlaces === 'number' && decimalPlaces >= 0) {
            return num.toFixed(decimalPlaces);
        } else {
            // 默认保留一位小数
            return num.toFixed(1);
        }
    }
}

// 数字添加千分号
export function formatNumber(number) {
    if (typeof number === 'number') {
        // 如果是数字类型,先转换为字符串
        number = number.toString();
    } else if (typeof number !== 'string') {
        // 如果不是数字或字符串类型,返回原始值
        return number;
    }

    // 使用正则表达式添加千分位逗号
    return number.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

//当input框类型为number 值为0时,点击input框内容为空
<el-input v-model="reportData.reportCount" type="number"                              @focus="reportCheckFrequencyFunction(reportData.reportCount)" />

const reportCheckFrequencyFunction = () => {
    if (reportData.reportCount == 0) {
        reportData.reportCount = ''
    }
}


//判断数组对象里的某一个值是否全部相同
arr.flat().every(item => item.type === 4)

//数组转字符串
arr.join(',')

//判断input框里面是否全为空格
if (passward.value.replace(/(^\s*)|(\s*$)/g, "") == "") {
    console.log("未输入或者输入为空格");
  } else {
    console.log("输入:", userNewSet.password);
  }

//input框不能输入小数点
<el-input type="number" @input="handleInput" :step="1" :min="1" v-model="modelReportData.reportCount" />

const handleInput = (value) => {
  console.log('v',value)
  if(value.includes('.')){
    modelReportData.reportCount = parseInt(value)
  }
};
//onkeyup="value=value.replace(/[^\d]/g,'')"  只允许输入数字(整数:小数点不能输入)
//onkeyup="value=value.replace(/^\D (\d (?:.\d{0,2})?). 1')"  允许输入小数(两位小数)
//onkeyup="value=value.replace(/^\D (\d (?:.\d{0,1})?). 1')" 允许输入小数(1位小数)
//onkeyup="value=value.replace(/[ \d]/g,'').replace(/ 0{1,}/g,'')" 开头不能为0,且不能输入小数

//在表格中使用input 为空时该值为0
// @blur="blurNumber(row)"
const blurNumber=(row)=>{
  if(!row.unclearCount){
    row.unclearCount=0
  }else{
    row.unclearCount=row.unclearCount*1
  }
}
截取最后一个特殊字符后面的全部内容
字符串.substring(字符串.lastIndexOf("/")+1);

判断数组中的某个字段是否全部相同
multipleSelectionList.value.flat().every((item) => item.purchaseType === 2

后面会继续补充

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值