| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- export function debounce(func, wait) {
- let timeout;
- return function () {
- let context = this;
- let args = arguments;
- if (timeout) clearTimeout(timeout);
- let callNow = !timeout;
- timeout = setTimeout(() => {
- timeout = null;
- }, wait)
- if (callNow) func.apply(context, args)
- }
- }
- // 获取出生日期
- export function getBirth(idCard) {
- if (!idCard) return
- var birthday = "";
- if (idCard != null && idCard != "") {
- if (idCard.length == 15) {
- birthday = "19" + idCard.slice(6, 12);
- } else if (idCard.length == 18) {
- birthday = idCard.slice(6, 14);
- }
- birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
- //通过正则表达式来指定输出格式为:1990-01-01
- }
- return birthday;
- }
- // 通过身份证号获取性别
- export function getSex(idCard) {
- if (!idCard) return
- var sexStr = '';
- if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
- sexStr = '男';
- } else {
- sexStr = '女';
- }
- return sexStr;
- }
- // 转换 类似 2 => 二
- export function numberToUpperCase(textIndex) {
- let newString = '';
- let newTextIndex = (textIndex + 1) + '';
- function sum(value, index) {
- var newValue = '';
- if ((textIndex === 9)) {
- return !index ? '十' : '';
- }
- let isSeat = (~~textIndex > 9 && ~~textIndex < 19);
- switch (~~value) {
- case 1:
- newValue = !index ? (isSeat ? '' : '一') : '十一';
- break;
- case 2:
- newValue = !index ? (isSeat ? '' : '二') : '十二';
- break;
- case 3:
- newValue = !index ? (isSeat ? '' : '三') : '十三';
- break;
- case 4:
- newValue = !index ? (isSeat ? '' : '四') : '十四';
- break;
- case 5:
- newValue = !index ? (isSeat ? '' : '五') : '十五';
- break;
- case 6:
- newValue = !index ? (isSeat ? '' : '六') : '十六';
- break;
- case 7:
- newValue = !index ? (isSeat ? '' : '七') : '十七';
- break;
- case 8:
- newValue = !index ? (isSeat ? '' : '八') : '十八';
- break;
- case 9:
- newValue = !index ? (isSeat ? '' : '九') : '九十';
- break;
- case 0:
- newValue = '十';
- break;
- default:
- break;
- }
- return newValue;
- }
- for (let i = 0; i < newTextIndex.length; i++) {
- newString += sum(newTextIndex.substring(i, i + 1), i);
- }
- return newString;
- }
- export function formatMonth(date) {
- if (!date) return '';
- if (typeof date === 'number') date = new Date(date);
- const year = date.getFullYear(); // 获取年份
- const month = date.getMonth() + 1; // 获取月份,月份是从0开始的,所以需要加1
- const formattedMonth = month < 10 ? '0' + month : month; // 如果月份小于10,前面补0
- return `${year}-${formattedMonth}`; // 返回格式化后的字符串
- }
- export function subtractMonths(value, date = new Date()) {
- // 获取原始日期的年、月、日
- const year = date.getFullYear();
- const month = date.getMonth();
- const day = date.getDate();
- let _month = month - Math.abs(value);
- const _year = year + Math.floor((_month - (month + 1)) / 12);
- _month = _month - Math.floor((_month - (month + 1)) / 12) * 12;
- // 处理月份天数不足的情况,比如1月31日加1个月不能是2月31日
- let _date = new Date(_year, _month, day);
- if (_date.getMonth() !== _month) {
- const _day = Math.min(day, new Date(_year, _month + 1, 0).getDate());
- _date = new Date(_year, _month, _day);
- }
- return _date;
- }
|