Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  170] [ 1]  / answers: 1 / hits: 74158  / 11 Years ago, wed, march 20, 2013, 12:00:00

I have a ISO date string as below



 var startTimeISOString = 2013-03-10T02:00:00Z;


when I convert it to date object in javascript using below code, it returns



var startTimeDate = new Date(startTimeISOString);


output is



Date {Sun Mar 10 2013 07:30:00 GMT+0530 (India Standard Time)}


It sure converts the ISOString to date but it converts to local time since new Date() is client dependent. How to just convert iso date time string to date and time but not to local date-time..?



Thanks


More From » javascript

 Answers
39

According to MDN:




Differences in assumed time zone



Given a date string of March 7, 2014, parse() assumes a local time
zone, but given an ISO format such as 2014-03-07 it will assume a
time zone of UTC. Therefore Date objects produced using those strings
will represent different moments in time unless the system is set with
a local time zone of UTC. This means that two date strings that appear
equivalent may result in two different values depending on the format
of the string that is being converted (this behavior is changed in
ECMAScript ed 6 so that both will be treated as local).




I have done like this and am now getting the exact time which is inside the ISO date string instead of the local time



 var startTimeISOString = 2013-03-10T02:00:00Z;

var startTime = new Date(startTimeISOString );
startTime = new Date( startTime.getTime() + ( startTime.getTimezoneOffset() * 60000 ) );


This will give the same date time inside iso date string , the output here is



o/p



Date {Sun Mar 10 2013 02:00:00 GMT+0530 (India Standard Time)}

[#79473] Tuesday, March 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kyrona

Total Points: 422
Total Questions: 111
Total Answers: 97

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;