libai8723 / front-end-dev-notes-bignerdbook

Github Pages
https://libai8723.github.io/front-end-dev-notes-bignerdbook/
1 stars 0 forks source link

前端的Date Time Format真TM是地狱 #74

Open libai8723 opened 1 year ago

libai8723 commented 1 year ago
const date = new Date();
// Results below assume UTC timezone - your results may vary

// Specify default date formatting for language (locale)
console.log(new Intl.DateTimeFormat('en-US').format(date));
// Expected output: "12/20/2020"

// Specify default date formatting for language with a fallback language (in this case Indonesian)
console.log(new Intl.DateTimeFormat(['ban', 'id']).format(date));
// Expected output: "20/12/2020"

// Specify date and time format using "style" options (i.e. full, long, medium, short)
console.log(new Intl.DateTimeFormat('zh-CN', { 
    year: "2-digit",
  month: "2-digit",
  day: "2-digit",
  hour: "numeric",
  minute: "numeric",
  second: "numeric",
  hour12: false, timeZone: 'Asia/Shanghai' }
                                   ).format(date));
// Expected output: "Sunday, 20 December 2020 at 14:23:16 GMT+11"

console.log(new Intl.DateTimeFormat('zh-CN', { 
    year: "2-digit",
  month: "2-digit",
  day: "2-digit",
  hour: "numeric",
  minute: "numeric",
  second: "numeric",
  hour12: false, timeZone: 'Asia/Shanghai' }
                                   ).formatToParts(date));
libai8723 commented 1 year ago

还不如使用前端的moment.js

var now = moment().format('YYYY-MM-DD HH:mm:ss dddd');
alert(now);
// 2023-04-25 22:46:14 Tuesday

https://momentjs.com/docs/#/displaying/format/