Make the Wheel series 2: JS toolset
Project address: github.com/YinXiaoj/My…
More types are being developed
MyJs. Js code
// Get the current URL
function getUrl() {
return window.location.href;
}
// Get url parameters
function getUrlParam(key) {
var url = window.location.search;
var reg = new RegExp("(^ | &)" + key + "= (/ ^ & *) (& | $)");
var result = url.substr(1).match(reg);
return result ? decodeURIComponent(result[2) :null;
}
// Get the height of the scroll bar from the top
function getScrollTop() {
return document.documentElement.scrollTop || document.body.scrollTop;
}
Scroll to the top of the page
function scrollToTop() {
var h = document.documentElement.scrollTop || document.body.scrollTop;
if (h > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, h - h / 8); }}@param {*} date Specifies the date variable. @param {string} dateType Specifies the type to be returned
function getFormatDate(date, dateType) {
var dateObj = new Date(date);
var month = dateObj.getMonth() + 1;
var strDate = dateObj.getDate();
var hours = dateObj.getHours();
var minutes = dateObj.getMinutes();
var seconds = dateObj.getSeconds();
var now_day = dateObj.getDay();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds
}
var weekDay=' ';
switch (now_day) {
case 0: {
weekDay = "Sunday"
}
break;
case 1: {
weekDay = "Monday"
}
break;
case 2: {
weekDay = "Tuesday"
}
break;
case 3: {
weekDay = "Wednesday"
}
break;
case 4: {
weekDay = "Thursday"
}
break;
case 5: {
weekDay = "Friday"
}
break;
case 6: {
weekDay = "Saturday"
}
break;
case 7: {
weekDay = "Sunday"
}
break;
}
var dateText = dateObj.getFullYear() +' '+ month +' '+ strDate;
switch(dateType){
case "yyyy-mm-dd":
dateText = dateObj.getFullYear() + The '-' + month + The '-' + strDate;
break;
case "yyyy.mm.dd":
dateText = dateObj.getFullYear() + '. ' + month + '. ' + strDate;
break;
case "yyyy-mm-dd MM:mm:ss":
dateText = dateObj.getFullYear() + The '-' + month + The '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
break;
case "mm-dd MM:mm":
dateText = month + The '-' + strDate + ' ' + hours + ":" + minutes;
break;
case "Yyyy mm: mm: SS":
dateText = dateObj.getFullYear() + 'years' + month + 'month' + strDate + 'day' + ' ' + hours + ":" + minutes + ":" + seconds;
break;
case "MM:mm:ss":
dateText = hours + ":" + minutes + ":" + seconds;
break;
case "weekday":
dateText = weekDay;
break;
}
return dateText;
}
@param {*} startTime timestamp of the startTime * @param {*} endTime timestamp of the endTime */
function getTimeInterval(startTime, endTime) {
let runTime = parseInt((endTime - startTime) / 1000);
let year = Math.floor(runTime / 86400 / 365);
runTime = runTime % (86400 * 365);
let month = Math.floor(runTime / 86400 / 30);
runTime = runTime % (86400 * 30);
let day = Math.floor(runTime / 86400);
runTime = runTime % 86400;
let hour = Math.floor(runTime / 3600);
runTime = runTime % 3600;
let minute = Math.floor(runTime / 60);
runTime = runTime % 60;
let second = runTime;
let str = ' ';
if (year > 0) {
str = year + 'years';
}
if (year <= 0 && month > 0) {
str = month + 'month';
}
if (year <= 0 && month <= 0 && day > 0) {
str = day + 'day';
}
if (year <= 0 && month <= 0 && day <= 0 && hour > 0) {
str = hour + 'hour';
}
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) {
str = minute + 'minutes';
}
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) {
str += second + '秒';
}
str += 'before';
return str;
}
// Get the timestamp n days after the current time
function toNextTimes(n){
var timestamp = +new Date() + n * 86400000;
return timestamp;
}
/** Verify date size * example: "2019-10-24" and "2019-10-25" * @param {string} d1 Date to be verified 1 * @param {string} d2 Date to be verified 2 * @return {Boolean} Returns a Boolean value */
function compareDate(d1, d2) {
return ((new Date(d1.replace(/-/g."/ /"))) < (new Date(d2.replace(/-/g."/ /"))));
}
* @param {string} date Start date * @param {number} day Number of days */
function convertDate(date, day) {
var tempDate = new Date(date);
tempDate.setDate(tempDate.getDate()+day);
var Y = tempDate.getFullYear();
var M = tempDate.getMonth()+1 < 10 ? '0'+(tempDate.getMonth()+1) : tempDate.getMonth()+1;
var D = tempDate.getDate() < 10 ? '0'+(tempDate.getDate()) : tempDate.getDate();
var result = Y + "-" + M + "-" + D
return result;
}
// Take a string and add an ellipsis
function subText(str, length) {
if (str.length === 0) {
return ' ';
}
if (str.length > length) {
return str.substr(0, length) + "...";
} else {
returnstr; }}// Generates a range of random numbers
function RandomNum(min, max, n) {
var result;
if(n) {
result = (Math.random() * (max - min + 1)).toFixed(n);
}else{
result = Math.floor(Math.random() * (max - min + 1));
}
return result;
}
// remove whitespace
function trim(str, type) {
if(type && type ! = =1&& type ! = =2&& type ! = =3&& type ! = =4) return;
switch (type) {
case 1:
return str.replace(/\s/g."");
case 2:
return str.replace(/(^\s)|(\s*$)/g."");
case 3:
return str.replace(/(^\s)/g."");
case 4:
return str.replace(/(\s$)/g."");
default:
returnstr; }}// Case conversion
function turnCase(str, type) {
switch (type) {
case 1:
return str.toUpperCase();
case 2:
return str.toLowerCase();
case 3:
return str[0].toUpperCase() + str.substr(1).toLowerCase();
default:
returnstr; }}// Random hex color
function randomColor() {
var n = (Math.random() * 0xfffff * 1000000).toString(16);
return The '#' + n.slice(0.6);
}
/ / escaped HTML
function escapeHTML(str) {
return str.replace(
/[&<>'"]/g,
tag =>
({
'&': '& '.'<': '< '.'>': '> '."'": '& # 39; '.'"': '" '
}[tag] || tag)
);
};
// Determine the data type
function type(target) {
let ret = typeof(target);
let template = {
"[object Array]": "array"."[object Object]":"object"."[object Number]":"number - object"."[object Boolean]":"boolean - object"."[object String]":'string-object'
};
if(target === null) {
return 'null';
}else if(ret == "object") {let str = Object.prototype.toString.call(target);
return template[str];
}else{
returnret; }}// Format money with commas for every three digits
function formatMoney(num) {
return num.toString().replace(/\B(? =(\d{3})+(? ! \d))/g.",");
}
// Check whether an element exists in the array
function inArray(item, data) {
for (let i = 0; i < data.length; i++) {
if (item === data[i]) {
returni; }}return - 1;
}
// Check whether the phone is Android or ios
function getOSType() {
let u = navigator.userAgent, app = navigator.appVersion;
let isAndroid = u.indexOf('Android') > - 1 || u.indexOf('Linux') > - 1;
letisIOS = !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/);
if (isIOS) {
return 0;
}
if (isAndroid) {
return 1;
}
return 2;
}
// Determine mobile /PC devices
function detectDeviceType() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? 'Mobile' : 'PC';
}
// Determine the browser kernel
function checkBrowser() {
const u = navigator.userAgent;
const obj = {
trident: u.indexOf("Trident") > - 1./ / IE kernel
presto: u.indexOf("Presto") > - 1./ / opera kernel
webKit: u.indexOf("AppleWebKit") > - 1.// Apple, Google kernel
gecko: u.indexOf("Gecko") > - 1 && u.indexOf("KHTML") = =- 1.// The Firefox kernel
}
return Object.keys(obj)[Object.values(obj).indexOf(true)]};Copy the code