I need to find out the time in milliseconds (epoch number) for my date string, however it doesn't take into consideration the timezone offset.
var dateTimeString = "2022-08-12T06:06:52.237+10:00";
var controlDateTimeString = "2022-08-12T06:06:52.237";
var epochNumber = new Date(dateTimeString).getTime();
var controlEpochNumber = new Date(controlDateTimeString).getTime();
console.log(epochNumber,controlEpochNumber,epochNumber === controlEpochNumber );
//OUTPUT
1660248412237 1660248412237 true
So when my code reads it it is always off by 10 hours as it doesn't seem to take it into account.
What do i need to do to take into account the timezone.
This is because, UNIX timestamps are the absolute time since January 1st, 1970 at UTC -- which is constant (by design) no matter the timezone.
It would therefore not make any sense to manipulate the number such that is was 10 hours ahead, since that would no longer be a unix timestamp. You probably want to do whatever you need without actually using a unix epoch as an intermediary.
It really depends what your use case is to know what to do next. Let me know in comments.
Based on the question Comments I believe your intent is that controlDateTimeString should be in the UTC (GMT) time zone. In the new Date(dateString) constructor used on line 5 you do not specify a time zone. ISO 8601 does not require a time zone designator to be used in combined date and time representations. However, the ECMAScript Standard does require a time zone to be specified. MDN notes that not conforming to "ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ)" my result in "parsing behavior ... implementation-defined and may not work across all browsers". It appears that not specifying a time zone results in all browsers we've tested interpreting the dateString as if it is in the local time zone of the executing computer.
Given the above, chances are appending a Z to the controlDateTimeString String literal will get the code to behave as you would like it do regardless of the current time zone of the computer on which the code is executed.
var dateTimeString = "2022-08-12T06:06:52.237+10:00";
var controlDateTimeString = "2022-08-12T06:06:52.237Z";
var epochNumber = new Date(dateTimeString).getTime();
var controlEpochNumber = new Date(controlDateTimeString).getTime();
console.log(epochNumber,controlEpochNumber,epochNumber === controlEpochNumber );
results in the output:
//OUTPUT
1660248412237 1660284412237 false
on my computer (GMT-4) for a difference of 36000000 ms = 10 hours, which is both the difference between UTC and UTC+10 and, I believe, what you want the code to do.
This code time zone specified in controlDateTimeString (as Z) should run the same on any computer in any time zone.