0

This date converter function working fine on chrome but not on Firefox! any one can help figure out problem.

function converter(string) {
    var d = new Date(string);
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'sep', 'Oct', 'Nov', 'Dec'];

    var hour = d.getHours();

    return d.getFullYear() + ' ' + months[d.getMonth()] + ' ' + d.getDate() + ' | ' + (hour % 12) + ' ' + d.getMinutes() + ':' + (hour > 11 ? 'pm' : 'am');
}

alert(converter('2013-03-10 19:43:55'))
4

2 に答える 2

3

This will fix your code:

converter('2013-03-10T19:43:55')

Please note the T between the date and the time.

Edit: The datetime string should be an ISO 8601 format. Read more about it here.

于 2013-03-14T11:47:21.463 に答える
1

Chrome is more forgiving of bad date formats, but that's technically not valid as input to Date.parse or new Date. You should make sure that it's a correctly formatted date (RFC2822 or ISO 8601), or if you want to allow more freeform input, use a library like http://www.datejs.com/

More information: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse

于 2013-03-14T11:47:47.513 に答える