当前位置: 首页 > news >正文

JS 实现Date日期格式的本地化

为了更好的更新多语言日期的显示,所以希望实现日期的本地化格式显示要求,常规的特殊字符型格式化无法满足显示要求,这里整理了几种我思考实现的本地化实现功能。

通过多方查找,总结了实现的思路主要有如下三个方向:

  • 官方基础支持:javascript自支持Intl.DateTimeFormat实现本地化
  • 三方工具:如dayjs使用‘dayjs/plugin/localeData
  • 自己实现

DateTimeFormat实现本地化

JavaScript已经提供了可以使用的本地化功能:Intl.DateTimeFormat,只需要传入当前语言和日期基本可以完成本地化的输出,如下给出一些基础的实现:

const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));// 假定下面输出的结果使用了洛杉矶时区(UTC-0800,太平洋标准时间)// 美式英语 (US English) 使用  month-day-year 格式
console.log(new Intl.DateTimeFormat("en-US").format(date));
// "12/19/2012"// 英式英语 (British English) 使用 day-month-year 格式
console.log(new Intl.DateTimeFormat("en-GB").format(date));
// "19/12/2012"// 韩国使用 year-month-day 格式
console.log(new Intl.DateTimeFormat("ko-KR").format(date));
// "2012. 12. 19."// 大部分阿拉伯国家使用阿拉伯字母 (real Arabic digits)
console.log(new Intl.DateTimeFormat("ar-EG").format(date));
// "١٩‏/١٢‏/٢٠١٢"// 在日本,应用可能想要使用日本日历,
// 2012 年是平成 24 年(平成是是日本天皇明仁的年号,由 1989 年 1 月 8 日起开始计算直至现在)
console.log(new Intl.DateTimeFormat("ja-JP-u-ca-japanese").format(date));
// "24/12/19"// 当请求可能不支持的语言,如巴厘语(ban)时,若同时指定了备用的语言,
// 那么将使用备用的语言输出(本例为印尼语(id))
console.log(new Intl.DateTimeFormat(["ban", "id"]).format(date));
// "19/12/2012"

同时,提供给我们使用options进行格式化的返回,这个很大程度已经足够使用了:

const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));// 请求参数 (options) 中包含参数星期 (weekday),并且该参数的值为长类型 (long)
let options = {weekday: "long",year: "numeric",month: "long",day: "numeric",
};
console.log(new Intl.DateTimeFormat("de-DE", options).format(date));
// "Donnerstag, 20. Dezember 2012"// 应用可能需要使用世界标准时间 (UTC),并且 UTC 使用短名字 (short) 展示
options.timeZone = "UTC";
options.timeZoneName = "short";
console.log(new Intl.DateTimeFormat("en-US", options).format(date));
// "Thursday, December 20, 2012, GMT"// 有时需要更精确的选项
options = {hour: "numeric",minute: "numeric",second: "numeric",timeZone: "Australia/Sydney",timeZoneName: "short",
};
console.log(new Intl.DateTimeFormat("en-AU", options).format(date));
// "2:00:00 pm AEDT"// 再精确些...
options.fractionalSecondDigits = 3; // 秒数的有效数字数量
console.log(new Intl.DateTimeFormat("en-AU", options).format(date));
// "2:00:00.200 pm AEDT"// 即便是美国,有时也需要使用 24 小时制
options = {year: "numeric",month: "numeric",day: "numeric",hour: "numeric",minute: "numeric",second: "numeric",hour12: false,timeZone: "America/Los_Angeles",
};
console.log(new Intl.DateTimeFormat("en-US", options).format(date));
// "12/19/2012, 19:00:00"// 要使用选项,但是需要使用浏览器的默认区域,请使用 'default'
console.log(new Intl.DateTimeFormat("default", options).format(date));
// "12/19/2012, 19:00:00"
// 有时需要包含一天的时段
options = { hour: "numeric", dayPeriod: "short" };
console.log(new Intl.DateTimeFormat("en-US", options).format(date));
// 10 at night

将其封装成方法如下:

function formatLocalTime(date) {const options = {year: 'numeric',month: 'long',}// 我这里将lang写在html标签进行全局切换const formatter = new Intl.DateTimeFormat(document.documentElement.lang, options)return formatter.format(date)
}
const  date = new Date()
formatLocalTime(date) // 2024年3月

三方库提供的本地化

其他的日期库没了解,这里介绍dayjs库使用的本地化操作,dayjs自生无法直接进行本地化,是需要通过插件dayjs/plugin/localeData来配合实现的。

1、安装

$ npm install dayjs 
$ npm install dayjs/plugin/localeData

2、使用

// 引入 dayjs 和 locale 插件
const dayjs = require('dayjs');
const localeData = require('dayjs/plugin/localeData');
const zh = require('dayjs/locale/zh-cn'); // 需要加载对应的语言包dayjs.extend(localeData);
dayjs.locale(zh);const date = dayjs('2023-01-01');
console.log(date.format('MMMM D, YYYY')); // 一月 1, 2023

自己封装

原理比较简单,我们通过解析Date数据格式,使用国际化插件配置对应的本地化数据进行格式化填充即可,原理很简单,但有点费时费力,如果实在无法实现的时间格式可以考虑自己封装实现,具体实现不提供了。

相关文章:

  • Halcon 多相机统一坐标系
  • 2024年6月四六级考试复盘
  • 【Python】PySide6使用入门和注意事项
  • springboot整合sentinel接口熔断
  • 在线兴趣教学类线上学习APP应用开发部署程序组建研发团队需要准备什么?
  • js如何添加新元素到数组中
  • pytest中失败用例重跑
  • A5M2报错【列 pd.adsrc 不存在】
  • Python+Selenium自动化测试环境搭建步骤(selenium环境搭建)
  • 企业微信发送消息
  • 基于Python的花卉识别分类系统【W9】
  • 高考没考好焦虑怎么选计算机专业!一篇告诉你,推荐三个风口专业!想学计算机怎么选大学专业
  • 【DPDK学习路径】九、学习分支
  • Python爬虫实现“自动重试”机制的方法(1)
  • Linux,shell ,gun基本概念和关系
  • 收藏网友的 源程序下载网
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • 【RocksDB】TransactionDB源码分析
  • CSS魔法堂:Absolute Positioning就这个样
  • HTML5新特性总结
  • interface和setter,getter
  • LintCode 31. partitionArray 数组划分
  • MySQL QA
  • orm2 中文文档 3.1 模型属性
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • session共享问题解决方案
  • Spring Boot快速入门(一):Hello Spring Boot
  • Spring-boot 启动时碰到的错误
  • Spring核心 Bean的高级装配
  • SwizzleMethod 黑魔法
  • Vim 折腾记
  • 电商搜索引擎的架构设计和性能优化
  • 对象管理器(defineProperty)学习笔记
  • 关于使用markdown的方法(引自CSDN教程)
  • 后端_ThinkPHP5
  • 解析带emoji和链接的聊天系统消息
  • 开源SQL-on-Hadoop系统一览
  • 聊聊sentinel的DegradeSlot
  • 前端面试题总结
  • 使用common-codec进行md5加密
  • 算法系列——算法入门之递归分而治之思想的实现
  • 微信支付JSAPI,实测!终极方案
  • 中文输入法与React文本输入框的问题与解决方案
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • (javascript)再说document.body.scrollTop的使用问题
  • (二)Eureka服务搭建,服务注册,服务发现
  • (三)终结任务
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)EOS中账户、钱包和密钥的关系
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • .NET/C# 中设置当发生某个特定异常时进入断点(不借助 Visual Studio 的纯代码实现)