How to convert string to timestamp and date in JS. here am using Date.parse(), but its not working in IE and FF. My code is..
in chrome its working fine.
var str = "05-Sep-2013 01:05:15 PM ";
console.log( Date.parse( str ) );
console.log( Date.parse( str.replace(/-/g, '/') ) ); // 1378404315000
in IE its returns
console.log( Date.parse( str.replace(/-/g, '/') ) ); // NaN
Please help me. thanks in advance.
How to convert string to timestamp and date in JS. here am using Date.parse(), but its not working in IE and FF. My code is..
in chrome its working fine.
var str = "05-Sep-2013 01:05:15 PM ";
console.log( Date.parse( str ) );
console.log( Date.parse( str.replace(/-/g, '/') ) ); // 1378404315000
in IE its returns
console.log( Date.parse( str.replace(/-/g, '/') ) ); // NaN
Please help me. thanks in advance.
Share Improve this question asked Aug 26, 2016 at 6:56 GVRGVR 1092 silver badges9 bronze badges 8- Help you how? What is your question? What do you want to have happen? – Makyen ♦ Commented Aug 26, 2016 at 6:58
- 3 because you can't just throw any old format that Chrome likes at IE and Firefox - Chrome handles dates differently (not more correct or less correct, just different) – Jaromanda X Commented Aug 26, 2016 at 6:59
- @Makye, I have to convert the string to date, for that am using Date.parse(). here am passing my string and converting to time stamp again am converting to date by using Date(). in IE am not able to parse the string. its returning NaN. please help me, thanks in advance. – GVR Commented Aug 26, 2016 at 7:04
-
That does not look like a Date in either a RFC2822 / IETF date syntax (RFC2822 Section 3.3) format, e.g. "Mon, 25 Dec 1995 13:30:00 GMT", or a ISO 8601 format e.g. "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time). Read:
Date.parse()
(Firefox) – Makyen ♦ Commented Aug 26, 2016 at 7:09 - From what source are you getting the date? – Makyen ♦ Commented Aug 26, 2016 at 7:09
3 Answers
Reset to default 7don't replace the '-' with '/', use whitespace instead.
var str = "05-Sep-2013 01:05:15 PM ";
console.log( Date.parse( str.replace(/-/g, ' ') ) );
that works for me in IE
have a look at w3schools - they're working with whitespaces :)
It is kind of odd, but a working solution for me is-
var str = "05-Sep-2013 01:05:15 PM ";
console.log( Date.parse( str.replace("-", " ") ) );
This format works with Chrome, Firefox and Safari:
const epochTime = Date.parse('2020/11/24 15:30')