// 日期补0
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
// 年月日时分秒
const formatTime = (date, word = false, datestring = '/', timestring = ":") => {
if(date.constructor==String){
var date = new Date(date.replace(/-/g, '/'));
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
if (word == false) {
return [year, month, day].map(formatNumber).join(datestring) + ' ' + [hour, minute, second].map(formatNumber).join(
timestring)
}
return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' + ' ' + [
hour
].map(formatNumber) + ':' + [minute].map(formatNumber) + ':' + [second].map(formatNumber)
}
// 年月日时分
const formatSpotDayHourMinute = (date, word = false, datestring = '/', timestring = ":") => {
if(date.constructor==String){
var date = new Date(date.replace(/-/g, '/'));
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
if (word == false) {
return [year, month, day].map(formatNumber).join(datestring) + ' ' + [hour, minute].map(formatNumber).join(
timestring)
}
return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' + ' ' + [
hour
].map(formatNumber) + ':' + [minute].map(formatNumber)
}
// 年月日
const formatTimeDay = (date, word = false, datestring = '/') => {
if(date.constructor==String){
var date = new Date(date.replace(/-/g, '/'));
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
if (word == false) {
return [year, month, day].map(formatNumber).join(datestring)
}
return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
}
// 年月
const formatWordsYM = (date, word = false, datestring = '/') => {
if(date.constructor==String){
var date = new Date(date.replace(/-/g, '/'));
}
const year = date.getFullYear()
const month = date.getMonth() + 1
if (word == false) {
return [year, month].map(formatNumber).join(datestring)
}
return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月'
}
// 月日
const formatWordsYD = (date, word = false, datestring = '/') => {
if(date.constructor==String){
var date = new Date(date.replace(/-/g, '/'));
}
const month = date.getMonth() + 1
const day = date.getDate()
if (word == false) {
return [month, day].map(formatNumber).join(datestring)
}
return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
}
// 时分
const formatTimeHourMonth = (date, word = false, timestring = ":") => {
if(date.constructor==String){
var date = new Date(date.replace(/-/g, '/'));
}
const hour = date.getHours()
const minute = date.getMinutes()
if (word == false) {
return [hour, minute].map(formatNumber).join(timestring)
}
return [hour].map(formatNumber) + '时' + [minute].map(formatNumber) + '分'
}
// 更新数组年月份日期
function getfullDateStr(dayCount = 0,word=false,datestring="-") {
var dd = new Date();
dd.setDate(dd.getDate() + dayCount); //设置日期
const year = dd.getFullYear()
const month = dd.getMonth() + 1
const day = dd.getDate()
if(!word){
return [year, month, day].map(formatNumber).join(datestring)
}
return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
}
//计算两个日期差
function daysBetween(sDate1, sDate2) {
//Date.parse() 解析一个日期时间字符串,并返回1970/1/1 午夜距离该日期时间的毫秒数
var time1 = (new Date(sDate1)).getTime();
var time2 = (new Date(sDate2)).getTime();
var nDays = parseInt((time2 - time1) / 1000 / 3600 / 24);
return nDays;
};
// 更新数组月份日期
function getDateStr(dayCount = 0) {
var dd = new Date();
dd.setDate(dd.getDate() + dayCount); //设置日期
var m = dd.getMonth() + 1; //获取当前月份的日期
var d = dd.getDate();
return `${m}月${d<10?'0'+d:d}日`
}
// 转化时间几分钟前 小时前 昨天 多少天前 '-'年月日
const formatTimeHours = (timespan,word=false) => {
if(timespan.constructor==String){
var dateTimeStamp = new Date(timespan.replace(/-/g, '/'));
}else{
var dateTimeStamp = timespan
}
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var result = '';
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
if (diffValue < 0) {
console.log("时间不对劲,服务器创建时间与当前时间不同步");
return result = "刚刚";
}
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (parseInt(dayC) > 7) {
if(word==false){
result = "" + formatSpotDayHourMinute(dateTimeStamp,false,'-',':')
}else{
result = "" + formatSpotDayHourMinute(dateTimeStamp,true)
}
} else if (parseInt(dayC) > 1) {
result = "" + parseInt(dayC) + "天前" + ' ' + formatTimeHourMonth(dateTimeStamp,word);
} else if (parseInt(dayC) == 1) {
result = "昨天" + ' ' + formatTimeHourMonth(dateTimeStamp,word);
} else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小时前";
} else if (minC >= 5) {
result = "" + parseInt(minC) + "分钟前";
} else {
result = "刚刚";
}
return result;
};
//更新数组星期
function getWeekDateStr(dayCount = 0) {
//更新数组星期
var weekStr = "周" + "日一二三四五六".charAt((new Date().getDay() + dayCount) % 7);
return weekStr;
}
//手机号是否合法
function isPhone(val) {
const regexp = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
return regexp.test(val)
// if (!(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/.test(phone)) || phone == '') {
// return true;
// }
}
//银行卡是否合法
function isIDcard(value) {
if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)) {
return true
}
}
/**
* 处理富文本里的图片宽度自适应
* 1.去掉img标签里的style、width、height属性
* 2.img标签添加style属性:max-width:100%;height:auto
* 3.修改所有style里的width属性为max-width:100%
* 4.去掉
标签
* @param html
* @returns {void|string|*}
*/
function formatRichText(html) {
let newContent = html.replace(/
]*>/gi, function (match, capture) {
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
return match;
});
newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
return match;
});
newContent = newContent.replace(/
]*\/>/gi, '');
newContent = newContent.replace(/\
{
const regexp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
return regexp.test(val)
}
module.exports = {
formatWordsYM: formatWordsYM, //年月
formatTime: formatTime, // 年月日时分秒
formatTimeDay: formatTimeDay, // 年月日
formatSpotDayHourMinute: formatSpotDayHourMinute, //年月日时分
formatWordsYD: formatWordsYD,// 月日
formatTimeHourMonth: formatTimeHourMonth, // 时分
formatTimeHours:formatTimeHours, // 时间处理刚刚,几天前
daysBetween: daysBetween, //计算两个日期差
isPhone: isPhone, //判断手机号是否合法
isIDcard: isIDcard, //身份证号验证
formatRichText, //富文本解析
formatNumber: formatNumber, //日期补0
toImageBase64: toImageBase64, //本地图片转base64
getDateStr: getDateStr, //获取距今天几天后的月日
getWeekDateStr: getWeekDateStr, //获取距今天几天后的星期几
getfullDateStr: getfullDateStr, ////获取距今天几天后格式化的年月日
RandomNumBoth: RandomNumBoth, //范围内的随机数字
randomName: randomName, //随机字符串
buildColor:buildColor,// 随机颜色值
path:path,
formatPhone,
testPwd
}