Reputation: 25
I'm working with an XML data that gives all information for my vue app. Everything works fine except one value.
I need to get the date from an element, but it comes in this format :
['2022-10-25']
It should be 25-10-2022
I get this item using :
item.datum.join().toString()
How can I change it ?
Since it's an Object, I can not find a solution for this, better would be to handle it as data, I have MomentJS installed but I also did not get it to work.
Upvotes: 0
Views: 111
Reputation: 4380
An easy way to to this is split
the string by the character "-"
to an array which we reverse
and join
it togheter with "-"
.
Or use moment if you have it installed already.
// 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("-"));
// with 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>
Upvotes: 3
Reputation: 211
Can't you simply reverse the string?
const dates = ['2022-10-25']
const desiredDate = dates[0].split('-').reverse().join('-')
Upvotes: 3