阿拉伯语 JavaScript 中的日期时间

我的 JavaScript 函数有问题


var tDate = new Intl.DateTimeFormat("ar-US", { 

  day: 'numeric',

  month: 'long',

  year: 'numeric'

}).format(Date.now()) +

  '\xa0\xa0/ \xa0' +

  new Intl.DateTimeFormat("ar-FR-u-ca-islamic", {

  day: 'numeric', 

  month: 'long',

  year: 'numeric'

}).format(Date.now());


console.log(tDate);


阿拉伯语的输出是:4 April 2020 / 11 Sha'ban 1441 AH


in 英语 : April 5, 2020 / Shaʻban 12, 1441 AH


阿拉伯语有什么问题,4号移到左边为什么?


喵喵时光机
浏览 311回答 2
2回答

慕少森

使用 Intl 对象的日期格式输出在不同的实现中不一定一致。对我来说,OP 代码在不同的浏览器中会产生不同的结果:野生动物园:2020 年 4 月 6 日 / Shaban 13, 1441火狐:2020 年 4 月 6 日 / 13 沙班 1441 AH铬:2020 年 4 月 6 日 / 13 沙班 1441 AH它们在格式或字符上都不完全相同。如果您想确保组件按您想要的顺序排列,请使用formatToParts,收集零件然后按您想要的顺序输出它们。只要确保结果是明确的(例如,使用您所做的月份名称)。let partsHeg = new Intl.DateTimeFormat('ar-FR-u-ca-islamic', {  day: 'numeric',   month: 'long',  year: 'numeric'}).formatToParts(Date.now());partsHeg.forEach(part => {  if (part.type != 'literal') {    console.log(part.type + ': ' + part.value);  }});let partsGre = new Intl.DateTimeFormat('ar-US', {  day: 'numeric',   month: 'long',  year: 'numeric'}).formatToParts(Date.now());partsGre.forEach(part => {  if (part.type != 'literal') {    console.log(part.type + ': ' + part.value);  }});

慕慕森

您可以通过添加 RTL Unicode 的一 (1) 个语句/代码来修复此问题,以修复方向。当阿拉伯文本中的最后一个或第一个字母是拉丁字符时,就会发生这种情况。let RTL = "\u200F";       // Added ****var tDate = RTL + new Intl.DateTimeFormat("ar-US", {  // Added RTL before  day: 'numeric',  month: 'long',  year: 'numeric'}).format(Date.now()) +  '\xa0\xa0/ \xa0' +  new Intl.DateTimeFormat("ar-FR-u-ca-islamic", {  day: 'numeric',   month: 'long',  year: 'numeric'}).format(Date.now());console.log(tDate);                  // You can add RTL here instead console.log(RTL+tDate); 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript