Encapsulation of commonly used functions in js

1: //Intercept the parameters brought by the url

function getvl(name) {
var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
return reg.test(location.href) ? decodeURI(RegExp.$2.replace(/\+/g, " ")) : '';
};

2://判断对象的长度
Object.size = function (obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
3:
// 转换字符串成时间

function changeTime(t, nohour) {
    if (String(t).indexOf("-") >= 0 || t == null) {
return t = '';
} else {
var date = new Date(t);
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return nohour ? Y + M + D : Y + M + D + h + m + s;
}
}
4://Pure numbers and no spaces and no decimal points 
// isZero == zero can be zero
// isZero == unNone cannot be spaces
function keepNum(obj, num, isZero) {
var $this = $(obj) ;
var v = $this.val();
if (isNaN(v) || (v.indexOf('.') != -1)) {
var values ​​= v.slice(0, -1);
if ( isNaN(values)) {
values ​​= ''
}
values ​​= values.replace(/\s/g, "");
$this.val(values);
} else { //remove spaces
$this.val(v.replace (/\s/g, ""));
}
num && (function () {
var newValue = $this.val();
if (!isNaN(newValue) && newValue != '') {
if (isZero == 'zero') {
if (newValue < 0) {
newValue = 0;
} else if (newValue > num) {
newValue = num;
}
} else {
if (newValue < 1) {
newValue = 1;
} else if (newValue > num) {
newValue = num;
}
}

} else {
if (isZero == 'unNone') {
if (newValue < 1) {
newValue = 1;
} else if (newValue > num) {
newValue = num;
}
}
}
$this.val(newValue);
})()
}
5://Pure numbers and no spaces and no decimal point can be negative default no decimal
function keepSR(obj, num,maxlength) {
var imposeNum = maxlength ? ( maxlength+1) : 1;
var $this = $(obj);
var v = $this.val();
if (isNaN(v)) {
if(v!='-'){
var values ​​= v.slice (0, -1);
if (isNaN(values)) {
values ​​= ''
}
values ​​= values.replace(/\s/g, "");
$this.val(values);
}
} else if (v .indexOf('.') != -1) {
if (v.split('.')[1].length == imposeNum) {
var values ​​= v.slice(0,-1);
$this.val(values);
} else if (v.split('.')[1].length > imposeNum) {
$this.val('');
}
}else { //Remove spaces
$this.val (v.replace(/\s/g, ""));
}
num && (function () {
var newValue = $this.val();
if (!isNaN(newValue) && newValue != '') {
if (newValue < -num) {
newValue = -num;
} else if (newValue > num) {
newValue = num;
}
}
$this.val(newValue);
})()
}
//Number and the most limited number of decimal places, (default three digits) and can not enter spaces and can be 0
function keepSH(obj, num ,maxlength) {
var $this = $(obj);
var v = $this.val();
var imposeNum = maxlength ? (maxlength+1) : 4;//限制小数点数量,默认为小数点后3位
if (isNaN(v)) {
var values = v.slice(0, -1);
if (isNaN(values)) {
values = ''
}
$this.val(values);
} else if (v.indexOf('.') != -1) {
if (v.split('.')[1].length == imposeNum) {
var values = v.slice(0, -1);
$this.val(values);
} else if (v.split('.')[1].length > imposeNum) {
$this.val('');
}
} else {
$this.val(v.replace(/\s/g, ""));
}

var newValue = $this.val();
if (!isNaN(newValue) && newValue != '') {
if (newValue > num) {
newValue = num;
}
}
$this.val(newValue);
}

function keepSH2(obj, num ,maxlength) {
var $this = $(obj);
var v = $this.val();
var imposeNum = maxlength ? (maxlength+1) : 4;//限制小数点数量,默认为小数点后3位
if (isNaN(v)) {
var values = v.slice(0, -1);
if (isNaN(values)) {
values = ''
}
$this.val(values);
} else if (v.indexOf('.') != -1) {
if (v.split('.')[1].length == imposeNum) {
var values = v.slice(0, -1);
$this.val(values);
} else if (v.split('.')[1].length > imposeNum) {
$this.val('');
}
} else {
$this.val(v.replace(/\s|-/g, ""));
}

var newValue = $this.val();
if (!isNaN(newValue) && newValue != '') {
if (newValue > num) {
newValue = num;
}
}
$this.val(newValue);
}
// Check if it is a mobile phone number 
function checkMobile(text) {
var myreg = /^(1+\d{10})$/;
return !myreg.test(text) ? false : true;
}
//Only enter Chinese, Letters, numbers
function unCharacter(v) {
return v.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')
}
//Prohibit special characters
function unSpecial( v) {
return v.replace(/[^u4e00-u9fa5w]/g,'')
}
// used to check if the object is empty
function isEmptyObject(e) {
var t;
for (t in e)
return !1;
return !0
}
//格式化日期:yyyy-MM-dd     
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();

if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday);
}

//获得某月的天数
function getMonthDays(myMonth) {
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;//Get the start month of the quarter
}


function getQuarterStartMonth() {
var quarterStartMonth = 0;
if (nowMonth < 3) {
quarterStartMonth = 0;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 3;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 6;
}
if (nowMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
}

//获得本周的开始日期
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}

//获得本周的结束日期
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}

//Get the start date of last week
function getpreWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
return formatDate(weekStartDate);
}

//Get the end date of last week
function getpreWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (-1 - nowDayOfWeek));
return formatDate(weekEndDate );
}

//Get the start date of this month
function getMonthStartDate() {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}

//Get the end date of this month
function getMonthEndDate() {
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}

//获得上月开始时间
function getLastMonthStartDate() {
var lastMonth2 = lastMonth;
var nowYear2 = nowYear;
if (nowMonth < lastMonth) {
var lastMonth2 = 11;
var nowYear2 = nowYear - 1;
}
var lastMonthStartDate = new Date(nowYear2, lastMonth2, 1);
return formatDate(lastMonthStartDate);
}

//获得上月结束时间
function getLastMonthEndDate() {
var lastMonth2 = lastMonth;
var nowYear2 = nowYear;
if (nowMonth < lastMonth) {
var lastMonth2 = 11;
var nowYear2 = nowYear - 1;
}
var lastMonthEndDate = new Date(nowYear2, lastMonth2, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}

//Get the start date of the quarter
function getQuarterStartDate() {

var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartDate);
}

//Get the end date of the quarter
function getQuarterEndDate() {
var quarterEndMonth = getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear , quarterEndMonth, getMonthDays(quarterEndMonth));
return formatDate(quarterStartDate);
}

//Calculate the day before yesterday
function _getDay(day) {
var today = new Date();

var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;

today.setTime(targetday_milliseconds); //注意,这行是关键代码

var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear + "-" + tMonth + "-" + tDate;
}

function doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024489&siteId=291194637