wheel/utils/util.js
2019-11-29 13:52:16 +08:00

99 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatDate(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
return [year, month, day].map(formatNumber).join('-');
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
/**
* 判断两个日期相差天数
*/
function getDays(strDateStart, strDateEnd) {
var strSeparator = "-"; //日期分隔符
var oDate1;
var oDate2;
var iDays;
oDate1 = strDateStart.split(strSeparator);
oDate2 = strDateEnd.split(strSeparator);
var strDateS = new Date(oDate1[0], oDate1[1] - 1, oDate1[2]);
var strDateE = new Date(oDate2[0], oDate2[1] - 1, oDate2[2]);
iDays = parseInt(Math.abs(strDateS - strDateE) / 1000 / 60 / 60 / 24)//把相差的毫秒数转换为天数
return iDays;
}
function dateToString(now) {
var year = now.getFullYear();
var month = (now.getMonth() + 1).toString();
var day = (now.getDate()).toString();
if (month.length == 1) {
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
var dateTime = year + "-" + month + "-" + day ;
return dateTime;
}
//一定概率执行处理N是概率例如七分之一n就是7
const randomJudgeDo = n => {
var randomValue = Math.floor(Math.random() * n) + 1;
console.log("====randomJudgeDo===" + n + "|" + randomValue);
if (randomValue == n) {
return true;
} else {
return false;
}
}
function getRandomCode() {
var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var nums = "";
for (var i = 0; i < 32; i++) {
var id = parseInt(Math.random() * 61);
nums += chars[id];
}
return nums;
}
const isNull = str => {
if (str == null || str == undefined || str == '') {
return true;
} else {
return false;
}
}
module.exports = {
formatTime: formatTime,
randomJudgeDo: randomJudgeDo,
getRandomCode: getRandomCode,
getRandomNum: getRandomCode,
isNull: isNull,
getDays: getDays,
dateToString: dateToString,
formatDate: formatDate,
}