我们如何在 Luxon 中获得 Moment Do 格式

问题描述 投票:0回答:1

时刻 => 第一、第二

看不到 luxon 不提供这种格式。有什么建议或发现吗?

datetime momentjs luxon
1个回答
0
投票

luxon 对此没有内置支持,但您可以编写如下所示的自定义逻辑。

// 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>

© www.soinside.com 2019 - 2024. All rights reserved.