为了账号安全,请及时绑定邮箱和手机立即绑定

阿拉伯语 JavaScript 中的日期时间

阿拉伯语 JavaScript 中的日期时间

喵喵时光机 2022-06-16 10:04:47
我的 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 AHin 英语 : April 5, 2020 / Shaʻban 12, 1441 AH阿拉伯语有什么问题,4号移到左边为什么?
查看完整描述

2 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

使用 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);

  }

});


查看完整回答
反对 回复 2022-06-16
?
慕慕森

TA贡献1856条经验 获得超17个赞

您可以通过添加 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); 


查看完整回答
反对 回复 2022-06-16
  • 2 回答
  • 0 关注
  • 310 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号