0

Hi i have a date that arrive with this format 2020-05-25T20:11:38Z, and i need to convert to 2020-05-25T21:11:38+01:00. In my project is not installed moment.js is a big project, and the masters don't use it. is there some where to make this change?

I have the timeZone for every zone. I know that there is options like this getTimezoneOffset(); And i did find in stackoverflow, but i didn't find any response in javascript to change zulu to utc with offset.

Thanks for your indications

The Fool
  • 16,715
  • 5
  • 52
  • 86
ddaudiosolutions
  • 131
  • 2
  • 15
  • I answered a similar question only a few hour ago. You can convert toISOString, replace the Z with +01:00 and make a new date – mplungjan Jan 22 '22 at 19:26
  • `let date = new Date("2020-05-25T20:11:38Z"); const isoString = date.toISOString(); const ZPlus1 = isoString.replace("Z","+0100"); date = new Date(ZPlus1);` – mplungjan Jan 22 '22 at 19:28
  • but if you need that the +01 will be +03 according to the timezone. how can you achieve? thanks for your aproach. is very usefull – ddaudiosolutions Jan 22 '22 at 19:56
  • See the other dupe - enough tools there – mplungjan Jan 22 '22 at 20:02

1 Answers1

0

The format "2020-05-25T20:11:38Z" is a common standard ISO 8601 format, it is also produced by the default Date.prototype.toString method, however it's only with a UTC (+0) offset.

The above ISO 8601 format is reliably parsed by reasonably current built–in parsers (some very old implementations won't parse it correctly), so to get a Date object:

let date = new Date('2020-05-25T20:11:38Z');

Formatting it for a fixed +1 offset can done by adjusting the Date for the offset then formatting it as required by leveraging the default toISOString method, e.g.

// Initial timestamp
let s = '2020-05-25T20:11:38Z'

// Convert s to a Date
let d = new Date(s);
// Show that it's the same date
console.log(`Initial value: ${s}\n` +
            `Parsed value : ${d.toISOString()}`);

// Create a new date with 1 hour added as 3,600,000 milliseconds
let e = new Date(d.getTime() + 3.6e6);

// Format and manually modify the offset part
let timestamp = e.toISOString().replace('Z','+01:00');
console.log(`Adjusted timestamp: ${timestamp}`);

// Parse back to date
console.log(`Parsed to a Date  : ${new Date(timestamp).toISOString()}`);

The resulting timestamp can be parsed back to a Date that represents the same instant in time as the original string (last line).

Note that the adjusted Date is only created for the sake of formatting the timestamp, it shouldn't be used for anything else.

If, on the other hand, you want a general function to format dates as ISO 8601 with the local offset, there is likely an answer at Javascript date format like ISO but local that suits. If so, then this is a duplicate, e.g. this answer or this one.

Also, there are a number of libraries that will allow specifying the formatting and timezone as separate parameters, so consider using one if you're going to do a lot of date formatting or manipulation.

RobG
  • 142,382
  • 31
  • 172
  • 209