-2

In some languages (or frameworks) there are format patterns to render the day of week, either full or in three characters, like ddd or dddd for example: date.format("mm.dd.yyyy, ddd) -> 05.06.2020, Tue

Is there any equivalent standard in javascript to achieve this?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • Does this answer your question? [Javascript: output current datetime in YYYY/mm/dd hh:m:sec format](https://stackoverflow.com/questions/8362952/javascript-output-current-datetime-in-yyyy-mm-dd-hhmsec-format) – Daan May 26 '20 at 09:41
  • Yes, take a look at this `new Date().toDateString()` – gorak May 26 '20 at 09:42
  • @Daan, how it is answers to how to display the name of the day, like Mon or Monday? – g.pickardou May 26 '20 at 09:44
  • @gorak: I would like to provide format using y and m and d chars, like in many languages, and place the abbreviation other times the full name to the tormatted string – g.pickardou May 26 '20 at 09:47
  • If there is no way, why not simply answer that? Instead overacting a minus.. Obviously needs to understand what the *format pattern* means – g.pickardou May 26 '20 at 09:48
  • the comment: *use Intl.DateTimeFormat see: https://tc39.es/ecma402/#datetimeformat-objects and its weekday property* would be helpful. or just: *see Intl.DateTimeFormat and weekday* – g.pickardou May 26 '20 at 12:53
  • As explained in [this answer](https://stackoverflow.com/a/62028196/257182), a new [Temporal object proposal](https://github.com/tc39/proposal-temporal) is being developed that will provide much more functionality to ECMAScript dates. – RobG May 26 '20 at 22:17

1 Answers1

1

First of all I don't get the minus votes. This is a good question. However asked already. There is Intl.DateTimeFormat available: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

Explained here as well: How to format a JavaScript date

You can also use toISOString(), toLocaleDateString, toDateString etc.

new Date(1590486552434).toISOString();
"2020-05-26T09:49:12.434Z"
new Date(1590486552434).toLocaleDateString();
"5/26/2020"

and the link example

const d = new Date('2010-08-05')
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d)
const mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d)
const da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d)

console.log(`${da}-${mo}-${ye}`)
Shnigi
  • 972
  • 9
  • 19