先看实现函数:
function fromString(str, unsigned, radix) {// 处理异常情况
if (str.length === 0)throw Error('empty string')
//处理为0的情况
if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")return ZERO
//处理只有两个参数的情况
if (typeof unsigned === 'number') {
// For goog.math.long compatibility
radix = unsigned,
unsigned = false
} else {
unsigned = !! unsigned
}
radix = radix || 10 if (radix <2 || 36 <radix)throw RangeError('radix')
var p if ((p = str.indexOf('-')) >0)throw Error('interior hyphen')
}
js中把long型的日期转换成String类型的代码如下
Date.prototype.format = function(f){var o ={
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(f))f=f.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length))
for(var k in o)
if(new RegExp("("+ k +")").test(f))f = f.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length))return f
}
以上代码用到正则表达式进行解决
正则表达式,又称规则表达式。计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。