Changing an ISO date object for a Date object - JavaScript

I am stuck in a strange situation and, unfortunately, even after doing some RnD and googling, I cannot solve this problem.

I have a date string in ISO format, for example 2014-11-03T19:38:34.203Z , and I want to convert it to a date object using the new Date() method.

But when I do this, the conclusion 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 I went through is 3 Nov,2014 , and the exit is 4 Nov,2014 , and this is due to GMT +5.30 of our local time (IST).

So, is there any general method with which I can get a date object that returns the date Nov 3,2014 .

NOTE I have no problem with timestamp. We can change the temporary string to zero using the setHours() method. The only thing I want is a date object like new Date() having a date of 3 Nov,2014 .

+6
source share
3 answers

Not passing strings to the Date constructor, as you know, is bad when parsing strings. IE 8, for example, will not parse ISO 8601 format strings and return NaN. It’s very 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 time zone is UTC +0530, then the time in this time zone is 01:08:34 in the morning the next day, therefore, the difference in dates. For example, for a person on the east coast of Australia, but not observing daylight saving time (i.e. UTC +10), this is equivalent to:

 4 November, 2014 05:38:34 

Edit

So, if you want to return it to the ISO date, you can use the getISO * methods to create any format that suits, for example

 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 toISOString :

  parseISOString('2014-11-03T19:38:34.203Z').toISOString(); // 2014-11-03T19:38:34.203Z 

Simple polyfill for ES5 preview 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'; } } 
+3
source

Well, it depends on what you want to do with the object later. You can always refer to the "UTC" javascript date functions.

Check link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

+1
source

You can use "getUTCDate ()" to get the actual date.

 var d = new Date('2014-11-03T19:38:34.203Z'); var n = d.getUTCDate(); 

But it will only return the date. get the month "getUTCMonth ()" and get the year "getUTCFullYear ()". Then build everything in your format. for instance

 var n=[]; var d = new Date('2014-11-03T19:38:34.203Z'); var s = d.getUTCDate(); n.push(s); s=d.getUTCMonth(); n.push(s); s=d.getUTCFullYear(); n.push(s); 

finally make "n" as an object.

All the best

+1
source

Source: https://habr.com/ru/post/978383/


All Articles