function GetParameterValueByName(parametername) { var reg = new RegExp("(^|&)" + parametername + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }
使用方法:GetParameterValueByName("id")
js解析后台时间格式的json
在后台返回DateTime格式的json是总是出现的是/Date(1421245202219)/ 所以用js写了个方法进行转换
function dateJsonToDate(val, hour, minute, second, ms) { var dataStr = ""; if (val != null && val != "") { var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); dataStr += date.getFullYear() + "-" + month + "-" + currentDate; if (hour) { var currenthour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); dataStr += " " + currenthour; if (minute) { var currentminute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); dataStr += ":" + currentminute; if (second) { var currentsecond = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); dataStr += ":" + currentsecond; if (ms) { var currentms = date.getMilliseconds() < 10 ? "0" + date.getMilliseconds() : date.getMilliseconds(); dataStr += "." + currentms; } } } } } return dataStr; }
使用方法:dateJsonToDate(data.Now, true, true, true, true)
// Js获取Url参数 function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest; } // 保留指定位小数 function fomatFloat(src, pos) { return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos); } //数组删除指定元素 Array.prototype.delRepet = function () { // this = this || []; var a = {}; for (var i = 0; i < this.length; i++) { var v = this[i]; if (typeof (a[v]) == 'undefined') { a[v] = 1; } }; this.length = 0; for (var i in a) { this[this.length] = i; } return this; } /日期格式转换(yyyy-mm-dd hh:mm) function ChangeDateFormatHM(date) { var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0])); var month = da.getMonth() + 1 < 10 ? "0" + (da.getMonth() + 1) : da.getMonth() + 1; var currentDate = da.getDate() < 10 ? "0" + da.getDate() : da.getDate(); var hour = da.getHours() < 10 ? "0" + da.getHours() : da.getHours() var minute = da.getMinutes() < 10 ? "0" + da.getMinutes() : da.getMinutes(); return da.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minute; } //日期格式转换(hh:mm:ss) function ChangeDateFormatShortHMS(jsondate) { var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", "").split("+")[0])); var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); return hour + ":" + minute + ":" + second; } //日期格式转换(yyyy-mm-dd hh:mm:ss) function ChangeDateFormatHMS(jsondate) { if (jsondate == undefined) { return "1990-01-01 A"; } jsondate = jsondate.replace("/Date(", "").replace(")/", ""); if (jsondate.indexOf("+") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("+")); } else if (jsondate.indexOf("-") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("-")); } var date = new Date(parseInt(jsondate, 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); return date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minute + ":" + second; } //日期格式转换(yyyy-mm-dd) function ChangeDateFormat(jsondate) { if (jsondate == undefined) { return "1990-01-01"; } jsondate = jsondate.replace("/Date(", "").replace(")/", ""); if (jsondate.indexOf("+") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("+")); } else if (jsondate.indexOf("-") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("-")); } var date = new Date(parseInt(jsondate, 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + currentDate; } //获取字符串指定长度的字符 function GetString(Str, size) { if (Str.toString().length > size) { return Str.substring(0, size) + "..."; } else { return Str; } }