回答

收藏

控制 JavaScript 中的日期格式

技术问答 技术问答 269 人阅读 | 0 人回复 | 2023-09-12

为什么下面的分钟和秒不打印两位数?, |$ k2 K- ^2 S& |' \+ u- Y- E
Intl.DateTimeFormat的文档。
6 ?; h4 f2 z: v2 e. B+ ^请注意,添加hour12: false小时、分钟或秒格式化程序的选项没有明显效果。
# j) q4 W/ @5 E$ \& i  }{ second: '2-digit' }).format(d)const format = (d) =>  `${ye(d)}/${mo(d)}/${da(d)} ${ho(d)}{mi(d)}{se(d)}`console.log(format(new Date('2019-01-01T00:00:00Z)code]                { second: '2-digit' }).format(d)const format = (d) =>  `${ye(d)}/${mo(d)}/${da(d)} ${ho(d)}{mi(d)}{se(d)}`console.log(format(new Date('2019-01-01T00:00:00Z'))) // 2019/01/01 00:0:0[/code]               
8 W) g! F4 C  f" c! q3 i3 }5 Q- E    解决方案:                                                               
  j. `! \( l" k, o8 j. t1 s; r                                                                只需使用formatToParts,因为这是一个已知的问题。
4 q; C0 u" m- h" E
    const DTF = new Intl.DateTimeFormat('in',{ year: 'numeric',month: '2-digit',day: '2-digit',hour: '2-digit',minute: '2-digit',second: '2-digit' })const dp = DTF.formatToParts(new Date('2019-01-01T01:02:03Z'))console.log(dp)console.log(`${dp[4].value}/${dp[2].value}/${dp[0].value} ${dp[6].value}{dp[8].value}{dp[10].value}`)/ w5 f7 [, j3 x( b8 m6 Y
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则