IMD中一些JS方法总结

时间:2022-03-23 15:39:44

1,日期格式化(如格式成2013-12-27 15:30,new Date().format("yyyy-MM-dd hh:mm:ss"),这种格式在safari浏览器上会出问题,可用.format("yyyy/MM/dd hh:mm:ss")代替):

 Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}

2,日期相加减:()

Date.prototype.DateAdd = function (strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));
case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));
case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));
case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));
case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
}
}

3,将指定日期转换成UTC时间:

//return utc date
function getUTCtime(date) { var UTCyear = date.getUTCFullYear();
var UTCmonth = date.getUTCMonth() + 1;
var UTCday = date.getUTCDate();
var UTChours = date.getUTCHours();
var UTCmin = date.getUTCMinutes();
var UTCseconds = date.getUTCSeconds();
var UTCtimeStr = UTCyear + "/" + UTCmonth + "/" + UTCday + " " + UTChours + ":" + UTCmin + ":" + UTCseconds;
return new Date(UTCtimeStr);
}

4,获取URL中的所有参数:

function getUrlParam(string) {
var obj = {}; if (string.indexOf("?") != -1) {
var string = string.substr(string.indexOf("?") + 1);
var strs = string.split("&");
for (var i = 0; i < strs.length; i++) {
var tempArr = strs[i].split("=");
obj[tempArr[0]] = unescape(tempArr[1]);
}
}
return obj;
}

5,从数组中获取最大值和最小值:

 var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);

6,数组排序:

  

 var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);
//默认按照各个元素的ASCII码逐位比较。
var sorterArr=numbers.sort();//结果 -215,-85411,120,122205,228,400,458
//自定义排序规则,按数字大小排序。
var sortByNumArr=numbers.sort(function (a,b){
if(a>b){
return 1;//return a>b 在safari浏览器上不正常。
}else if(a<b){
return -1;
}else{
return 0;
}
});