1.JS数字位数不够,前面位数补零
function formatZero(num, len) {
if(String(num).length > len) return num;
return (Array(len).join(0) + num).slice(-len);
}
2.数字保留两位小数
returnFloat (v) {
let value = Math.round(parseFloat(v) * 100) / 100;
const xsd = value.toString().split('.');
if (xsd.length === 1) {
value = value.toString() + '.00';
return value;
}
if (xsd.length > 1) {
if (xsd[1].length < 2) {
value = value.toString() + '0';
}
return value;
}
},
3.出生日期:根据身份证自动填入
getBirthdayFromIdCard (idCard) {
let birthday = '';
if (idCard !== null && idCard !== '') {
if (idCard.length === 15) {
birthday = '19' + idCard.substr(6, 6);
} else if (idCard.length === 18) {
birthday = idCard.substr(6, 8);
}
birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
}
return birthday;
}
4.所属地区:根据身份证前6位判断带入
<script>
export default {
data () {
return {
arrData: {
province:{
110000: '北京市',
120000: '天津市',
130000: '河北省',
410000: '河南省'
},
city: {
130100: '石家庄市',
130200: '唐山市',
130300: '秦皇岛市',
130400: '邯郸市',
130500: '邢台市',
},
county: {
130104: '桥西区',
130105: '新华区',
130106: '郊区',
130107: '井陉矿区',
}
}
};
},
methods: {
getAreaByIdCard (idCard) {
if(!idCard || idCard.length < 6) {return '';}
const provinceNo = idCard.substring(0, 2) + '0000';
const cityNo = idCard.substring(0, 4) + '00';
const countyNo = idCard.substring(0, 6);
console.log(provinceNo,cityNo,countyNo,'映射数组');
const provinceName = this.arrData.province[provinceNo];
const cityName = this.arrData.city[cityNo];
const countyName = this.arrData.county[countyNo];
let result='';
result = (provinceName ? provinceName : '') + '' + (cityName ? cityName : '') + '' + (countyName ? countyName : '');
return result;
},
}
};
</script>
document.getElementById('demo').innerHTML = this.getAreaByIdCard('1301041998042301236');
输出:河北省石家庄市桥西区
5.查找日期位于一年中的第几天
const dayOfYear = (date) =>
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
dayOfYear(new Date());
6.计算两个日期的间隔天数
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366
7.校验数组是否为空
const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;
isNotEmpty([1, 2, 3]);
// Result: true
8.JS数字金额转化为汉字金额
function changeNumMoneyToChinese(money) {
var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
//汉字的数字
var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
var cnInteger = "整"; //整数金额时后面跟的字符
var cnIntLast = "元"; //整型完以后的单位
var maxNum = 999999999999999.9999; //最大处理的数字
var IntegerNum; //金额整数部分
var DecimalNum; //金额小数部分
var ChineseStr = ""; //输出的中文金额字符串
var parts; //分离金额后用的数组,预定义
if (money === "") { return ""; }
money = parseFloat(money);
if (money >= maxNum) { alert('超出最大处理数字'); return ""; }
if (money === 0) { ChineseStr = cnNums[0] + cnIntLast + cnInteger; return ChineseStr; }
money = money.toString(); //转换为字符串
if (money.indexOf(".") === -1) { IntegerNum = money; DecimalNum = ''; }
else { parts = money.split("."); IntegerNum = parts[0]; DecimalNum = parts[1].substr(0, 4); }
if (parseInt(IntegerNum, 10) >= 0) { //获取整型部分转换
if (IntegerNum === "0") {
ChineseStr = "零";
}
else {
var zeroCount = 0; var IntLen = IntegerNum.length; for (var i = 0; i < IntLen; i++) {
var n = IntegerNum.substr(i, 1); var p = IntLen - i - 1; var q = p / 4; var m = p % 4; if (n === "0") { zeroCount++; } else {
if (zeroCount > 0) { ChineseStr += cnNums[0]; } zeroCount = 0; //归零
ChineseStr += cnNums[parseInt(n, 10)] + cnIntRadice[m];
} if (m === 0 && zeroCount < 4) { ChineseStr += cnIntUnits[q]; }
}
} ChineseStr += cnIntLast; //整型部分处理完毕 }
if (DecimalNum !== '') { //小数部分
var decLen = DecimalNum.length;
for (var i = 0; i < decLen; i++) { var n = DecimalNum.substr(i, 1); if (n !== '0') { ChineseStr += cnNums[Number(n)] + cnDecUnits[i]; } }
} if (ChineseStr === '') { ChineseStr += cnNums[0] + cnIntLast + cnInteger; } else if (DecimalNum === '') { ChineseStr += cnInteger; } return ChineseStr;
}
};