Passaro
Passaro

Reputation: 143

How to format a date that is a string in Luxon

given the following string: 2022-11-23, how can I use luxon to format it to be 23-11-2022

Upvotes: 1

Views: 2820

Answers (1)

VincenzoC
VincenzoC

Reputation: 31502

Using Luxon, you can parse your input string using fromISO (api-docs) and then format to your desired format using toFormat (api-docs) See Table of tokens to get details on how to match part of the datetime with its cooresponding token.

Example:

const DateTime = luxon.DateTime;
console.log(DateTime.fromISO("2022-11-23").toFormat("dd-MM-yyyy"));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>

In your case, you can also avoid using Luxon and set up some string manipulation like the following:

let parts = "2022-11-23".split('-');
console.log(parts[2] + '-' + parts[1] + '-' + parts[0]);

Upvotes: 2

Related Questions