1,日期格式化(如格式成2013-12-27 15:30,new Date().format("yyyy-MM-dd hh:mm:ss"),这种格式在safari浏览器上会出问题,可用.format("yyyy/MM/dd hh:mm:ss")代替):
1 Date.prototype.format = function (format) { 2 var o = { 3 "M+": this.getMonth() + 1, 4 "d+": this.getDate(), 5 "h+": this.getHours(), 6 "m+": this.getMinutes(), 7 "s+": this.getSeconds(), 8 "q+": Math.floor((this.getMonth() + 3) / 3), 9 "S": this.getMilliseconds() 10 } 11 if (/(y+)/.test(format)) { 12 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 13 } 14 for (var k in o) { 15 if (new RegExp("(" + k + ")").test(format)) { 16 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); 17 } 18 } 19 return format; 20 }
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,从数组中获取最大值和最小值:
1 var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; 2 var maxInNumbers = Math.max.apply(Math, numbers); 3 var minInNumbers = Math.min.apply(Math, numbers);
6,数组排序:
1 var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; 2 var maxInNumbers = Math.max.apply(Math, numbers); 3 var minInNumbers = Math.min.apply(Math, numbers); 4 //默认按照各个元素的ASCII码逐位比较。 5 var sorterArr=numbers.sort();//结果 -215,-85411,120,122205,228,400,458 6 //自定义排序规则,按数字大小排序。 7 var sortByNumArr=numbers.sort(function (a,b){ 8 if(a>b){ 9 return 1;//return a>b 在safari浏览器上不正常。 10 }else if(a<b){ 11 return -1; 12 }else{ 13 return 0; 14 } 15 });