Vue 常用工具类
1.验证手机号
//验证手机号 function isPoneAvailable(str) { var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/; if (!myreg.test(str)) { return false; } else { return true; } }
2.格式化日期
function formatZNdate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); if (month < 10) { month = "0" + month; } if (day < 10) { day = "0" + day; } var nowDate = `${year}年${month}月${day}日` return nowDate }
3.身份证判断年龄
/** * 判断年龄 * @param {*} UUserCard * @returns */ function getIdCard(UUserCard) { //获取年龄 var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.getDate(); var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1; if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) { age++; } return age }
4.身份证获取性别
// 身份证获得性别 function getSex(idCard) { var sexStr = ''; if (parseInt(idCard.slice(-2, -1)) % 2 == 1) { sexStr = '男'; } else { sexStr = '女'; } return sexStr; }
5.判断身份证格式
function isIdCard(idCard){ let reg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; if (reg.test(idCard)) { return true } else{ return false } }
6.获得生日格式
function getBirthday(idCard){ var 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; }
7. 判断当前时间到时到了某个截止时间
function getBirthday(endTime){ // 将 2022-01-01 包含“-”符号的替换成“/”(兼容iOS时间戳转换成null的bug) var dateTime = endTime.replace(/-/g, "/"); var endtimes=(new Date(this.dateTime)).getTime() var nowtimes=(new Date()).getTime() if (endtimes>nowtimes) {//如果结束时间大于当前时间 没有到期 return true } else { return false } }
8.时间戳转换为日小时分秒
//通过时间戳的方式来 //var day = parseInt(总秒数/60/60/24);//天 // var t = parseInt(总秒数/60/60%24);//时 // var m = parseInt(总秒数/60%60);//分 // var s = parseInt(总秒数%60);//秒 function countDown(inputTime) {
var days = parseInt (mss / (1000 * 60 * 60 * 24));
var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
var seconds = (mss % (1000 * 60)) / 1000;
return days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";
}
9.获取当前时间的周一至周日时间(一周所在的日期)
function getWeekDay(presentDate) { let today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7; console.log(today) return Array.from(new Array(7), function(val, index) { return formatDates(new Date(presentDate.getTime() - (today - index-1) * 24 * 60 * 60 * 1000)); }); function formatDates(date) { return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+" 00:00:00"; } }
本文来自博客园,作者:三线码工,转载请注明原文链接:https://www.cnblogs.com/shangrao/p/15783619.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过