JavaScript.Extend.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. Date.prototype.addDay = function (num) {
  2. var tempDate = new Date(this);
  3. tempDate.setDate(this.getDate() + num);
  4. return tempDate;
  5. }
  6. Date.prototype.addMonth = function (num) {
  7. var tempDate = this.getDate();
  8. this.setMonth(this.getMonth() + num);
  9. if (tempDate != this.getDate()) this.setDate(0);
  10. return this;
  11. }
  12. Date.prototype.addYear = function (num) {
  13. var tempDate = this.getDate();
  14. this.setYear(this.getYear() + num);
  15. if (tempDate != this.getDate()) this.setDate(0);
  16. return this;
  17. }
  18. Date.prototype.substractDay = function (date) {
  19. return Math.ceil((this - date) / 24 / 60 / 60 / 1000);
  20. }
  21. /**
  22. * 对Date的扩展,将 Date 转化为指定格式的String
  23. * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
  24. * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  25. * eg:
  26. * (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  27. * (new Date()).format("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
  28. * (new Date()).format("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
  29. * (new Date()).format("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
  30. * (new Date()).format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  31. */
  32. Date.prototype.format = function (fmt) {
  33. var o = {
  34. "M+": this.getMonth() + 1, //月份
  35. "d+": this.getDate(), //日
  36. "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
  37. "H+": this.getHours(), //小时
  38. "m+": this.getMinutes(), //分
  39. "s+": this.getSeconds(), //秒
  40. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  41. "S": this.getMilliseconds() //毫秒
  42. };
  43. var week = {
  44. "0": "/u65e5",
  45. "1": "/u4e00",
  46. "2": "/u4e8c",
  47. "3": "/u4e09",
  48. "4": "/u56db",
  49. "5": "/u4e94",
  50. "6": "/u516d"
  51. };
  52. if (/(y+)/.test(fmt)) {
  53. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  54. }
  55. if (/(E+)/.test(fmt)) {
  56. fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
  57. }
  58. for (var k in o) {
  59. if (new RegExp("(" + k + ")").test(fmt)) {
  60. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  61. }
  62. }
  63. return fmt;
  64. }
  65. String.prototype.toDate = function () {
  66. var t = this;
  67. if (t.split("T").length > 1 || t.split("Z").length > 1) {
  68. var t1 = this.replace(/T/g, ' ').replace(/Z/g, ' ');
  69. var time = new Date(Date.parse(t1.replace(/-/g, "/"))).getTime();
  70. t = "/Date(" + time + ")/";
  71. }
  72. var d = eval(t.replace(/\/(Date\(-?\d+\))\//ig, "new $1"));
  73. return d;
  74. }
  75. String.prototype.toDateString = function (p) {
  76. return this.toDate().format(p);
  77. }
  78. String.prototype.toCHSSymbols = function () {
  79. /// <summary>
  80. /// 将字符串内所有英文符号替换为中文符号-_- Add by 劳炳辉 2013.09.06
  81. /// </summary>
  82. var s = this;
  83. s = s.replace(/\,/g, ",");
  84. // s = s.replace(/\./g, "。");
  85. s = s.replace(/\!/g, "!");
  86. s = s.replace(/\?/g, "?");
  87. // s = s.replace(/\:/g, ":");
  88. // s = s.replace(/\;/g, ";");
  89. // s = s.replace(/\</g, "《");
  90. // s = s.replace(/\>/g, "》");
  91. // s = s.replace(/\(/g, "(");
  92. // s = s.replace(/\)/g, ")");
  93. // s = s.replace(/\[/g, "【");
  94. // s = s.replace(/\]/g, "】");
  95. // s = s.replace(/\{/g, "{");
  96. // s = s.replace(/\}/g, "}");
  97. // s = s.replace(/\`/g, "·");
  98. // s = s.replace(/\$/g, "¥");
  99. // s = s.replace(/\^/g, "……");
  100. // s = s.replace(/\_/g, "——");
  101. s = s.replace(/\你/g, "您");
  102. return s;
  103. }
  104. String.prototype.replaceDoubleSlashesToSingle = function () {
  105. var str = this;
  106. while (str.indexOf("//") != -1) {
  107. str = str.replace(/\/\//g, "/");
  108. }
  109. while (str.indexOf("\\\\") != -1) {
  110. str = str.replace(/\\\\/g, "\\");
  111. }
  112. return str.toString();
  113. }
  114. Number.prototype.add = function (b) {
  115. var c, d, e;
  116. try {
  117. c = this.toString().split(".")[1].length;
  118. } catch (f) {
  119. c = 0;
  120. }
  121. try {
  122. d = b.toString().split(".")[1].length;
  123. } catch (f) {
  124. d = 0;
  125. }
  126. return e = Math.pow(10, Math.max(c, d)), (this.mul(e) + b.mul(e)) / e;
  127. };
  128. Number.prototype.sub = function (b) {
  129. var c, d, e;
  130. try {
  131. c = this.toString().split(".")[1].length;
  132. } catch (f) {
  133. c = 0;
  134. }
  135. try {
  136. d = b.toString().split(".")[1].length;
  137. } catch (f) {
  138. d = 0;
  139. }
  140. return e = Math.pow(10, Math.max(c, d)), (this.mul(e) - b.mul(e)) / e;
  141. };
  142. Number.prototype.mul = function (b) {
  143. var c = 0,
  144. d = this.toString(),
  145. e = b.toString();
  146. try {
  147. c += d.split(".")[1].length;
  148. } catch (f) { }
  149. try {
  150. c += e.split(".")[1].length;
  151. } catch (f) { }
  152. return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
  153. };
  154. Number.prototype.div = function (b) {
  155. var c, d, e = 0,
  156. f = 0;
  157. try {
  158. e = this.toString().split(".")[1].length;
  159. } catch (g) { }
  160. try {
  161. f = b.toString().split(".")[1].length;
  162. } catch (g) { }
  163. return c = Number(this.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), (c / d).mul(Math.pow(10, f - e));
  164. }
  165. Number.prototype.toPercent = function (n) {
  166. n = n || 2;
  167. return (Math.round(this * Math.pow(10, n + 2)) / Math.pow(10, n)).toFixed(n) + '%';
  168. };
  169. String.prototype.trim = function () {
  170. var reExtraSpace = /^\s*(.*?)\s+$/;
  171. return this.replace(reExtraSpace, "$1");
  172. }
  173. Array.prototype.contains = function (v) {
  174. for (var i = 0; i < this.length; i++) {
  175. if (this[i] === v) return true;
  176. }
  177. return false;
  178. };
  179. Array.prototype.unique = function () {
  180. var arr = [];
  181. for (var i = 0; i < this.length; i++) {
  182. if (!arr.contains(this[i])) {
  183. arr.push(this[i]);
  184. }
  185. }
  186. return arr;
  187. };
  188. Array.prototype.uniqueObj = function (callback) {
  189. if (!callback || !$.isFunction(callback)) {
  190. return [];
  191. }
  192. var result = [], hash = {};
  193. for (var i = 0; i < this.length; i++) {
  194. var key = callback(this[i]);
  195. if (!hash[key]) {
  196. result.push(this[i]);
  197. hash[key] = true;
  198. }
  199. }
  200. return result;
  201. };
  202. Array.prototype.remove = function (dx) {
  203. if (isNaN(dx) || dx > this.length) { return false; }
  204. for (var i = 0, n = 0; i < this.length; i++) {
  205. if (this[i] != this[dx]) {
  206. this[n++] = this[i];
  207. }
  208. }
  209. this.length -= 1;
  210. };
  211. Array.isArray = function (o) {
  212. return Object.prototype.toString.call(o) == '[object Array]';
  213. };
  214. function Guid(g) {
  215. var arr = new Array(); //存放32位数值的数组
  216. if (typeof (g) == "string") { //如果构造函数的参数为字符串
  217. InitByString(arr, g);
  218. }
  219. else {
  220. InitByOther(arr);
  221. }
  222. //返回一个值,该值指示 Guid 的两个实例是否表示同一个值。
  223. this.Equals = function (o) {
  224. if (o && o.IsGuid) {
  225. return this.ToString() == o.ToString();
  226. }
  227. else {
  228. return false;
  229. }
  230. }
  231. //Guid对象的标记
  232. this.IsGuid = function () { }
  233. //返回 Guid 类的此实例值的 String 表示形式。
  234. this.ToString = function (format) {
  235. if (typeof (format) == "string") {
  236. if (format == "N" || format == "D" || format == "B" || format == "P") {
  237. return ToStringWithFormat(arr, format);
  238. }
  239. else {
  240. return ToStringWithFormat(arr, "D");
  241. }
  242. }
  243. else {
  244. return ToStringWithFormat(arr, "D");
  245. }
  246. }
  247. //由字符串加载
  248. function InitByString(arr, g) {
  249. g = g.replace(/\{|\(|\)|\}|-/g, "");
  250. g = g.toLowerCase();
  251. if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1) {
  252. InitByOther(arr);
  253. }
  254. else {
  255. for (var i = 0; i < g.length; i++) {
  256. arr.push(g[i]);
  257. }
  258. }
  259. }
  260. //由其他类型加载
  261. function InitByOther(arr) {
  262. var i = 32;
  263. while (i--) {
  264. arr.push("0");
  265. }
  266. }
  267. /*
  268. 根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。
  269. N 32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  270. D 由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  271. B 括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  272. P 括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
  273. */
  274. function ToStringWithFormat(arr, format) {
  275. switch (format) {
  276. case "N":
  277. return arr.toString().replace(/,/g, "");
  278. case "D":
  279. var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20, 32);
  280. str = str.replace(/,/g, "");
  281. return str;
  282. case "B":
  283. var str = ToStringWithFormat(arr, "D");
  284. str = "{" + str + "}";
  285. return str;
  286. case "P":
  287. var str = ToStringWithFormat(arr, "D");
  288. str = "(" + str + ")";
  289. return str;
  290. default:
  291. return new Guid();
  292. }
  293. }
  294. };
  295. //Guid 类的默认实例,其值保证均为零。
  296. Guid.Empty = new Guid();
  297. //初始化 Guid 类的一个新实例。
  298. Guid.NewGuid = function () {
  299. var g = "";
  300. var i = 32;
  301. while (i--) {
  302. g += Math.floor(Math.random() * 16.0).toString(16);
  303. }
  304. return new Guid(g);
  305. };
  306. function validate(value) {
  307. var reg = new RegExp("^[0-9]*$");
  308. if (!reg.test(value)) {
  309. return false;
  310. }
  311. else {
  312. return true;
  313. }
  314. };