

H = hours am/pm hh = 2-digit hours am/pm H = hours HH = 2-digit hours
SETDATE JS FORMAT FULL
M = digit month MM = 2-digit month MMM = short month name MMMM = full month nameĮEEE = full weekday name EEE = short weekday name Try the code out by clicking "Run code snippet." ( formatDate is implemented in the code snippet below) See code for more examples.įormatDate(new Date(), 'EEEE, MMMM d, yyyy HH:mm:ss:S') Using similar formatting as Moment.js, Class DateTimeFormatter (Java), and Class SimpleDateFormat (Java), I implemented a comprehensive solution formatDate(date, patternStr) where the code is easy to read and modify.
SETDATE JS FORMAT INSTALL
Npm install luxon or yarn add luxon (visit link for other installation methods) Luxon is hosted on the Moment.js website and developed by a Moment.js developer because Moment.js has limitations that the developer wanted to address but couldn't. If you want to use a one solution to fit all, I recommend using date-fns or Luxon. For instance, the month can be show as full-text or abreviation. The option second parameter, allow for configuring more specific format inside the selected locale. The function by default will return the format configured on the device it runs, but this can be specified by setting the arguments (ECMA-402).

This is because different cultures make uses of different formats, and express their date or time in different ways. Some of the standard functions listed above are dependent on the locale: Return '' + (d new Date().toISOString().slice(0,10) //return YYYY-MM-DDĬonsole.log("1) "+ new Date().toDateString()) Ĭonsole.log("2) "+ new Date().toISOString()) Ĭonsole.log("4) "+ new Date().toLocaleDateString()) Ĭonsole.log("5) "+ new Date().toLocaleString()) Ĭonsole.log("6) "+ new Date().toLocaleTimeString()) Ĭonsole.log("7) "+ new Date().toString()) Ĭonsole.log("8) "+ new Date().toISOString().slice(0,10)) Specifying the locale for standard functions: Note: If you would only like to configure the content options, but still use the current locale, passing null for the first parameter will cause an error. You can change the number of options values based on your requirements, and this will also reflect the presence of each date time term. Possible values are "numeric", "2-digit", "narrow", "short", "long".Īll these keys are optional. Possible values are "narrow", "short", "long". Possible values are "numeric", "2-digit".

The locale and options arguments let applications specify the language whose formatting conventions should be used, and allow some customization of the rendering. If you need slightly less control over formatting than the currently accepted answer, Date#toLocaleDateString can be used to create standard locale-specific renderings.
