javascript date object issue in Safari and IE

后端 未结 5 1685
广开言路
广开言路 2020-12-16 03:02

I am taking a date from a JSON object in the format of 2012-12-31 and trying to convert it into friendly values and output it.

    var redeemableDate = item.         


        
5条回答
  •  失恋的感觉
    2020-12-16 03:40

    The yyyy-mm-dd (ISO 8601) date format is not supported in Safari and IE. It is part of ECMAscript 5 though, so it should be just a matter of time.

    A solution would be to pass the date in as arguments to Date.

    var date = "2012-12-31".split("-");
    var your_date = new Date(date[0], date[1]-1, date[2]);
    

    Note that month parameter starts at zero (for January), so you must subtract 1 from the value obtained from the string.

    EDIT: For a shortcut see answer by joe larson below.

提交回复
热议问题