Javascript "Invalid Date" error in Safari - javascript

I've been trying to debug a script of mine and I can't get my head around what's wrong with this:
var date = new Date("19871104071535".replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$4:$5:$6 $2/$3/$1'
));
alert(date);
It works in Firefox, IE, Chrome but Safari gives me an "Invalid Date" error. Any ideas?

The Time and Date are in the wrong order (for just Safari I guess :):
I tested this in Safari and it works (I just swapped Date and Time position in the final string):
var date = new Date("19871104071535".replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$2/$3/$1 $4:$5:$6'
));
alert(date);
It will also work in the other browsers because this is what is expected.

Related

Moment Js produce invalid date in Safari

Hello any one having idea why i am getting "invalid date" in Safari, in other browsers works as expected.
moment('01/01/2023 11:44:00.000 AM').tz(time_zone, true).format('hh:mm:ss:SS A z')
chrome
safari
I have tried something like
.replace(/ /g,"T")
but no luck any idea what to try?
Look like this will work, provide date format as second param.
moment('01/01/2023 11:44:00.000 AM', 'MM/dd/yyyy HH:mm:ss.SSS`').tz(time_zone, true).format('hh:mm:ss:SS A z')

How to fix parsing error by moment.js on Safari?

How to fix parsing error by moment.js?
Const.DATE_MONTH_FORMAT = 'DD-MMM-YYYY';
var fromDate = moment(new Date(period.fromDate)).format(Const.DATE_MONTH_FORMAT);
On Firefox, Chrome: 19-Aug-2016
On Safari: Invalid date
On Firefox:
console.debug(period);
Object { fromDate="19/Aug/2016", toDate="30/Aug/2016"}
format() is used for output, not for determining what format the input is in. You're also passing period.fromDate to a Date constructor, rather than to moment, so you're using the browser's own parsing, which can do whatever it likes when dates don't conform to the JS spec (which DD/MMM/YYYY doesn't). Firefox and Chrome evidently think they can pluck a date from period.fromDate, while Safari can't. You should be able to fix this by passing in the string directly to moment, along with the format of period.fromDate, so it can do all the work itself:
Const.DATE_MONTH_FORMAT = 'DD-MMM-YYYY';
var fromDate = moment(period.fromDate, "DD/MMM/YYYY").format(Const.DATE_MONTH_FORMAT);

Chrome and Firefox parse Date differently

I found chrome parse some date pattern differently, the time string "2015-07-13T07:30:00" got from moment(time).format()
Chrome:
new Date("2015-07-13T07:30:00").toISOString()
// output "2015-07-13T07:30:00.000Z"
Firefox:
new Date("2015-07-13T07:30:00").toISOString()
// output "2015-07-12T23:30:00.000Z"
Firefox's output is my desired output, Chrome automatically assumes the timezone is utf offset 0, how can I get fix this?

Javascript invalid Date on iphone

I have a webpage that creates a date from a string. It works fine except for the iphone where I get invalid date.
I have read a small bit about IOS handling dates a little differnt but have not been able to see a fix.
I have opened the page in the stock browser and the latest release of Chrome and get the same error. Works on Android and PC.
dateString = "2013-08-06"
date = new Date(dateString);
I have tried this fix but same error
var arr = "2010-03-15 10:30:00".split(/[- :]/),
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
I just had an issue like this yesterday, but with internet explorer. I found that using a cross-browser date library like moment.js helped alleviate the issue:
var date = "2013-03-15 10:30:00";
date = moment(date, "YYYY-MM-DD HH:mm:ss").toDate();
Its just a wrapper around the date object, so the toDate() function returns its date object. If you want to take advantage of the formatting options moment provides, just remove the toDate().

Invalid date [dd/mm/yyyy] in safari? How to display in dd/mm/yyyy format using jquery

Safari browser gives a syntax error when i try to display the date format in
dd/mm/yyyy
format. Is there any solution how to display them using jquery? Thanks in advance
function parseDate(input) {
var d= new Date(input);
return d.format("dd-mm-yyyy");//"dd/mm/yyyy"
}//works on chrome not in safari
I've created a quick demo of your example code using the date format plugin, fiddle here and it appears to work fine in Safari.
Things to try:
Check the format of the date you're passing in to the input is a valid date object
Make sure that the javascript plugin is included in your page correctly, and that an older version is not being cached by Safari.
Failing that, please provide more information about the error Safari gives you.
UPDATE
As you've now said you're passing a string to the Date() object, I can see the problem. Safari is very strict about what it will accept, so you'll need to parse the string first, to form a valid Date object. Try the below:
//var d = new Date("2011-11-02"); // This will work for the vast majority of browsers - but not safari
function parseDate(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[0], parts[1]-1, parts[2]);
}
var d = parseDate("2011-11-02");
alert(d.format("dd-mm-yyyy"));
You can test it in this fiddle: http://jsfiddle.net/Ly4vb/1/

Categories