下面要说的这几个方法,很少有人用到,或者说少有人知道.所以,我看到很多js文件里开发者们大把大把的重复造轮子,这应该是框架维护人员的责任,一直以来,没有形成个详细的api文档.但也说明,开发人员之间缺少对框架的关注,或者说,交流不够.
formatNumber2: function (value){ return Math.round(value * 100) / 100; }, //数值精确到<digit>小数位 formatNum: function (value, digit){ if (digit == undefined) return Math.round(value * 100) / 100; else{ if (digit == 0){ return Math.round(value); } else{ var tmp = Math.pow(10, digit); return Math.round(value * tmp) / tmp; } } }, formatWZ: function (value){ return "" + value + "万支"; }, //数值加上百分号 formatPercent: function (value){ return value + "%"; }, formatPercent2: function (value){ return Math.round(value * 10000) / 100 + "%"; }, //字期值<string>转化为YYYY-MM-dd格式 formatDate: function (v){ if (v == undefined) v = ""; if (v.indexOf('-') == - 1) return v.substr(0, 4) + "-" + v.substr(4, 2) + "-" + v.substr(6, 2); else return v; }, //字期值<string>转化为YYYY-MM-DD hh:mm:ss格式 toFullDate: function (v){ return v.substr(0, 4) + "-" + v.substr(4, 2) + "-"//月 + v.substr(6, 2) + " "//日 + v.substr(8, 2) + ":"//时 + v.substr(10, 2) + ":"//分 + v.substr(12, 2);//秒 }, toDate: function (v){ var flag = (v.indexOf('-') == - 1) ? true : false; var year = v.substr(0, 4); var month = (flag) ? v.substr(4, 2) : v.substr(5, 2); var day = (flag) ? v.substr(6, 2) : v.substr(8, 2); month = parseInt(month) - 1; return new Date(year, month, day); }, toTime: function (value){ if (value == "") return ""; if (value.length & gt; 6 & amp; & amp; value.indexOf(":") == - 1) value = value.left(6); if (value.indexOf(":") == - 1 & amp; & amp; value.length == 6){ if ( ! Sail.isTime(value)) return ""; var vs = "" + value; return vs.substr(0, 2) + ":" + vs.substr(2, 2) + ":" + vs.substr(4, 2); } var vs = value.split(":"); if (vs.length == 1){ var vback = value; vs = []; var h = parseInt(vback.left(2), 10) || 0; vs.push((h & gt; 23) ? vback.left(1) : "" + h); vback = ((h & gt; 23) ? vback.right(vback.length - 1) : vback.right(vback.length - 2)); var h = parseInt(vback.left(2), 10) || 0; vs.push((h & gt; 59) ? vback.left(1) : "" + h); vback = ((h & gt; 59) ? vback.right(vback.length - 1) : vback.right(vback.length - 2)); vs.push((parseInt(vback, 10) & gt; 59) ? "0" : vback); } for (var i = 0; i & lt; vs.length; i++){ vs[i] = (vs[i] == "") ? "00" : vs[i]; if (vs[i].length == 1 & amp; & amp; parseInt(vs[i], 10) & lt; 10 & amp; & amp; parseInt(vs[i], 10) != 0){ vs[i] = "0" + vs[i]; } if (vs[i] == "0") vs[i] = "00"; } return vs.join(":"); }, num2String: function (num, format){ var str = "" + num; var formatLen = format.length; if (str.length & lt; formatLen){ var len = formatLen - str.length; for (var i = 0; i & lt; len; i++) str = "0" + str; } return str; }
其中我注释的函数是项目中实际用得比较多的,但大家不知道,所以就重复的造轮子.很简单,比如我要转换一个字符串为日期,就可以这样写.Sail.toFullDate(argv)
如果不知道,也不去关注.那就悲剧了,截图为证: