最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date format as MMDDYYYY using Intl.DateTimeFormat - Stack Overflow

programmeradmin2浏览0评论

I want the Date format as MM/DD/YYYY and I am using Intl.DateTimeFormat

console.log(new Intl.DateTimeFormat('en-US').format(date));

output: "6/20/2012"

The output I need is "06/20/2012"

What change should I do to get the expected result?

I want the Date format as MM/DD/YYYY and I am using Intl.DateTimeFormat

console.log(new Intl.DateTimeFormat('en-US').format(date));

output: "6/20/2012"

The output I need is "06/20/2012"

What change should I do to get the expected result?

Share Improve this question asked Jun 26, 2020 at 8:39 Jane FredJane Fred 1,4898 gold badges26 silver badges49 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 20

Provide the options object:

console.log(new Intl.DateTimeFormat('en-US',{month:'2-digit',day:'2-digit', year:'numeric'}).format(new Date()));

Helpful Cheatsheet
Available Options:

{
  weekday: 'narrow' | 'short' | 'long',
  era: 'narrow' | 'short' | 'long',
  year: 'numeric' | '2-digit',
  month: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long',
  day: 'numeric' | '2-digit',
  hour: 'numeric' | '2-digit',
  minute: 'numeric' | '2-digit',
  second: 'numeric' | '2-digit',
  timeZoneName: 'short' | 'long',

  // Time zone to express it in
  timeZone: 'Asia/Shanghai',
  // Force 12-hour or 24-hour
  hour12: true | false,

  // Rarely-used options
  hourCycle: 'h11' | 'h12' | 'h23' | 'h24',
  formatMatcher: 'basic' | 'best fit'
}
let today = new Date().toISOString().split('T')[0];
发布评论

评论列表(0)

  1. 暂无评论