123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- Date.prototype.addDay = function (num) {
- var tempDate = new Date(this);
- tempDate.setDate(this.getDate() + num);
- return tempDate;
- }
- Date.prototype.addMonth = function (num) {
- var tempDate = this.getDate();
- this.setMonth(this.getMonth() + num);
- if (tempDate != this.getDate()) this.setDate(0);
- return this;
- }
- Date.prototype.addYear = function (num) {
- var tempDate = this.getDate();
- this.setYear(this.getYear() + num);
- if (tempDate != this.getDate()) this.setDate(0);
- return this;
- }
- Date.prototype.substractDay = function (date) {
- return Math.ceil((this - date) / 24 / 60 / 60 / 1000);
- }
- /**
- * 对Date的扩展,将 Date 转化为指定格式的String
- * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
- * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
- * eg:
- * (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
- * (new Date()).format("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
- * (new Date()).format("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
- * (new Date()).format("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
- * (new Date()).format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
- */
- Date.prototype.format = function (fmt) {
- var o = {
- "M+": this.getMonth() + 1, //月份
- "d+": this.getDate(), //日
- "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
- "H+": this.getHours(), //小时
- "m+": this.getMinutes(), //分
- "s+": this.getSeconds(), //秒
- "q+": Math.floor((this.getMonth() + 3) / 3), //季度
- "S": this.getMilliseconds() //毫秒
- };
- var week = {
- "0": "/u65e5",
- "1": "/u4e00",
- "2": "/u4e8c",
- "3": "/u4e09",
- "4": "/u56db",
- "5": "/u4e94",
- "6": "/u516d"
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
- }
- if (/(E+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
- }
- }
- return fmt;
- }
- String.prototype.toDate = function () {
- var t = this;
- if (t.split("T").length > 1 || t.split("Z").length > 1) {
- var t1 = this.replace(/T/g, ' ').replace(/Z/g, ' ');
- var time = new Date(Date.parse(t1.replace(/-/g, "/"))).getTime();
- t = "/Date(" + time + ")/";
- }
- var d = eval(t.replace(/\/(Date\(-?\d+\))\//ig, "new $1"));
- return d;
- }
- String.prototype.toDateString = function (p) {
- return this.toDate().format(p);
- }
- String.prototype.toCHSSymbols = function () {
- /// <summary>
- /// 将字符串内所有英文符号替换为中文符号-_- Add by 劳炳辉 2013.09.06
- /// </summary>
- var s = this;
- s = s.replace(/\,/g, ",");
- // s = s.replace(/\./g, "。");
- s = s.replace(/\!/g, "!");
- s = s.replace(/\?/g, "?");
- // s = s.replace(/\:/g, ":");
- // s = s.replace(/\;/g, ";");
- // s = s.replace(/\</g, "《");
- // s = s.replace(/\>/g, "》");
- // s = s.replace(/\(/g, "(");
- // s = s.replace(/\)/g, ")");
- // s = s.replace(/\[/g, "【");
- // s = s.replace(/\]/g, "】");
- // s = s.replace(/\{/g, "{");
- // s = s.replace(/\}/g, "}");
- // s = s.replace(/\`/g, "·");
- // s = s.replace(/\$/g, "¥");
- // s = s.replace(/\^/g, "……");
- // s = s.replace(/\_/g, "——");
- s = s.replace(/\你/g, "您");
- return s;
- }
- String.prototype.replaceDoubleSlashesToSingle = function () {
- var str = this;
- while (str.indexOf("//") != -1) {
- str = str.replace(/\/\//g, "/");
- }
- while (str.indexOf("\\\\") != -1) {
- str = str.replace(/\\\\/g, "\\");
- }
- return str.toString();
- }
- Number.prototype.add = function (b) {
- var c, d, e;
- try {
- c = this.toString().split(".")[1].length;
- } catch (f) {
- c = 0;
- }
- try {
- d = b.toString().split(".")[1].length;
- } catch (f) {
- d = 0;
- }
- return e = Math.pow(10, Math.max(c, d)), (this.mul(e) + b.mul(e)) / e;
- };
- Number.prototype.sub = function (b) {
- var c, d, e;
- try {
- c = this.toString().split(".")[1].length;
- } catch (f) {
- c = 0;
- }
- try {
- d = b.toString().split(".")[1].length;
- } catch (f) {
- d = 0;
- }
- return e = Math.pow(10, Math.max(c, d)), (this.mul(e) - b.mul(e)) / e;
- };
- Number.prototype.mul = function (b) {
- var c = 0,
- d = this.toString(),
- e = b.toString();
- try {
- c += d.split(".")[1].length;
- } catch (f) { }
- try {
- c += e.split(".")[1].length;
- } catch (f) { }
- return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
- };
- Number.prototype.div = function (b) {
- var c, d, e = 0,
- f = 0;
- try {
- e = this.toString().split(".")[1].length;
- } catch (g) { }
- try {
- f = b.toString().split(".")[1].length;
- } catch (g) { }
- return c = Number(this.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), (c / d).mul(Math.pow(10, f - e));
- }
- Number.prototype.toPercent = function (n) {
- n = n || 2;
- return (Math.round(this * Math.pow(10, n + 2)) / Math.pow(10, n)).toFixed(n) + '%';
- };
- String.prototype.trim = function () {
- var reExtraSpace = /^\s*(.*?)\s+$/;
- return this.replace(reExtraSpace, "$1");
- }
- Array.prototype.contains = function (v) {
- for (var i = 0; i < this.length; i++) {
- if (this[i] === v) return true;
- }
- return false;
- };
- Array.prototype.unique = function () {
- var arr = [];
- for (var i = 0; i < this.length; i++) {
- if (!arr.contains(this[i])) {
- arr.push(this[i]);
- }
- }
- return arr;
- };
- Array.prototype.uniqueObj = function (callback) {
- if (!callback || !$.isFunction(callback)) {
- return [];
- }
- var result = [], hash = {};
- for (var i = 0; i < this.length; i++) {
- var key = callback(this[i]);
- if (!hash[key]) {
- result.push(this[i]);
- hash[key] = true;
- }
- }
- return result;
- };
- Array.prototype.remove = function (dx) {
- if (isNaN(dx) || dx > this.length) { return false; }
- for (var i = 0, n = 0; i < this.length; i++) {
- if (this[i] != this[dx]) {
- this[n++] = this[i];
- }
- }
- this.length -= 1;
- };
- Array.isArray = function (o) {
- return Object.prototype.toString.call(o) == '[object Array]';
- };
- function Guid(g) {
- var arr = new Array(); //存放32位数值的数组
- if (typeof (g) == "string") { //如果构造函数的参数为字符串
- InitByString(arr, g);
- }
- else {
- InitByOther(arr);
- }
- //返回一个值,该值指示 Guid 的两个实例是否表示同一个值。
- this.Equals = function (o) {
- if (o && o.IsGuid) {
- return this.ToString() == o.ToString();
- }
- else {
- return false;
- }
- }
- //Guid对象的标记
- this.IsGuid = function () { }
- //返回 Guid 类的此实例值的 String 表示形式。
- this.ToString = function (format) {
- if (typeof (format) == "string") {
- if (format == "N" || format == "D" || format == "B" || format == "P") {
- return ToStringWithFormat(arr, format);
- }
- else {
- return ToStringWithFormat(arr, "D");
- }
- }
- else {
- return ToStringWithFormat(arr, "D");
- }
- }
- //由字符串加载
- function InitByString(arr, g) {
- g = g.replace(/\{|\(|\)|\}|-/g, "");
- g = g.toLowerCase();
- if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1) {
- InitByOther(arr);
- }
- else {
- for (var i = 0; i < g.length; i++) {
- arr.push(g[i]);
- }
- }
- }
- //由其他类型加载
- function InitByOther(arr) {
- var i = 32;
- while (i--) {
- arr.push("0");
- }
- }
- /*
-
- 根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。
-
- N 32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-
- D 由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
- B 括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
- P 括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
- */
- function ToStringWithFormat(arr, format) {
- switch (format) {
- case "N":
- return arr.toString().replace(/,/g, "");
- case "D":
- var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20, 32);
- str = str.replace(/,/g, "");
- return str;
- case "B":
- var str = ToStringWithFormat(arr, "D");
- str = "{" + str + "}";
- return str;
- case "P":
- var str = ToStringWithFormat(arr, "D");
- str = "(" + str + ")";
- return str;
- default:
- return new Guid();
- }
- }
- };
- //Guid 类的默认实例,其值保证均为零。
- Guid.Empty = new Guid();
- //初始化 Guid 类的一个新实例。
- Guid.NewGuid = function () {
- var g = "";
- var i = 32;
- while (i--) {
- g += Math.floor(Math.random() * 16.0).toString(16);
- }
- return new Guid(g);
- };
- function validate(value) {
- var reg = new RegExp("^[0-9]*$");
- if (!reg.test(value)) {
- return false;
- }
- else {
- return true;
- }
- };
|