[Javascript] Natively Format JavaScript Dates and Times
JavaScript and format
2023-09-14 08:59:13 时间
The ability to natively format dates and times using the browser is impressive. You may find yourself not needed to grab libraries such as moment.js or date-fns when doing simple formatting and better yet, the browser support is surprisingly good!
const date = new Date(); console.log(date.toLocaleDateString()); console.log(date.toLocaleDateString("en-US")); console.log(date.toLocaleDateString("fr-FR")); const dateOptions = { weekday: "long", year: "numeric", month: "long", day: "numeric" }; console.log(date.toLocaleDateString("en-US", dateOptions)); console.log( date.toLocaleDateString("en-US", { month: "short", day: "numeric" }) ); console.log( date.toLocaleDateString("fr-FR", { month: "long" }) ); console.log(date.toLocaleTimeString()); console.log(date.toLocaleTimeString("en-US")); console.log(date.toLocaleTimeString("de-DE")); const timeOptions = { hour12: true, hour: "numeric", minute: "2-digit", second: "2-digit", timeZone: "America/Los_Angeles" }; console.log(date.toLocaleTimeString("en-US", timeOptions)); console.log( date.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit" }) ); console.log( date.toLocaleTimeString("en-US", { hour12: false }) ); console.log( date.toLocaleString("en-US", { ...dateOptions, ...timeOptions }) ); const dateTimeFormat = new Intl.DateTimeFormat("en-US", { ...dateOptions, ...timeOptions }); console.log(dateTimeFormat.format(date)); const anotherDate = new Date("2000-12-25T12:34:56.789Z"); console.log(dateTimeFormat.format(anotherDate));
相关文章
- 第一百五十节,封装库--JavaScript,表单验证--密码验证
- JavaScript经典代码【一】【javascript HTML控件获取值】
- JavaScript Window Location 当前页面的地址
- [Javascript] Understanding the difference between .prototype and .__proto__ in JavaScript
- [Javascript] Create an Image with JavaScript Using Fetch and URL.createObjectURL
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part3
- [Javascript] Understanding the .constructor property on JavaScript Objects
- [Poi] Build and Analyze Your JavaScript Bundles with Poi
- [Javascript] Identify and Deal with NaN in JavaScript
- [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
- [Javascript] Linting JavaScript with ESLint
- [Javascript] Private, public variables and method in Name Space
- 换个思路理解Javascript中的this
- [Javascript] HTML5 DOM project
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [HTML5] Add an SVG Image to a Webpage and Get a Reference to the Internal Elements in JavaScript
- [Javascript] Understanding the .constructor property on JavaScript Objects
- [Javascript] Iterate Over Items with JavaScript's for-of Loop
- [Javascript] Web APIs: Persisting browser data with window.localStorage
- JavaScript事件处理的例子:事件捕捉和冒泡 - event capture and bubble
- 使用JavaScript Function.prototype进行代码重构的一些例子
- JavaScript and Ruby in ABAP
- JavaScript 函数调用时带括号和不带括号的区别
- JavaScript练习:显示一个钟表,当下的时间
- 生日祝福(HTML+CSS+JavaScript+jQuery)
- 【华为OD机试 2023】最左侧冗余覆盖子串(C++ Java JavaScript Python)
- 【华为OD机试 2023】星际篮球争霸赛(C++ Java JavaScript Python)
- JavaScript实现禁用键盘和鼠标的点击事件
- 《Javascript权威指南》学习笔记之十二:数组、多维数组和符合数组(哈希映射)