我正在尝试获取希伯来日历日期,以便我可以根据当前希伯来日期创建一个函数。 像这样的事情
if (monthName == "Tishrei") {
$('.Tishrei').css('display', 'block')
这样我就可以根据今天的日期来控制网页上显示的内容。
我试图像这样获取月份的数字
var monthNumber = new Intl.DateTimeFormat('en-u-ca-hebrew', { month: 'narrow' }).format(new Date());
它正确地返回月份数字,但是当我尝试在 switch 语句中使用它时它不起作用,它只是返回默认值。这是我的 switch 语句:
switch (monthNumber) {
case 1:
monthName = "Tishrei"
break;
我也考虑过使用月份的名称 -
var monthNumber = new Intl.DateTimeFormat('en-u-ca-hebrew', { month: 'narrow' }).format(new Date());
但是用英语书写的希伯来语单词的拼写各不相同,我不知道在这种情况下使用哪种拼写。
有什么想法吗?
最佳答案
…but the spelling of Hebrew words written in English varies and I don't know which spelling is used in this case.
Intl 对象应使用 CLDR 项目中的数据,因此所有浏览器中的英文名称应相同。
以下函数使用Intl.DateTimeFormat获取希伯来月份名称。 Safari、Firefox、Chrome 和 Opera 返回相同的列表。也许 Edge 是不同的。
function getHebrewMonthNames() {
let d = new Date(2022,0);
let months = {};
let fNum = new Intl.DateTimeFormat('en-u-ca-hebrew', {month: 'numeric'});
let fName = new Intl.DateTimeFormat('en-u-ca-hebrew', {month: 'long'});
for (let i=1; i<366; i++) {
d.setDate(i);
if (!months[fNum.format(d)]) {
months[fNum.format(d)] = fName.format(d);
}
}
return months;
}
console.log(getHebrewMonthNames());
/* {
"1": "Tishri",
"2": "Heshvan",
"3": "Kislev",
"4": "Tevet",
"5": "Shevat",
"6": "Adar",
"7": "Adar II",
"8": "Nisan",
"9": "Iyar",
"10": "Sivan",
"11": "Tamuz",
"12": "Av",
"13": "Elul"
} */
根据列表获取当前月份名称的简单函数是:
function getHebrewMonthName(d = new Date()) {
return [,"Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "Adar", "Adar II", "Nisan",
"Iyar", "Sivan", "Tamuz", "Av", "Elul"]
[d.toLocaleString('en-u-ca-hebrew', {month: 'numeric'})];
}
console.log('Current Hebrew month from list: ' +
getHebrewMonthName()
);
console.log('Current Hebrew month from Intl: ' +
new Date().toLocaleString('en-u-ca-hebrew', {month: 'long'})
);
关于javascript - 使用 JS 获取希伯来历日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74791361/