I encountered an issue where the date was incorrect after using moment.js for conversion

Can someone assist me with this issue?

console.log(moment(new Date('2016-05-24T18:50:05.000')).format('LL'));

The desired output is 24 May 2016, however, the code returns 25 May 2016.

I would appreciate any help provided.

Answer №1

To ensure the correct date format, it is essential to specify the date as UTC.

This can be achieved in two ways:

console.log(moment(new Date('2016-05-24T18:50:05.000Z')).format('LL'));

By utilizing the 'Z', which signifies that the time is in UTC. Alternatively:

console.log(moment.utc(new Date('2016-05-24T18:50:05.000')).format('LL'));


Another method is passing an ISO 8601 date string directly to moment.

It is not necessary to enclose the string within a javascript Date object. In this case, your code will appear as:

console.log(moment('2016-05-24T18:50:05.000Z').format('LL'));

Alternatively:

console.log(moment.utc('2016-05-24T18:50:05.000').format('LL'));

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

Comes equipped with functionality for array within an array

I have an array of arrays. m = [[-100,100], [-100,-100], [-100,-100], [-100,100]] Removing duplicates in this array has proven tricky for me. I attempted to use: aa = [...new Set(m)]; // and let chkdup = (arr, tar) => tar.every(v => arr.includes ...

Learn how to fetch data using JavaScript and store it in an array, then display the data in a Bootstrap card using inner

I am working with a Fetch method that looks like this: async function sendApiRequest() { let APP_ID = "f61bb958"; let APP_Key = "7c465e19d8e2cb3bc8f79e7a6e18961e" let INPUT_VALUE = document.getElementById("inputRecipe&q ...

Angulajrs - instructions for activating Bootstrap tabs

I'm trying to set the first tab as active in my modal every time it is opened. The issue I'm facing is that if I switch to the second tab, close the modal, and reopen it, the second tab remains active. How can I ensure that the first tab is alway ...

Tips on resolving the error message "Property ... is not present on type 'IntrinsicAttributes & ...' in NextJS"

In my nextjs application, I have a Navbar component that accepts menu items as props: <Navbar navitems={navigationItems} /> The navigationItems prop is an array of objects. Within the Navbar component, I have defined the following: export interface ...

Is there a way for me to access, edit, and update the user.json file?

I recently launched a next.js application and included a member registration feature that responds to requests from the front-end using next.js API routes. While testing it locally, the membership registration was successful. However, after deploying it to ...

Issue with Local Storage: Value not being saved, instead [object MouseEvent] being stored

I am truly grateful for the help from @zim as it allowed me to drastically simplify my code for 2 buttons that store true/false values locally. However, I am facing an issue where the button click is registering as [object MouseEvent] instead of True/False ...

The server is returning an unauthorized status when making a post request to the node, yet the client's fetch

While attempting to retrieve data through a fetch request on the front-end, I successfully obtained the type and id values as intended. export const getUserProfile = () => { return ( fetch( "https://api.spotify.com/v1/me", { headers: ...

The React live search functionality is operational, however, it is not effectively canceling previous requests in the

Currently, I am in the process of following a helpful tutorial over at "alligator.io". You can check out the specific link here: https://alligator.io/react/live-search-with-axios/ The code snippet below belongs to App.js: import React, { Component } from ...

Ways to modify babel output file extensions

I'm facing an issue where babel --out-file-extension is not working as expected. Below is my package.json : { "name": "Assets", "version": "1.0.0", "description": "", "main" ...

Using Ajax with the submitHandler function in jQuery Validation Plugin

Currently, I'm working with the jQuery validation plugin in conjunction with ASP.NET MVC. I am trying to utilize $.Ajax() to submit a form, but I'm encountering an issue where the entire section of code I have written seems to be getting skipped ...

The error message "Unexpected style type encountered while loading model in ifc.js" was displayed

When I try to load IFC files using the ifc.js library, I frequently encounter numerous messages like the following in the console: Unexpected style type: 3800577675 at 213152 (web-ifc-api.js: 933) Unexpected style type: 3800577675 at 213511 (web-ifc-api.js ...

The function "getWatchLength" is being called in the render process without being defined on the instance. Remember to define reactive data properties in the data option of Vue.js

I have a Vuex store that holds the state of the number of watched products. I am able to retrieve the product length from my store, but every time I attempt to {{getWatchlength}} in my code, I receive an error stating that "getWatchlength" is not defined ...

Tips for adjusting column sizes in ag-grid

I'm a beginner with ag-grid and need some help. In the screenshot provided, I have 4 columns initially. However, once I remove column 3 (test3), there is empty space on the right indicating that a column is missing. How can I make sure that when a col ...

What is the best way to consolidate multiple JS files into one main file that I can easily reference in my HTML code?

I've dedicated countless hours to watching videos and compiling a comprehensive library of assets, similar to p5.js (I'm especially fond of Dan Shiffman's content). I now have numerous files for various functions - init.js, dom.js, drawing.j ...

Is it possible to establish the page state upon loading (in the context of a GET request

At present, my navigation bar is set up to utilize AJAX for loading new pages and window.pushState() in order to update the URL when the AJAX call is successful. I've come to realize that it's crucial to push the page state during GET requests s ...

Getting the x-auth token from a response header using React - a step-by-step guide

Encountered a new issue - the server passes my token in the response header as 'x-auth' https://i.sstatic.net/HNo09.png Seems like React is unable to access this value using response.headers.x-auth If I do console.log(response.headers), it doe ...

Transform a string into a property of a JSON object

One of my challenges involves extracting a value from a JSON object called Task, which contains various properties. Along with this, I have a string assigned to var customId = Custom_x005f_f3e0e66125c74ee785e8ec6965446416". My goal is to retrieve the value ...

Unveiling the power of Axios and Vue in fetching API data: The quest for

I've encountered a problem while trying to integrate my API with Vue/Axios. The issue arises when I attempt to store the data retrieved by Axios into an empty variable within the data object of my component. It throws an "undefined at eval" error. Can ...

What is the best way to limit the character count to a maximum of 5000 in a Quill editor using AngularJS?

I need assistance with my HTML code. How can I restrict the user to a maximum of 5000 characters for input? <div class="ql-container ql-bubble" id="richTextQuel"> <ng-quill-editor theme="bubble" id="richTextEditor" placeholder="Inclusions" ng ...

Issue with Date generation in TypeScript class causing incorrect date output

I have a simple code snippet where I am creating a new Date object: var currentDate = new Date(); After running this code, the output value is: Sat May 11 2019 13:52:10 GMT-0400 (Eastern Daylight Time) {} ...