I am stuck in a weird situation and unfortunately, even after doing some RnD and googling, I am unable to solve this problem.
I have a date string in ISO format, like 2014-11-03T19:38:34.203Z
and i want to convert it into a date object with new Date()
method.
But when i do so, output is:
var isoDate = '2014-11-03T19:38:34.203Z'; console.log(new Date(isoDate)); //output is: Tue Nov 04 2014 01:08:34 GMT+0530 (IST)
The date which i passed is of 3 Nov,2014
and the output is 4 Nov,2014
and it's because of GMT +5.30 of our local time(IST).
So, is there any generic method with which i can get the date
object which return the date of Nov 3,2014
.
NOTE: I don't have any issues with timestamp. We can change time string to zero with setHours()
method. The only thing which i want is date
object like new Date()
having date of 3 Nov,2014
.
The ISO date format For example, "3rd of April 2002", in this international format is written: 2002-04-03 .
The date. toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ).
Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.
Do not pass strings to the Date constructor, it is notoriously bad at parsing strings. IE 8, for one, will not parse ISO 8601 format strings at all and return NaN. It's really simple to write your own parser:
function parseISOString(s) { var b = s.split(/\D+/); return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6])); }
Note also that if the time is 19:38:34.203 UTC and your timezone is UTC +0530, then the time in that timezone is 01:08:34 am on the following day, hence the difference in dates. For example, for a person on the east coast of Australia but not observing daylight saving (i.e. UTC +10), it's equivalent to:
4 November, 2014 05:38:34
So if you want to return it to an ISO date, you can use the getISO* methods to create whatever format that suits, e.g.
function isoFormatDMY(d) { function pad(n) {return (n<10? '0' : '') + n} return pad(d.getUTCDate()) + '/' + pad(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear(); } var s = '2014-11-03T19:38:34.203Z'; var date = parseISOString(s); console.log(isoFormatDMY(date)) // 03/11/2014
or use ES5's toISOString:
parseISOString('2014-11-03T19:38:34.203Z').toISOString(); // 2014-11-03T19:38:34.203Z
A simple polyfill for pre ES5 browsers:
if (!Date.prototype.toISOString) { Date.prototype.toISOString = function() { var d = this; // Padding functions function pad(n) {return (n<10? '0' : '') + n} function padd(n){return (n<100? '0' : '') + pad(n)} return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + '.' + padd(d.getMilliseconds()) + 'Z'; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With