Reorder the position of an item within an array

I have an XML data source that provides all the information needed for my Vue app. Everything is working smoothly except for one specific value.

The issue lies in retrieving a date from an element, which is currently formatted as:

['2022-10-25']

However, I actually need it to be displayed as 25-10-2022.

Currently, I am using the following code snippet to retrieve the date:

item.datum.join().toString()

Is there a way to manipulate this date format? I have tried using MomentJS since it's already installed, but haven't been successful in making it work.

Answer №1

To achieve this, one simple approach is to use the split function on the string with the delimiter being "-". After splitting, you can then reverse the array and finally join it back together using the same delimiter "-".

Alternatively, if you have Moment.js installed in your project, you can utilize that for date formatting.

// result step 1
console.log('2022-10-25'.split("-"));
// result step 2
console.log('2022-10-25'.split("-").reverse());
// final result
console.log('2022-10-25'.split("-").reverse().join("-"));
// using moment.js
console.log(moment('2022-10-25').format("DD-MM-YYYY"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

Answer №2

Wouldn't it be possible to just flip the string around?

let names = ['John-Doe']
let desiredName = names[0].split('-').reverse().join('-')

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

Is Vue.JS compatible with AJAX requests for http calls?

I am currently attempting to achieve the following in my HTML: var app = new Vue({ el: '#loginContent', data: { main_message: 'Welcome', isAuthenticated: false, loginErrorMessage: '', ...

How can you use ng-click to re-sort data that has already been loaded using Angular's ng-click?

I'm having trouble switching between loading and sorting the information in the table using ng-click. The functions to load and sort work correctly individually, but I can't seem to switch between the two. It seems like I reset the countries data ...

The true essence of Angular values only comes to light once the view has been updated

Here is the HTML code I am working with : <div class="container-fluid"> <div class="jumbotron" id="welcomehead"> <br><br><br><br><br><br><br><br><br><br> ...

Interpret a variety of date formats using date-fns

Here is a code snippet that I am working with: function checkDate(value: string) { return isBefore( parse(value, 'dd-MM-yyyy', new Date()), sub(new Date(), { days: 1 }) ); } const result = checkDate('31-12-2020'); In the p ...

Utilizing Vue Js and Query to retrieve data from a Jira Rest API endpoint

Trying to utilize the JIRA REST API within a Vue.js application has presented some challenges. After generating the API key, I successfully ran an API request using Postman. Initially, I attempted to use the axios client, but encountered issues with cross ...

"Create a dynamic entrance and exit effect with Tailwind CSS sliding in and out from

My goal is to create a smooth sliding animation for this div that displays details of a clicked project, transitioning in and out from the right side. This is my attempt using Tailwind CSS: {selectedProject !== null && ( <div classNam ...

What is the process for refreshing HTML elements that have been generated using information from a CSV document?

My elements are dynamically generated from a live CSV file that updates every 1 minute. I'm aiming to manage these elements in the following way: Remove items no longer present in the CSV file Add new items that have appeared in the CSV file Maintai ...

Is it possible to transform a Vuejs project into Vue-Native?

I recently completed a Vue.js project and now I'm interested in turning it into a native app. I'm wondering if I'll need to completely rewrite the application using Vue-Native components, or if there is a way to convert my existing project i ...

The received URL from the POST request in React is now being opened

After completing an API call, I successfully received the correct response in my console. Is there a way to redirect my React app from the local host to the URL provided (in this case, the one labeled GatewayUrl under data)? Any assistance would be greatly ...

Guide on transmitting Cookie from Server to User's Browser

I'm currently exploring the functionality of cookies and how they work. I'm attempting to send a cookie from the server and set it in the user's browser. However, when I check within the developer tools--> application-->cookie, I can&a ...

PHP - organizing an array into sets

My task involves organizing an array of filenames: Array ( [2] => 1_1_page2-img1.jpg [3] => 1_2_page2-img1-big.jpg [4] => 2_1_page2-img1.jpg [5] => 2_2_page2-img1-big.jpg [6] => 3_1_page2-img1.jpg [7] => 4_1_page ...

What is causing the issue with $(document).append() method in jQuery version 1.9.1?

Why is the following code not functioning properly in jQuery 1.9.1? It worked fine in previous versions. $(function () { $(document).append(test); document.write('done'); }); var test = { version: "1.0", }; JSFiddle: http://jsfiddl ...

Looking for assistance with transferring API information to components

I am currently working on a basic Movie finder application that will display a list of movies containing the name entered by the user. My focus at this stage is to just show the information on the screen. As I'm using React for this project, I have t ...

Implementing a progress loader in percentage by simultaneously running an asynchronous setTimeout counter alongside a Promise.all() operation

I'm encountering a problem running an asynchronous setTimeout counter simultaneously with a Promise.all() handler to display a progress loader in percentage. Here are the specifics: I've developed a Vue application comprised of three components ...

Unable to implement the hover property for the entire class in CSS

I am struggling to highlight the entire class element on hover. Even though I have specified the button class as a selector, only the background color of the anchor tag is changing when I hover over the button. What could be causing this issue and how can ...

What are some solutions to troubleshoot the issue with my select dropdown menu?

I'm having an issue with my football team members array. I've categorized the players by position, and when I select a specific position (e.g., striker), only those players are displayed. However, when I press the 'secin' option again, ...

Adjusting the size of the parent element for a ThreeJS renderer

Is there a way to display a fixed 550 x 500 container inside a table for a 3D object without changing the parent container's size when calling container.appendChild(renderer.domElement);? Any suggestions on how to resolve this issue? HTML: <t ...

Struggling to retrieve data from Firebase in React Native?

It's been a challenge for me as a newcomer to React Native trying to retrieve data from a Firebase database. This is the process flow of how my data is handled: 1. A user selects locations and trip details (name, startDate, endDate) --> stored in ...

Searching for related values in a nested array of objects using JavaScript: A guide

My goal for this project is to thoroughly examine the path along with all nested elements within the Items and assign them to the details variable. However, due to the limitations of the function inside the useEffect, I am unable to check nested items eff ...

What methods can be used to identify if a browser is mobile and adjust the size of a video accordingly using JavaScript conditions?

My goal is to incorporate JavaScript and jQuery into my HTML page in order to detect if visitors are using mobile browsers, and display a video at different sizes based on this information. I have experimented with various methods, but none of them have p ...