如何让toLocaleString()输出贾拉利历的英文阿拉伯数字?
贾拉利历转换为英文阿拉伯数字格式的实现方案
你可以通过在toLocaleString中添加numberingSystem选项,指定使用拉丁数字系统(latn),来得到英文阿拉伯数字格式的贾拉利历结果:
document.write(new Date().toLocaleString("fa-IR", { numberingSystem: "latn" }));
这段代码会输出类似1401/12/22, 11:42:00的结果——既保留了贾拉利历的日期计算,又用英文数字展示数值。
如果需要更精准地控制日期时间的格式(比如强制两位数的月、日、时分秒),可以使用Intl.DateTimeFormat来定制:
const jalaliFormatter = new Intl.DateTimeFormat("fa-IR", { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", numberingSystem: "latn" }); document.write(jalaliFormatter.format(new Date()));
原理说明
默认情况下,fa-IR区域设置会使用波斯数字系统(编号为arab),所以显式指定numberingSystem: "latn"就能切换到我们需要的英文阿拉伯数字格式,同时不影响贾拉利历的日期转换逻辑。
内容的提问来源于stack exchange,提问作者Ahmad Beyranvand
相关产品推荐
相关产品推荐

