0

After doing a lot of searching, I am finally writing this.

I am trying to send a ISO-8601 formatted string from my front end to backend node rest API. So the thing is I want to parse the sent string into a UTC datetime object for further use and persisting in a database.

The question is how do I parse a ISO-8601 string correctly to a UTC datetime object ignoring the client timezone and server timezone with UTC.

I have tried this but its showing me the following -

enter image description here

// String sent from client side
const iso8601 = new Date().toISOString();

const nums = iso8601.match(/\d+/g).map(Number);
const utcDate = new Date(nums[0], --nums[1], nums[2], nums[3], nums[4], nums[5], nums[6]);

console.log(utcDate);
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
  • https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc – developer Apr 07 '20 at 14:03
  • *iso8601* is UTC. You are seeing a timezone in the output because *console.log* is calling the default [*toString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) which formats the date as local. But the underlying Date object is still UTC. So just send *iso8601* to the back end. – RobG Apr 07 '20 at 21:58
  • @RobG so how do we parse back in the backend ? – Kunal Mukherjee Apr 08 '20 at 04:40

0 Answers0