How to convert American date format to European

前端 未结 3 505
无人及你
无人及你 2020-12-11 19:26

from : 5/19/2011

to : 2011-05-19

I need it to raise an error when it finds that it cannot be real like 5/40/2011 etc.

3条回答
  •  伪装坚强ぢ
    2020-12-11 20:23

    How about Datejs an open source date library? Specifically:

    http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact

    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
    

    Return Value {Date} A Date object or null if the string cannot be converted into a Date.

    or

    http://code.google.com/p/datejs/wiki/APIDocumentation#validateDay

    Date.validateDay(15, 2007, 1);  // true, 15-Feb-2007
    Date.validateDay(31, 2007, 10); // false, throws RangeError exception
    

    Return Value {Boolean} true if within range, otherwise false.

提交回复
热议问题