JavaScript.Extend.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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.ymdToDate = function () {
  76. var t = this;
  77. if (!t) {
  78. return null;
  79. }
  80. if (t.length == 0) {
  81. return null;
  82. }
  83. var d = eval("new Date(" + t.replace(/-/g, ",") + ")");
  84. return d;
  85. }
  86. String.prototype.toDateString = function (p) {
  87. return this.toDate().format(p);
  88. }
  89. String.prototype.toCHSSymbols = function () {
  90. /// <summary>
  91. /// 将字符串内所有英文符号替换为中文符号-_- Add by 劳炳辉 2013.09.06
  92. /// </summary>
  93. var s = this;
  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. // s = s.replace(/\(/g, "(");
  103. // s = s.replace(/\)/g, ")");
  104. // s = s.replace(/\[/g, "【");
  105. // s = s.replace(/\]/g, "】");
  106. // s = s.replace(/\{/g, "{");
  107. // s = s.replace(/\}/g, "}");
  108. // s = s.replace(/\`/g, "·");
  109. // s = s.replace(/\$/g, "¥");
  110. // s = s.replace(/\^/g, "……");
  111. // s = s.replace(/\_/g, "——");
  112. s = s.replace(/\你/g, "您");
  113. return s;
  114. }
  115. String.prototype.replaceDoubleSlashesToSingle = function () {
  116. var str = this;
  117. while (str.indexOf("//") != -1) {
  118. str = str.replace(/\/\//g, "/");
  119. }
  120. while (str.indexOf("\\\\") != -1) {
  121. str = str.replace(/\\\\/g, "\\");
  122. }
  123. return str.toString();
  124. }
  125. Number.prototype.add = function (b) {
  126. var c, d, e;
  127. try {
  128. c = this.toString().split(".")[1].length;
  129. } catch (f) {
  130. c = 0;
  131. }
  132. try {
  133. d = b.toString().split(".")[1].length;
  134. } catch (f) {
  135. d = 0;
  136. }
  137. return e = Math.pow(10, Math.max(c, d)), (this.mul(e) + b.mul(e)) / e;
  138. };
  139. Number.prototype.sub = function (b) {
  140. var c, d, e;
  141. try {
  142. c = this.toString().split(".")[1].length;
  143. } catch (f) {
  144. c = 0;
  145. }
  146. try {
  147. d = b.toString().split(".")[1].length;
  148. } catch (f) {
  149. d = 0;
  150. }
  151. return e = Math.pow(10, Math.max(c, d)), (this.mul(e) - b.mul(e)) / e;
  152. };
  153. Number.prototype.mul = function (b) {
  154. var c = 0,
  155. d = this.toString(),
  156. e = b.toString();
  157. try {
  158. c += d.split(".")[1].length;
  159. } catch (f) { }
  160. try {
  161. c += e.split(".")[1].length;
  162. } catch (f) { }
  163. return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
  164. };
  165. Number.prototype.div = function (b) {
  166. var c, d, e = 0,
  167. f = 0;
  168. try {
  169. e = this.toString().split(".")[1].length;
  170. } catch (g) { }
  171. try {
  172. f = b.toString().split(".")[1].length;
  173. } catch (g) { }
  174. return c = Number(this.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), (c / d).mul(Math.pow(10, f - e));
  175. }
  176. Number.prototype.toPercent = function (n) {
  177. n = n || 2;
  178. return (Math.round(this * Math.pow(10, n + 2)) / Math.pow(10, n)).toFixed(n) + '%';
  179. };
  180. String.prototype.trim = function () {
  181. var reExtraSpace = /^\s*(.*?)\s+$/;
  182. return this.replace(reExtraSpace, "$1");
  183. }
  184. Array.prototype.contains = function (v) {
  185. for (var i = 0; i < this.length; i++) {
  186. if (this[i] === v) return true;
  187. }
  188. return false;
  189. };
  190. Array.prototype.unique = function () {
  191. var arr = [];
  192. for (var i = 0; i < this.length; i++) {
  193. if (!arr.contains(this[i])) {
  194. arr.push(this[i]);
  195. }
  196. }
  197. return arr;
  198. };
  199. Array.prototype.uniqueObj = function (callback) {
  200. if (!callback || !$.isFunction(callback)) {
  201. return [];
  202. }
  203. var result = [], hash = {};
  204. for (var i = 0; i < this.length; i++) {
  205. var key = callback(this[i]);
  206. if (!hash[key]) {
  207. result.push(this[i]);
  208. hash[key] = true;
  209. }
  210. }
  211. return result;
  212. };
  213. Array.prototype.remove = function (dx) {
  214. if (isNaN(dx) || dx > this.length) { return false; }
  215. for (var i = 0, n = 0; i < this.length; i++) {
  216. if (this[i] != this[dx]) {
  217. this[n++] = this[i];
  218. }
  219. }
  220. this.length -= 1;
  221. };
  222. Array.isArray = function (o) {
  223. return Object.prototype.toString.call(o) == '[object Array]';
  224. };
  225. function Guid(g) {
  226. var arr = new Array(); //存放32位数值的数组
  227. if (typeof (g) == "string") { //如果构造函数的参数为字符串
  228. InitByString(arr, g);
  229. }
  230. else {
  231. InitByOther(arr);
  232. }
  233. //返回一个值,该值指示 Guid 的两个实例是否表示同一个值。
  234. this.Equals = function (o) {
  235. if (o && o.IsGuid) {
  236. return this.ToString() == o.ToString();
  237. }
  238. else {
  239. return false;
  240. }
  241. }
  242. //Guid对象的标记
  243. this.IsGuid = function () { }
  244. //返回 Guid 类的此实例值的 String 表示形式。
  245. this.ToString = function (format) {
  246. if (typeof (format) == "string") {
  247. if (format == "N" || format == "D" || format == "B" || format == "P") {
  248. return ToStringWithFormat(arr, format);
  249. }
  250. else {
  251. return ToStringWithFormat(arr, "D");
  252. }
  253. }
  254. else {
  255. return ToStringWithFormat(arr, "D");
  256. }
  257. }
  258. //由字符串加载
  259. function InitByString(arr, g) {
  260. g = g.replace(/\{|\(|\)|\}|-/g, "");
  261. g = g.toLowerCase();
  262. if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1) {
  263. InitByOther(arr);
  264. }
  265. else {
  266. for (var i = 0; i < g.length; i++) {
  267. arr.push(g[i]);
  268. }
  269. }
  270. }
  271. //由其他类型加载
  272. function InitByOther(arr) {
  273. var i = 32;
  274. while (i--) {
  275. arr.push("0");
  276. }
  277. }
  278. /*
  279. 根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。
  280. N 32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  281. D 由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  282. B 括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  283. P 括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
  284. */
  285. function ToStringWithFormat(arr, format) {
  286. switch (format) {
  287. case "N":
  288. return arr.toString().replace(/,/g, "");
  289. case "D":
  290. var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20, 32);
  291. str = str.replace(/,/g, "");
  292. return str;
  293. case "B":
  294. var str = ToStringWithFormat(arr, "D");
  295. str = "{" + str + "}";
  296. return str;
  297. case "P":
  298. var str = ToStringWithFormat(arr, "D");
  299. str = "(" + str + ")";
  300. return str;
  301. default:
  302. return new Guid();
  303. }
  304. }
  305. };
  306. //Guid 类的默认实例,其值保证均为零。
  307. Guid.Empty = new Guid();
  308. //初始化 Guid 类的一个新实例。
  309. Guid.NewGuid = function () {
  310. var g = "";
  311. var i = 32;
  312. while (i--) {
  313. g += Math.floor(Math.random() * 16.0).toString(16);
  314. }
  315. return new Guid(g);
  316. };
  317. function validate(value) {
  318. var reg = new RegExp("^[0-9]*$");
  319. if (!reg.test(value)) {
  320. return false;
  321. }
  322. else {
  323. return true;
  324. }
  325. };