You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript将孟加拉国日期时间转换为孟加拉语?

实现孟加拉语时间格式转换(含孟加拉历)

要将给定的时间字符串转换为目标孟加拉语格式,你可以利用JavaScript的Intl.DateTimeFormatAPI,它原生支持本地化日期时间格式化,包括孟加拉语和孟加拉历(Bengali Calendar)。以下是完整实现步骤:

步骤1:解析输入时间字符串

首先将输入的时间字符串转换为Date对象:

const inputTime = "Sat Feb 10 2024 15:33:51 GMT+0600 (Bangladesh Standard Time)";
const date = new Date(inputTime);

步骤2:定义格式化器

分别创建公历、孟加拉历、时间的格式化器,指定孟加拉语本地化(bn-BD)和孟加拉时区(Asia/Dhaka):

// 公历格式化器(提取星期、日、月、年)
const gregorianFormat = new Intl.DateTimeFormat('bn-BD', {
  weekday: 'long',
  day: 'numeric',
  month: 'long',
  year: 'numeric',
  timeZone: 'Asia/Dhaka'
});

// 孟加拉历格式化器(提取孟加拉历的日、月、年)
const bengaliFormat = new Intl.DateTimeFormat('bn-BD', {
  day: 'numeric',
  month: 'long',
  year: 'numeric',
  calendar: 'bengali',
  timeZone: 'Asia/Dhaka'
});

// 12小时制时间格式化器(提取时、分、上午/下午)
const timeFormat = new Intl.DateTimeFormat('bn-BD', {
  hour: '2-digit',
  minute: '2-digit',
  hour12: true,
  timeZone: 'Asia/Dhaka'
});

步骤3:提取各部分内容

使用formatToParts()方法拆分格式化后的内容,方便单独获取每个部分:

// 提取公历部分
const gregorianParts = gregorianFormat.formatToParts(date);
const weekday = gregorianParts.find(p => p.type === 'weekday').value;
const gregDay = gregorianParts.find(p => p.type === 'day').value;
const gregMonth = gregorianParts.find(p => p.type === 'month').value;
const gregYear = gregorianParts.find(p => p.type === 'year').value;

// 提取孟加拉历部分
const bengaliParts = bengaliFormat.formatToParts(date);
const bengDay = bengaliParts.find(p => p.type === 'day').value;
const bengMonth = bengaliParts.find(p => p.type === 'month').value;
const bengYear = bengaliParts.find(p => p.type === 'year').value;

// 提取时间部分
const timeParts = timeFormat.formatToParts(date);
const hour = timeParts.find(p => p.type === 'hour').value;
const minute = timeParts.find(p => p.type === 'minute').value;
const period = timeParts.find(p => p.type === 'dayPeriod').value; // 输出পূর্বাহ্ন/অপরাহ্ন

步骤4:拼接成目标格式

将所有提取的部分按示例格式拼接:

const finalOutput = `সময়: ${hour}:${minute}, ${weekday}, ${gregDay} ${gregMonth}, ${gregYear} ${bengDay} ${bengMonth} ${bengYear} বঙ্গাব্দ, ${hour}:${minute} ${period}`;
console.log(finalOutput);

说明

  • Intl.DateTimeFormat会自动将阿拉伯数字转换为孟加拉数字(如2024→২০২৪),无需额外处理。
  • 指定timeZone: 'Asia/Dhaka'确保时区正确,避免因本地时区差异导致的错误。
  • calendar: 'bengali'是获取孟加拉历的关键参数,现代主流浏览器均支持该选项。

内容的提问来源于stack exchange,提问作者anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 12:40:54