gpt4 book ai didi

javascript - 时刻获取在语言环境中格式化的日期

转载 作者:行者123 更新时间:2023-12-03 10:01:15 25 4
gpt4 key购买 nike

从那一刻起,文档将格式设置为 l以“M/D/YYYY”格式返回。
有没有办法在 D/M 中获取日期?或 M/D格式取决于语言环境?这是我现在拥有的代码。

var locale = window.navigator.userLanguage || window.navigator.language;
moment.locale(locale);
var momentTime = moment(d);
console.log(momentTime.format('l'));

例如,如果语言环境是 French那么日期应该返回 D/M格式,如果语言环境是 english-us那么日期应该返回 M/D自动格式化。

最佳答案

一种满足您需要的方法是本地化 longDateFormat然后用正则表达式删除年份部分。

这是一个使用 localeData 的工作示例然后 longDateFormat .我不确定它是否适用于每个语言环境,但它为 fr 提供了正确的结果和 en-us (可能还有许多其他人)。

var locale = window.navigator.userLanguage || window.navigator.language;
moment.locale(locale);

// Get locale data
var localeData = moment.localeData();
var format = localeData.longDateFormat('L')
// Remove year part
format = format.replace(/.YYYY/, '');

var momentTime = moment();
console.log(momentTime.format(format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>

关于javascript - 时刻获取在语言环境中格式化的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41750489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com