Safari Javascript Date() NaN Issue (yyyy-MM-dd HH:mm:ss)
Asked Answered
O

6

37

My code is working properly in Google Chrome, but not in Safari.

I figured out that I need to convert yyyy-MM-dd HH:mm:ss to ISO 8601, but I didn't found a solution to do this.


Online Test Link: http://jsfiddle.net/UVgHR/


Javascript:

$(document).ready(function() {

    calculateMinutes();
    setInterval(calculateMinutes, 60000);

});

function calculateMinutes() {
    $('.calculateMinutes').each(function () {
        var diff = Math.abs(new Date( $(this).data('timestamp') ) - new Date());
        var minutes = Math.floor((diff/1000)/60);
        $(this).html( minutes + ' min.' );
    });
}

HTML Example:

<span class="calculateMinutes" data-timestamp="2014-02-18 15:00:48">
Oaken answered 19/2, 2014 at 14:39 Comment(0)
G
100

To make your question easier your problem is with:

new Date('2014-02-18 15:00:48')

This work okay in chrome but not in safari. The mdn talks about ECMAScript 5 ISO-8601 format support says:

Alternatively, the date/time string may be in ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed.

If you include T it works:

new Date('2014-02-18T15:00:48')

You can use new Date('2014-02-18T15:00:48'.replace(/\s/, 'T')).

If you handle a lot of cases like this I will recommend using moment which seems to handle this case very well with or without T: parsing from string. Additionally your whole example is easier with momentjs:

var minutes = moment().diff("2014-02-18 15:00:48", 'minutes');
Graz answered 19/2, 2014 at 14:59 Comment(2)
it is pretty handy utility indeed. I can't imagine working with dates in javascript without moment.Aylmer
moment is great but it's also enormous. moment alone is almost 2x bigger than my entire appMetropolis
F
13

I think Jose missed one point here - Do not forget to include Z or else there will be lag of the timezone.

new Date('2014-02-18T15:00:48') new Date('2014-02-18T15:00:48Z')

You can use new Date('2014-02-18T15:00:48'.replace(/\s/, 'T')+'Z').

Refer this for more info - new Date() works differently in Chrome and Firefox

Ferebee answered 13/12, 2017 at 10:11 Comment(4)
Please comment on Jose's answer, in stead of a new answer.Thatcher
I can't comment unless it's my own answer. Says - "You must have 50 reputation to comment" :(Ferebee
Noticed this issue in iOS and ended up here looking for the solution. Worked well for my needs (handled string replacement in PHP on a MySQL datetime value). Upvoting this answer since @AbhishekJain doesn't have the rep to comment, and upvoting José's answer as well ;)Delegacy
This solution adds 5 hours in the date. Which is wrong implementationOvermatch
S
5

I've seen an issue like this before and what works for me is to replace the space between the Date and the Time with a T. Try this:

Updated JavaScript:

function calculateMinutes() {
    $('.calculateMinutes').each(function () {
        var timestamp = $(this).data('timestamp').replace(' ', 'T');
        var diff = Math.abs(new Date(timestamp) - new Date());
        var minutes = Math.floor((diff / 1000) / 60);
        $(this).html(minutes + ' min.');
    });
}

JSFiddle here.

Salable answered 19/2, 2014 at 14:50 Comment(0)
O
1

Change this piece of code

new Date('2014-02-18 15:00:48')

To

new Date('2014-02-18 15:00:48'.replace(" ", "T"))
Overmatch answered 28/7, 2022 at 8:24 Comment(0)
L
0

It works in Safari if you replace the dashes with slashes:

dateTimeString.replace(/-/g, '/')

Loree answered 18/5, 2022 at 15:23 Comment(0)
I
0

Just replacing "-" to "/" may works.

TO

var myTime = new Date('03/09/2022 00:00:00');
Interjacent answered 4/11, 2022 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.