Ho do we get Moment Do format in Luxon

749 views Asked by At

Moment Do => 1st, 2nd

cant see luxon doesn't provide this format. any suggestion or findings?

1

There are 1 answers

0
Sahil Patel On

There is no in-built support in luxon for that, but you can write your custom logic something like below.

// Import the DateTime class from Luxon
const DateTime = luxon.DateTime;

// Function to add ordinal suffix to day number
function addOrdinal(number) {
  const suffixes = ['th', 'st', 'nd', 'rd'];
  const v = number % 100;
  return number + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);
}

const date = DateTime.local(2023, 1, 1);

// Format the date as "1st Jan"
const formattedDate = `${addOrdinal(date.day)} ${date.toFormat('LLL')}`;

console.log(formattedDate); // Output: 1st Jan
<script src="https://unpkg.com/[email protected]/build/global/luxon.js"></script>