How to convert American date format to European
来自:
至:
当它发现它不能像
也许这不是最佳解决方案,但是您可以尝试以下简单方法:
1 2 3 | var from="5/19/2011"; var temp = from.split("/"); var to = temp[2] +"-" + temp[0] +"-" + temp[1]; |
保持简单:
[edit]对,您也想要检查,因此添加了fn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function zeroPad(n){ return (parseInt(n,10)<10 ? '0' : '') + n; } var usdat = '5/19/2011'.split('/') ,eudat = [usdat[2],zeroPad(usdat[0]),zeroPad(usdat[1])]; alert(chkDat(usdat,eudat); //=> true alert(eudat.join('-')); //=> '2011-05-19' function chkDat(orig,eu){ var eu = new Date(eu.join('/')); return eu.getMonth()+1 === parseInt(orig[0],10) && eu.getDate() === parseInt(orig[1],10) && eu.getFullYear() === parseInt(orig[2],10) ; } |
请注意,您使用的日期格式称为"日历日期"(ISO 8601)。
Datejs开源日期库怎么样?具体来说:
http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact
1 2 3 4 | Date.parseExact("10/15/2004","M/d/yyyy"); // The Date of 15-Oct-2004 Date.parse("15-Oct-2004","d-MMM-yyyy"); // The Date of 15-Oct-2004 Date.parse("2004.10.15","yyyy.MM.dd"); // The Date of 15-Oct-2004 Date.parseExact("10/15/2004", ["M/d/yyyy","MMMM d, yyyy"]); // The Date of 15-Oct-2004 |
返回值
{Date}日期对象;如果不能将字符串转换为日期,则为null。
或
http://code.google.com/p/datejs/wiki/APIDocumentation#validateDay
1 2 | Date.validateDay(15, 2007, 1); // true, 15-Feb-2007 Date.validateDay(31, 2007, 10); // false, throws RangeError exception |
返回值
{Boolean}如果在范围内,则为true,否则为false。