-4

My ts file:

mydate: Date = new Date('2021-11-14T18:30:00.000+00:00');

I want in this format:-

07-July-2022

Plzz Help

Benezir
  • 113
  • 1
  • 9

2 Answers2

0

Note: When parsing date strings with the Date constructor (and Date.parse, they are equivalent), always make sure that the input conforms to the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ) — the parsing behavior with other formats is implementation-defined and may not work across all browsers. Support for RFC 2822 format strings is by convention only. A library can help if many different formats are to be accommodated.

Date-only strings (e.g. "1970-01-01") are treated as UTC, while date-time strings (e.g. "1970-01-01T12:00") are treated as local. You are therefore also advised to make sure the input format is consistent between the two types.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

It is not recommended to parse strings like that with the Date object. You really should give a library a try. While moment.js was a popular choice in the past, I personally have a great experience with date-fns.

Docs: https://date-fns.org/

MoxxiManagarm
  • 8,735
  • 3
  • 14
  • 43
-3

The simplest way is using the day.js

dayjs("2022/07/07").format("DD-MMMM-YYYY")
  • 3
    moment.js is not recommended to use in new projects, even the developers themselves state that. https://momentjs.com/docs/ But have a look on the docs, they recommend a list otf alternatives. – MoxxiManagarm Jul 07 '22 at 13:11
  • Not to mention, even when they were maintaining the library, they explicitly said that you should give a format when parsing. – Heretic Monkey Jul 07 '22 at 13:13
  • I just want to say that it is a lot easy using some library, not native Date API. Also I am not a fan of moment.js, in my projects I often use Ant.Design whose components are using moment.js, so I just write the first library I know at all – standbyoneself Jul 07 '22 at 13:30
  • Changing the recommended lib only improves your answer halfway. If you provide code, then provide the code in full, which also includes the formatting to `07-July-2022` – MoxxiManagarm Jul 07 '22 at 14:04