'Safari Javascript Date() NaN Issue (yyyy-MM-dd HH:mm:ss)
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">
Solution 1:[1]
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
Solution 2:[2]
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.');
});
}
Solution 3:[3]
It works in Safari if you replace the dashes with slashes:
dateTimeString.replace(/-/g, '/')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhishek Jain |
| Solution 2 | Jamie Dunstan |
| Solution 3 | Neets |
