Transform an array of dates containing time zones into Coordinated Universal Time (UTC) or Greenwich Mean Time (

So, I am facing an issue with a plugin that generates an array of dates without the ability to modify it. This plugin is embedded in the app's core and pulls data from a central database.

The problem lies in the fact that the array of dates includes the local timezone offset. Here is an example of how the elements look:

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)

When I pass this array to a web service using axios and do:

JSON.stringify(MyArray)

The values are converted to a text format with the timezone offset applied:

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

As a result, instead of displaying 00:00:00, it shows 23:00:00 and the day before the actual date due to the timezone offset.

Does anyone know of a workaround to convert the full array to GMT or make JSON.stringify ignore the timezone?

Answer №1

Imagine you have an array of dates coming in from JSON data:

// Here's a sample array from your JSON slice
[
    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)
]

If you need to convert these GMT dates into the appropriate format, like UTC, you can utilize the map function. Here's how:

// Assign the date array to a variable for easy reference
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)
]; 

// Use map to convert the dates to UTC
const utcDate = dateArray.map((gmtDateString) => {
    const gmtDateObj = new Date(gmtDateString);
    return gmtDateObj.toUTCString();
});  

// Now you can send the `utcDate` using Axios...
// Make sure to check if you need to call `JSON.stringify(utcDate);`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Injecting Dependencies Into ExpressJS Routes Middleware

Hey there! I'm currently working on injecting some dependencies into an expressjs route middleware. Usually, in your main application, you would typically do something like this: const express = require('express'); const userRouter = requi ...

The alpha value returned by gl.readPixels in Three.js will consistently be 255

Is there a way to retrieve pixel values from a threejs application? const gl = renderer.context; const currentFrame = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4); // read image data from gl gl.readPixels(0, 0, gl.drawingBufferWidth ...

Adjust the top position of a div element at regular intervals

How can I make the top position of an absolutely positioned div with children change every x seconds using jQuery? I tried using setInterval but it only changes once. Here is my code: .com_prices { width: 100%; height: 100%; overflow: hidden; back ...

Guide to implementing a datepicker on a webpage using Laravel

I am having trouble displaying the datepicker using the code below in my Laravel 4.2 application on Firefox. Is there a better way to display datepickers? Are there any CDNs available for this purpose? <table class="table table-striped"> <t ...

Obtaining the result from within the .then() block

Through the utilization of Google's API, I am successful in retrieving and displaying nearby places on the console. router.get('/', function (req, res, next) { // Locating nearby establishments googleMapsClient.placesNearby({ ...

Tips for formatting angular text sections

Within the scope of a controller, I have a status variable. After a successful rest PUT request, I add a message to this JavaScript variable. This message is displayed in my template using the {{status}} Is there a way to customize the styling of this mes ...

Tips for efficiently storing data in the Laravel database using Ajax

I'm attempting to upload a product with multiple images to the database without refreshing the page. Despite not encountering any errors in the console, I am seeing a long block of text that starts like this: <script> Sfdump = window.Sfdump || ...

Sending a function in React.js from one functional component to another

I'm facing an issue with my code where I have a function called postaviRutu in the App component that I need to pass to the child component Sidebar. When clicking on an element in Sidebar, I want it to call the postaviRutu function in App. I've a ...

"Utilizing the Ajax ScriptManager for Effective Namespace Management

After incorporating my Ajax webservice into a namespace (MyCompany.Web.MyService), I encountered an issue where the proxy in Javascript is being regenerated as MyCompany.Web.MyService. Is it possible to change the name of the Javascript proxy to just MySe ...

Top strategy for monitoring a user's advancement in reading different text segments

If you are familiar with zyBooks, I am looking to implement a similar feature where users can track the sections they have read and mark them as completed with a button. However, I am struggling conceptually with determining how best to structure my mongo ...

Add CSS styling to a section of a canvas

I want to use a specific cursor png for just a portion of a canvas. Currently, I have code that applies the cursor to the entire canvas like so: .myClass { cursor: url('../img/myCursor.png') 7 33, auto; /* img hotspot centred*/ } However, I ...

transmitting an array from JavaScript to PHP

Struggling with passing an array from JavaScript to PHP for a school assignment. I'm still learning and can't seem to figure out what's missing. Any help would be greatly appreciated. This is the code I've tried: if(bets.length > ...

How to retrieve a variable from a Multidimensional JSON decoded array?

I am currently attempting to determine which type of card the customer used for their transaction. To do this, I am testing the following code snippet: $request_body = '{"id":8799347,"order_id":"1854059","accepted":true,"type":"Payment","text_on_stat ...

How to dynamically load bootstrap-multiselect within a loop in a vueJs application

I am attempting to dynamically load bootstrap-multiselect in a loop using VueJs. My desired implementation looks something like this: <div class="col-xs-6 col-sm-4 mb-1" v-for="params in param"> <select class="mult" multiple="multiple"> ...

Store the result of the previous AJAX call in a jQuery variable and combine it with the data from the next AJAX response

I am working on a program where I retrieve price values using ajax. My goal is to add the previous price value to the current price value when it is retrieved again. The issue I am facing is that each time I get a new price value, it overrides the previou ...

Issues encountered with asp.net javascript function not executing

Having trouble getting a javascript function to execute via jquery in an asp.net setting. Despite trying various approaches, the function doesn't run upon clicking the button. I've experimented with omitting jquery and using a static html input c ...

Is it a mistake to gather errors together and send them to the promise resolve in JavaScript?

When processing a list in a loop that runs asynchronously and returns a promise, exiting processing on exception is not desired. Instead, the errors are aggregated and passed to the resolve callback in an outer finally block. I am curious if this approach ...

How to Implement HighCharts in AngularJS Using Json Data

Currently working on learning AngularJS and HighCharts. If you are interested, check out this [Plunker Link][1] I am trying to figure out how to dynamically update the x-axis and bar chart values using data from a JSON object. The y-axis values will remai ...

Use the on-screen keyboard to move around by using the arrow keys and choose a value from the list to

I am currently working on developing an on-screen keyboard that allows for navigation using arrow keys and selection with the enter key. My goal is to have each selected key append to an input field. Below is the HTML code I have implemented: <div id ...

Issue with saving cookie from Express.js backend to Nuxt.js frontend

After successfully creating an authorization server using Express.js, I encountered a problem when trying to save the access and rotating refresh tokens as signed cookies. The issue arose from having separate backend and frontend servers with different dom ...