Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format as MM/DD/YYYY using Intl.DateTimeFormat

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?

like image 607
Jane Fred Avatar asked Jul 16 '26 03:07

Jane Fred


1 Answers

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'
}
like image 57
user120242 Avatar answered Jul 18 '26 17:07

user120242



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!