"Can you help me figure out how to transform a date string into a concise date object using JavaScript

As a newcomer to UI coding, I am faced with the challenge of working with date data from JSON in the format "2021-02-28 00:00:00". However, when writing to an xlsx file, I need the date to be in a different format rather than as a string. Here is my attempted solution:

variable = new Date("2021-02-28 00:00:00")

This results in the date object appearing as follows:

Sun Feb 28 2021 00:00:00 GMT+0530 (India Standard Time)

But what I really want is the date to be displayed in the following format [still as a date object and not a string]:

28 Feb 2021 00:00:00

Answer №1

There is a way to achieve this using the Date object, but it involves writing multiple lines of code.

Personally, I prefer to handle all my date formatting tasks with moment.js.

For example: moment('2021-02-28 00:00:00').format('DD MMM YYYY hh:mm:ss')

console.log(moment('2021-02-28 00:00:00').format('DD MMM YYYY HH:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

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

Solving the Issue of Assigning a Random Background Color to a Dynamically Created Button from a Selection of Colors

Trying to create my own personal website through Kirby CMS has been both challenging and rewarding. One of the features I'm working on is a navigation menu that dynamically adds buttons for new pages added to the site. What I really want is for each b ...

Using async/await in combination with Vuex and Feathers

Please review my code below. I am attempting to integrate Async Await into vuex. While everything is functioning properly, I would like to call another action after this one using async await. However, the expected result is not being achieved as the conso ...

Durable Container for input and select fields

I need a solution for creating persistent placeholders in input and select boxes. For instance, <input type="text" placeholder="Enter First Name:" /> When the user focuses on the input box and enters their name, let's say "John", I want the pl ...

Modifying properties of an array of objects in React Native using JavaScript

I have a scenario where I am using Flatlist to render a couple of boxes. If the "shapes" element's "visible" key is false, the box will be blank. This visibility property is defined in state and I'm not sure if this is the correct approach. Now, ...

What are some ways I can showcase the outcomes of my multiple choice quiz?

I need help with my multiple choice quiz. I want to display the answers under each question and highlight the correct one in bold. Here is the current code: function check() { var question1 = document.quiz.question1.value; var question2 = document.q ...

Flickering transitions in Phonegap and jQuery Mobile pages

As a beginner in Phonegap/jQuery Mobile, I have encountered a frustrating issue of a white screen appearing during page transitions. Despite trying various solutions found online, such as using -webkit-backface-visibility:hidden;, the problem persists. I ...

Filtering JSON array is just as simple as working with XML

During my project, I have transitioned from using XML format to JSON format. The XML structure includes nodes such as: <Creature> <Name>Someone</Name> <Desert>false</Desert> <Woods>false</Woods> < ...

Enhancing code readability in vim with custom Javascript syntax highlighting

Is anyone else experiencing issues with VIM's syntax highlighting for Javascript? I have noticed that at times, the highlighting seems to disappear and I have to scroll around to get it adjusted properly. Does anyone know of any workarounds or soluti ...

The sequence of elements within a React.addons.createFragment object

Currently, I am exploring the documentation on creating fragments in React from https://facebook.github.io/react/docs/create-fragment.html. It appears that the engineers at Facebook place a significant emphasis on the object memory layout, specifically the ...

How can one access a client instance that has been generated using the $create method in ASP.NET AJAX?

I've utilized the client-side ASP.NET AJAX library to create a client component instance using the $create shortcut method. The object is linked to a DOM element, but I'm struggling to find a way to reference the instance since it's not regi ...

Tips for reintroducing a previously deleted shape into the canvas using Konva

I recently removed a rectangle from the scene using the remove() method. Now, I am trying to figure out how to draw it back. According to the documentation: "remove a node from parent, but don't destroy. You can reuse the node later." Here is the li ...

Sending data to server with Backbone (using save() function and php script)

Wondering if you can assist me with an issue I'm facing. I am trying to utilize Backbone's save() method and send data to a php file. However, I encountered a problem right at the beginning. When I execute the following in the browser's co ...

File resolution issue with TypeScript

While I am aware that using TypeScript outFile is generally advised against, I currently have no choice but to utilize it until a more suitable solution like AMD can be implemented. It seems like there may be a "module splitting issue" in my project. In t ...

Recursively apply a custom condition to filter a tree

Within this tree structure, I need to extract items with a release version of 0.0.1 to use in building my navigation based on that specific release number. { title: '', items: [ { title: '', path: '', ...

Passing resolved data from a parent state to a nested view in Angular UI Router

Currently, I am developing an Angular web application that will showcase various financial events for the user. These events are categorized under a single ID and grouped into different categories. Each group needs to have its own HTML <table> for di ...

Can you explain the distinction between Array() and [] in Javascript, and when would it be preferable to use one over the other?

Similar Question: Understanding the difference between "new Array()" and "[]" in JavaScript array declaration When working with JavaScript, you have the option to create a new array using: var arr = new Array(); or simply using: var arr2 = []; Wha ...

Is it possible for a font to exclude the space character?

I'm currently developing a font detection library that must be extremely compact in size, perfect for direct inclusion on every page of a website. I've managed to shrink it down quite a bit already (compressed to 417 bytes). Take a look at it on ...

Tips for disabling default browser input validation in React

https://i.stack.imgur.com/834LB.png Is there a way to remove the message "Please fill out this field" while still keeping the "required" attribute intact? I have my own validation system in place, so I need to use the "required" attribute to determine whe ...

Struggling to find the element using Selenium

Hey there, I'm struggling with identifying the input id button in my HTML code while using Selenium through Java. The error message says it's unable to locate the element. Can anyone provide some guidance? I've already tried using xpath and ...

Converting JS carousel to TS for showcasing multiple items

I am currently working on integrating a bootstrap carousel into my Angular project and I need to convert my JavaScript file to a TypeScript file. As someone who is new to this, I'm unsure of the process for converting and implementing it in a .ts file ...