Reputation: 109
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.
Upvotes: 4
Views: 7845
Reputation: 1758
This format works with Chrome, Firefox and Safari:
const epochTime = Date.parse('2020/11/24 15:30')
Upvotes: 0
Reputation: 794
don'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 :)
Upvotes: 7
Reputation: 14598
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("-", " ") ) );
Upvotes: 0