如何从瞬间转换为卢克森,以进行两字母的一周格式?

发布于 2025-01-27 16:56:18 字数 490 浏览 5 评论 0 原文

我正在将打字稿应用程序转换为使用Luxon而不是用于DateTime处理的时刻,并且不确定如何使用Luxon的内置功能(或可配置的选项)作为两个字母返回一周中的一天。

片刻: momment()。格式('mm/dd/y')应返回 '04/tu/2022'

卢克森: dateTime.now()。toformat('mm/ccc/yyyy'),但这给了我 '04/tue/2022',这不适合所需的后端数据格式。

我可以设置一个选项参数来指定一周​​中返回的字母数量吗?还是另一种方法?

这是我发现的一个示例,可让您使用选项指定2位数量的日子和月份...

dateTime.now()。tolocalestring({day:'二维',月份:'2位数','2位数',年:'numeric'}) => 05/10/2022

I am converting a typescript app to use Luxon instead of Moment for datetime processing and am not sure how to use Luxon's built-in features (or configurable options) to return the day of week as two letters.

Moment:
moment().format('MM/dd/y') should return '04/Tu/2022'.

Luxon:
DateTime.now().toFormat('MM/ccc/yyyy') but that gives me '04/Tue/2022', which does not fit the required backend data format.

Is there an options parameter I can set to specify the number of letters to return for the day of week string? Or another approach?

This is an example that I found that allows you to specify 2 digit day and month using options...

DateTime.now().toLocaleString({ day: '2-digit', month: '2-digit', year: 'numeric' }) => 05/10/2022

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2025-02-03 16:56:18

我担心与Luxon,令牌表仅列出 ccc (每周的日,作为缩写本地化字符串), cccc (一周中的一天,一天中的一天)作为一个毫不掩饰的本地化字符串), ccccc (每周的一天,作为单个局部字母),它绘制可能的值('nrarch''short ''long' intl.datetimeformat option

可能的解决方法是使用自定义功能,并仅获得一周中一天的前两个数字。

const DateTime = luxon.DateTime;

function customFormat(dt) {
  const year = dt.year;
  const month = dt.toFormat('MM');
  const dow = dt.toFormat('ccc').substring(0, 2);
  return month + '/' + dow + '/' + year;
}

console.log(customFormat(DateTime.now()));
console.log(customFormat(DateTime.fromISO('2022-05-11')));
console.log(customFormat(DateTime.fromISO('2022-05-10')));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>

I fear that this is not possible "natively" with Luxon, Table of tokens lists only ccc (day of the week, as an abbreviate localized string), cccc (day of the week, as an unabbreviated localized string), ccccc (day of the week, as a single localized letter) that maps exactly the possible values ('narrow', 'short', 'long') of weekday key of the Intl.DateTimeFormat option object.

Possible workaround is to use custom function and get only first two digit of day of the week.

const DateTime = luxon.DateTime;

function customFormat(dt) {
  const year = dt.year;
  const month = dt.toFormat('MM');
  const dow = dt.toFormat('ccc').substring(0, 2);
  return month + '/' + dow + '/' + year;
}

console.log(customFormat(DateTime.now()));
console.log(customFormat(DateTime.fromISO('2022-05-11')));
console.log(customFormat(DateTime.fromISO('2022-05-10')));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文