0

I have this date "Wednesday, 4th Apr 2018" format and I am trying to format this in "yyyy-MM-dd". It's working fine for all months but getting issue for April and august month. I am not getting what is the issue as it's working with other months This is my code

var Arrivaldte = Date.parse("Wednesday, 4th Apr 2018").toString('yyyy-MM-dd');
console.log(Arrivaldte);
mplungjan
  • 134,906
  • 25
  • 152
  • 209
testingexpert
  • 43
  • 1
  • 8
  • 3
    There is no jQuery in your question - [date toString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) does not take a format. I think you meant [dateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat) – mplungjan Mar 27 '18 at 09:08
  • 1
    @phuzi - that is a VERY old post. There must be a better/newer post – mplungjan Mar 27 '18 at 09:12
  • i have already tried this – testingexpert Mar 27 '18 at 09:13
  • 2
    Also your parse does not accept that format. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – mplungjan Mar 27 '18 at 09:15

1 Answers1

0

Using Mozilla docs as reference, your date string does not meet the standard RFC2822 .

That th part should be removed. Example:

var Arrivaldte = new Date("Wednesday, 4 Apr 2018").toString('yyyy-MM-dd');
console.log(Arrivaldte);

Alternatively, use moment.js:

moment("Wednesday, 4th Apr 2018")

But, be aware of this deprecation warning:

Value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release

Juan Diego Godoy Robles
  • 12,742
  • 2
  • 36
  • 47