我有这两个函数创建一个正确格式的新字符串(mm-dd-yyyy
),但现在它似乎不能很好地工作...当我输入日期31-03-2013
这是一个有效的日期,它出来与04-01-2013
在第一后的一个月....日期将2013年4月1日而不是2013年3月31日
这里有两个功能:
Date.prototype.sqlDate = Date.prototype.sqlDate || function() {
return this.getMonth() + "-" + this.getDate() + "-" + this.getFullYear();
};
String.prototype.sqlDate = String.prototype.sqlDate || function() {
var date = new Date(0);
var s = this.split("-");
//If i log "s" here its output is:
// ["31", "03", "2013", max: function, min: function]
date.setDate(s[0]);
date.setMonth(s[1]);
date.setYear(s[2]);
return date.sqlDate();
};
请记住,JavaScript将返回基于0的月份值,而日期和年份将基于1。所以一月份就会是0. – 2013-03-12 21:17:30
呵呵。忘了这个。 – FabianCook 2013-03-12 21:18:06
Javascript的这个“特性”是我遇到过的最愚蠢的设计决定之一。 – 2013-03-12 21:19:18