I need to convert a Json date to a date object, but the server's timezone is causing the time to change. I need to ensure the date and time zone remain consistent

this.convertJSON_DateTimeTo_datetimeObj = function (jsonDate) {
            var date_dd_MM_yyyy = $filter('date')(jsonDate.slice(6, -2), 'medium');
            //var dt = new Date(date_dd_MM_yyyy).toLocaleString("en-US", { timeZone: "Asia/Kolkata" });
            //var dt = new Date(date_dd_MM_yyyy.split("/").reverse().join("/"));
            return date_dd_MM_yyyy;
        },

Input Parameter jsonDate = "/Date(1629810881857)/"

In the above code snippet, I am trying to convert a JSON DateTime into a DateTime Object with the output timezone set to "Asia/Kolkata". The code in the comment section shows what I have already attempted. Any help on this matter would be greatly appreciated. Thank you.

Answer №1

One issue that needs addressing is the lack of timezone information being sent by the server in the /Date(1629810881857)/ string. This presents a challenge when trying to convert it to Asia/Kolkata timezone without knowing the original timezone. It's crucial for the server to include this information in its response.

Once this hurdle is cleared, the next step is to correctly parse the date string into a JavaScript Date. The use of the $filter service may not be necessary in this context. You can find the recommended method for parsing dates here.

Additionally, it is generally advisable to allow JavaScript dates to adjust to the end user's local timezone based on their browser/OS settings instead of enforcing a specific timezone like Asia/Kolkata. However, if you prefer consistent display in a particular timezone regardless of the user's location, a helpful explanation can be found here.

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

What is the best way to incorporate an AppBar featuring a Back to Top button from Material UI for React into my application?

While exploring the Material UI documentation, I came across this interesting code snippet: import React from 'react'; import PropTypes from 'prop-types'; import AppBar from '@material-ui/core/AppBar'; import Toolbar from &ap ...

trouble encountered while sending data to php using ajax and json

Despite trying multiple solutions, my script is still not working properly. It functions without the $_POST value and without JSON, but when both are included, it fails to work. The error message displayed by the function is as follows: <b>Notice< ...

Utilizing Regex to eliminate consecutive duplicates in a string separated by spaces

My current challenge involves removing only sequential duplicates from a string. For example, in the string "1 2 3 3 2 1", I want to remove one of the consecutive 3's so it becomes "1 2 3 2 1". I thought I had the solution figured out, but when I test ...

Patience is key as you wait for the observable to finish

My methods have dependencies where one method needs to complete before the next can be called. process1(data: string) : Observable<string> { this.dataservice.process(data).subscribe( (response) => { return response. ...

The button fails to log any text to the developer console

Attempting to verify the functionality of my button by logging a message on the developer console. However, upon clicking the button, the text does not appear in the console. import { Component, EventEmitter, Input, Output } from '@angular/core'; ...

How to efficiently export modules and handle return values in Node.js using async functions

In my current project, I am extracting data from a CSV file and performing some operations on it to create an object with the derived information. This function is implemented using module.exports for exporting. var csvtojson = require('csvtojson&apo ...

Creating a visually appealing multi-bar chart in AngularJS: Tips for effectively presenting data

Imagine that I have the JSON data below: [ { month: "Jan", cost: 80, energy: 90 }, { month: "Feb", cost: 50, energy: 80 }, { month: ...

Open the window by using the `Window.open` method, then find and

Is there a way to open a document, not in a new window but on the same page using JavaScript.Window.Open()? Could it be possible to display the document within a specific div element that is obtained with getElementById? For example: document.getElementB ...

Different ways to notify a React/Next.js page that Dark Mode has been switched?

I am in the process of creating my first basic Next.js and Tailwind app. The app includes a fixed header and a sidebar with a button to toggle between dark and light modes. To achieve this, I'm utilizing next-theme along with Tailwind, which has been ...

The constructor for audio in Next JS cannot be found

I'm facing an issue and struggling to find a solution. My project follows the standard structure of Next JS. In the root directory, I have included a components folder. Within the components folder, there is a component with the following code: imp ...

What could be causing my dropdown menu to not appear when clicked?

I am trying to implement the functionality shown in the image. When the Settings button is clicked, a window should open allowing the user to navigate to their desired option. However, I am facing an issue where nothing happens when the button is clicked. ...

Vue plugins that emit events

In the scenario where I have a basic Vue plugin that does not contain any components, but simply provides some methods to the user: export default { install(Vue, options) { // Unrelated tasks go here. } Vue.prototype.$foo = () => { ...

I'm looking to utilize the Blueimp File Uploader to upload just one file

I'm trying to upload only one file using Blueimp File Upload. However, whenever I select a second file after uploading the first one, the new file gets appended below the old one instead of replacing it. How can I modify the code to replace the existi ...

Angular's counterpart to withCredentials in jQuery AJAX

Currently, I am using Angular to make an HTTP GET request to my local CouchDB. The code snippet below demonstrates how it is done with AngularJS: var req = { method: 'POST', url: BASEURL+'_session', headers: { &apos ...

What steps are needed to pass an additional parameter to the onMouseDown function in ReactJS?

The TypeScript function listed below demonstrates the functionality: const handleMouseDownHome = (e: any) => { // Only activates when scroll mouse button is clicked if (e.button === 1) { window.open("/", "_blank", " ...

How can I access internal.Writable within the basic-ftp NodeJS module using TypeScript?

After examining the API documentation of the basic-ftp module, I came across the following: downloadTo(writableStream | localPath, remotePath, startAt = 0): Promise<FTPResponse> However, when utilizing the module in a TypeScript project, the method ...

`Where can I locate a grid example?`

Right here: Upon reading the documentation, I discovered: onItemInserting has the following arguments: { grid // represents the grid instance item // item being inserted } In my software application there are multi ...

Spinning cubemap texture in Three.js

I've created this cubetexture in Three.js. Now, I want to rotate the cubeTexture itself by 180 degrees, not the camera. Is there a way to achieve this? Specifically, I aim to rotate the x axis of the cubeTexture to display the opposite side. It would ...

Mobile devices do not support CSS3 transition functionality

I have a straightforward div setup that smoothly transitions upward when swiped up on the screen, functioning perfectly on my desktop. However, I am encountering difficulties getting the transition to work on my mobile device (also tested on an Android emu ...

Unable to utilize JavaScript objects extracted from JSON

Working on developing a web application using AngularJS has led me to encounter an issue. I have a PHP server that retrieves data from an SQL database and encodes it into JSON. Utilizing the Angular $http service on the client side, I am able to successful ...