zoukankan      html  css  js  c++  java
  • Javascript扩展String.prototype实现格式金额、格式时间、字符串连接、计算长度、是否包含、日期计算等功能

      1 <script src="Js/jquery-3.1.1.min.js"></script>
      2 <script type="text/javascript">
      3 /*
      4 * 格式化金额 个位起每三位逗号分隔 10,000.00
      5 * @param n 小数位
      6 * @return
      7 */
      8 String.prototype.toThousands = function (n) {
      9 var n = n > 0 && n <= 20 ? n : 2;
     10 var s = this;
     11 s = parseFloat((s + "").replace(/[^d.-]/g, "")).toFixed(n) + "";
     12 var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
     13 t = "";
     14 for (i = 0; i < l.length; i++) {
     15 t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
     16 }
     17 return t.split("").reverse().join("") + "." + r;
     18 }
     19 
     20 /*
     21 * 类似C#中string.format函数
     22 * @return
     23 */
     24 String.prototype.format = function () {
     25 var result = this;
     26 if (arguments.length == 0)
     27 return null;
     28 for (var i = 0; i < arguments.length; i++) {
     29 var re = new RegExp('\{' + (i) + '\}', 'gm');
     30 result = result.replace(re, arguments[i]);
     31 }
     32 return result;
     33 };
     34 
     35 /*
     36 * 返回字符的长度,一个中文算2个
     37 * @return
     38 */ 
     39 String.prototype.strLength = function () {
     40 return this.replace(/[^x00-xff]/g, "**").length;
     41 };
     42 
     43 /*
     44 * 判断字符串是否以指定的字符串开始
     45 * @return
     46 */ 
     47 String.prototype.strStartsWith = function (str) {
     48 return this.substr(0, str.length) == str;
     49 };
     50 
     51 /*
     52 * 判断密码安全级别
     53 * @return
     54 */
     55 String.prototype.checkPassWordLevel = function () {
     56 var n = 0;
     57 if (/d/.test(this)) n++; //包含数字 
     58 if (/[a-z]/.test(this)) n++; //包含小写字母 
     59 if (/[A-Z]/.test(this)) n++; //包含大写字母 
     60 if (this.length <= 6) n = 1; //长度小于等于6位 
     61 return n;
     62 }
     63 
     64 /* 
     65 * 检索数组元素(原型扩展或重载) 
     66 * @param o 被检索的元素值 
     67 * @returns 元素索引 
     68 */
     69 Array.prototype.contains = function (o) {
     70 var index = -1;
     71 for (var i = 0; i < this.length; i++) { if (this[i] == o) { index = i; break; } }
     72 return index;
     73 }
     74 
     75 /*
     76 * 日期格式化(原型扩展或重载) 
     77 * 格式 YYYY/yyyy/YY/yy 表示年份 
     78 * MM/M 月份 
     79 * W/w 星期 
     80 * dd/DD/d/D 日期 
     81 * hh/HH/h/H 时间 
     82 * mm/m 分钟 
     83 * ss/SS/s/S 秒 
     84 * @param {formatStr} 格式模版 
     85 * @returns 日期字符串 
     86 */
     87 Date.prototype.formatDate = function (formatStr) {
     88 var str = formatStr;
     89 var Week = ['日', '一', '二', '三', '四', '五', '六'];
     90 str = str.replace(/yyyy|YYYY/, this.getFullYear());
     91 str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
     92 str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
     93 str = str.replace(/M/g, this.getMonth());
     94 str = str.replace(/w|W/g, Week[this.getDay()]);
     95 str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
     96 str = str.replace(/d|D/g, this.getDate());
     97 str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
     98 str = str.replace(/h|H/g, this.getHours());
     99 str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    100 str = str.replace(/m/g, this.getMinutes());
    101 str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    102 str = str.replace(/s|S/g, this.getSeconds());
    103 return str;
    104 }
    105 
    106 /* 
    107 * 比较日期差(原型扩展或重载) 
    108 * @param {strInterval} 日期类型:'y、m、d、h、n、s、w' 
    109 * @param {dtEnd} 格式为日期型或者 有效日期格式字符串 
    110 * @returns 比较结果 
    111 */
    112 Date.prototype.dateDiff = function (strInterval, dtEnd) {
    113 var dtStart = this;
    114 if (typeof dtEnd == 'string') { //如果是字符串转换为日期型 
    115 dtEnd = StringToDate(dtEnd);
    116 }
    117 switch (strInterval) {
    118 case 's': return parseInt((dtEnd - dtStart) / 1000);
    119 case 'n': return parseInt((dtEnd - dtStart) / 60000);
    120 case 'h': return parseInt((dtEnd - dtStart) / 3600000);
    121 case 'd': return parseInt((dtEnd - dtStart) / 86400000);
    122 case 'w': return parseInt((dtEnd - dtStart) / (86400000 * 7));
    123 case 'm': return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);
    124 case 'y': return dtEnd.getFullYear() - dtStart.getFullYear();
    125 }
    126 }
    127 
    128 /* 
    129 * 日期计算(原型扩展或重载) 
    130 * @param {strInterval} 日期类型:'y、m、d、h、n、s、w' 
    131 * @param {Number} 数量 
    132 * @returns 计算后的日期 
    133 */
    134 Date.prototype.dateAdd = function (strInterval, Number) {
    135 var dtTmp = this;
    136 switch (strInterval) {
    137 case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));
    138 case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));
    139 case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));
    140 case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));
    141 case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
    142 case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    143 case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    144 case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    145 }
    146 }
    147 
    148 /* 
    149 * 取得日期数据信息(原型扩展或重载) 
    150 * @param {interval} 日期类型:'y、m、d、h、n、s、w' 
    151 * @returns 指定的日期部分 
    152 */
    153 Date.prototype.datePart = function (interval) {
    154 var myDate = this;
    155 var partStr = '';
    156 var Week = ['日', '一', '二', '三', '四', '五', '六'];
    157 switch (interval) {
    158 case 'y': partStr = myDate.getFullYear(); break;
    159 case 'm': partStr = myDate.getMonth() + 1; break;
    160 case 'd': partStr = myDate.getDate(); break;
    161 case 'w': partStr = Week[myDate.getDay()]; break;
    162 case 'ww': partStr = myDate.WeekNumOfYear(); break;
    163 case 'h': partStr = myDate.getHours(); break;
    164 case 'n': partStr = myDate.getMinutes(); break;
    165 case 's': partStr = myDate.getSeconds(); break;
    166 }
    167 return partStr;
    168 }
    169 
    170 /* 
    171 * 字符串包含(原型扩展或重载) 
    172 * @param {string: str} 要搜索的子字符串 
    173 * @param {bool: mode} 是否忽略大小写 
    174 * @returns 匹配的个数 
    175 */
    176 String.prototype.matchCount = function (str, mode) {
    177 return eval("this.match(/(" + str + ")/g" + (mode ? "i" : "") + ").length");
    178 }
    179 
    180 /* 
    181 * 去除左右空格(原型扩展或重载) 
    182 * @type string 
    183 * @returns 处理后的字符串 
    184 */
    185 String.prototype.trim = function () {
    186 return this.replace(/(^s*)|(s*$)/g, "");
    187 }
    188 
    189 
    190 /* 
    191 * 字符串转换为日期型(原型扩展或重载) 
    192 * @type Date 
    193 * @returns 日期 
    194 */
    195 String.prototype.toDate = function () {
    196 var converted = Date.parse(this);
    197 var myDate = new Date(converted);
    198 if (isNaN(myDate)) { var arys = this.split('-'); myDate = new Date(arys[0], --arys[1], arys[2]); }
    199 return myDate;
    200 }
    201 </script>
    202 <script type="text/javascript">
    203 //测试 
    204 var a = "{0}--{1}".format("张三", "的歌") //张三--的歌
    205 console.log(a);
    206 var b = "10000".toThousands(2)
    207 console.log(b); //10,000.00
    208 var c = "电脑aabbA";
    209 console.log(c.strLength()); //9
    210 console.log(c.strStartsWith("电脑")); //true
    211 console.log(c.checkPassWordLevel()); //2
    212 var d = [1, 2, 3, 4, 5];
    213 console.log(d.contains(6)); //-1 不存在
    214 
    215 var date = new Date("2016-12-12");
    216 console.log(date.formatDate("yy-MM")); //16-12
    217 var date2 = new Date("2016-12-16");
    218 console.log(date.dateDiff("d", date2)); //4
    219 console.log(date.dateAdd("y", 2).toLocaleDateString()); //Wed Dec 12 2018
    220 console.log(date.datePart("d")); //12
    221 console.log(date.datePart("w")) //一 (星期一)
    222 console.log(c.matchCount("a", true)); //3
    223 console.log(" 我的 ".trim()) //我的
    224 console.log("2016 12 12".toDate().toLocaleDateString()) //Mon Dec 12 2016
    225 </script>
  • 相关阅读:
    SpringCloud学习笔记(6)——Eureka高可用
    上传大数据到SharePoint 2010
    批量导入导出站点权限site permissions
    List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user
    Change upload file limit in specified Webapllication in SharePoint (PowerShell)
    导出SharePoint2013用户及权限
    启用SharePoint 2013文档版本控制
    移除SharePoint2013里的NoteBook笔记本链接
    启用SharePoint 的 web application下面所有站点“备用语言”
    SharePoint 2013 event id 8321 错误
  • 原文地址:https://www.cnblogs.com/luorende/p/10077119.html
Copyright © 2011-2022 走看看