I am new to javascript
and have the following string '2023-06-19 05:00:00.554'
and want to convert it to epoch i am trying something like this
var s = "2023-06-19 05:00:00.554";
var epoch = new Date(s).getTime();
console.log(epoch);
but it give wrong results any idea how to achieve this ?
2
Answers
As explained by Ricky Mo you must specify the timezone value of your date,
otherwise the system will use the host system’s timezone at this date (with STD or DST calculation).
2 ways
Assuming you have string
"2023-06-19 05:00:00.554"
and it represents GMT, you need to first convert that to a ISO 8601 format string, so that it can be converted to a Date object reliably, regardless of the browser used. The ISO format of your input is"2023-06-19T05:00:00.554Z"
Code using a regex to convert your input to the ISO format: