0

OK.. So I have this plugin that generates for me an array of dates. The problem is I cannot modify this plugin as it's embedded in the apps core and it takes data from a central database.

So here's the issue, the array of dates has the local timezone offset applied, so basically elements look like this:

0: Thu Apr 11 2019 00:00:00 GMT+0100 (hora de verano de Europa occidental)
1: Fri Apr 12 2019 00:00:00 GMT+0100 (hora de verano de Europa occidental)

I need to pass this array to a web service (using axios), so when I do

JSON.stringify(MyArray)

The value is converted to a text format time with the timezone offset applied:

"2019-04-10T23:00:00.000Z"

As you can see, instead of showing 00:00:00 it shows 23:00:00 and the day before the actual date, as the timezone offset is applied

Is there any workaround? maybe something to convert the full array to GMT or make JSON.stringify to ignore the timezone??

Intervalia
  • 10,248
  • 2
  • 30
  • 60
Jack Casas
  • 914
  • 18
  • 37

1 Answers1

0

So; let's say you have this Array of dates coming in from JSON:

// WE ASSUME THIS IS A SAMPLE ARRAY FROM A SLICE OF YOUR JSON DATA
[
    Thu Apr 11 2019 00:00:00 GMT+0100 (hora de verano de Europa occidental),
    Fri Apr 12 2019 00:00:00 GMT+0100 (hora de verano de Europa occidental)
]

You may want to run the Array into a map Function and within the map Function, convert the GMT Dates into the appropriate Date (say UTC or whatsoever). Here's how:

// FIRST ASSIGN THE DATE ARRAY TO A VARIABLE SO YOU CAN REFERENCE IT EASILY.
const dateArray =  [
    Thu Apr 11 2019 00:00:00 GMT+0100 (hora de verano de Europa occidental),
    Fri Apr 12 2019 00:00:00 GMT+0100 (hora de verano de Europa occidental)
]; // THIS WOULD IMPLY WHATSOEVER VARIABLE HOLDS YOUR DATE ARRAY....

// TO CONVERT THE DATE TO SAY UTC OR SO, YOU COULD DO THAT WITHIN MAP LIKE SO
const utcDate  = dateArray.map((gmtDateString) => {
    const gmtDateObj  = new Date(gmtDateString);
    return gmtDateObj.toUTCString();
});  

// NOW, YOU CAN SHIP THE `utcDate` WITH AXIOS...
// YOU MAY HAVE TO CHECK IF YOU NEED TO STILL CALL `JSON.stringify(utcDate);`
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • I think this process does the same as JSON.Stringify. My output is a GMT time, but with the offset applied Wed, 10 Apr 2019 23:00:00 GMT – Jack Casas Apr 04 '19 at 08:52
  • So, how would you like your Date to be formatted in the end? Do you want a Date in the Format `YYYY-MM-DD HH:II:SS` ie `2019-04-11 21:30:15`? Or rather; could you (please) **specify** the **END DATE-FORMAT** you are aiming for? – Poiz Apr 05 '19 at 05:20
  • End date Format I'm aiming for: ```Thu Apr 11 2019 00:00:00 GMT``` I just want to remove the time zone offset, but keep the original format – Jack Casas Apr 08 '19 at 10:51