0

Original Question How to format the date correctly?

I need to change date in this format - Fri Jul 12 2019 to this format Friday, 12 Jule.

I have tried using date-fns

(Reason: An element has a date attribute in this format)

RobG
  • 142,382
  • 31
  • 172
  • 209
evans
  • 549
  • 7
  • 22
  • use moment js to change – rajesh Jul 30 '19 at 08:50
  • momentJS is the best plugin to do this. You could find it here: https://momentjs.com – Mo Baqeri Jul 30 '19 at 08:59
  • 1
    @MohammadBagheri why should momentjs compared to date-fns be the better choice or the best library. date-fns was created due to some design flaws of momentjs. – t.niese Jul 30 '19 at 09:00
  • @t.niese well in my opinion momentJS is very simple and easy to use. I have not used fate-fns and whatever I have needed so far has been done by momentJS. This is my personal opinion. – Mo Baqeri Jul 30 '19 at 09:05
  • @MohammadBagheri then you shouldn't say that it is the best library. – t.niese Jul 30 '19 at 09:07
  • This question has been asked [*many, many times before*](https://stackoverflow.com/search?q=%5Bjavascript%5D+reformat+date+string). Please search before creating a new question. Also many duplicates using [*date-fns*](https://stackoverflow.com/search?q=%5Bjavascript%5D%5Bdate-fns%5D+format+date). – RobG Jul 30 '19 at 10:12

4 Answers4

1

You can parse yourself the date with pure JS if you want :

    var dateToParse = 'Fri Jul 12 2019';
var dateParsed = new Date(dateToParse);

const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const months = ['January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December'
];

document.write(days[dateParsed.getDay()] + ', ' + dateParsed.getDate() + ' ' + months[dateParsed.getMonth()]);

JSFiddle

0

Using date-fns

const date = "Fri Jul 12 2019"
const dateFormat = 'dddd, DD MMMM';
console.log(dateFns.format(new Date(date),dateFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>

It would recommend use of library like momentjs

const date = "Fri Jul 12 2019"
const dateFormat = 'dddd, DD MMMM';
console.log(moment(new Date(date)).format(dateFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
  • When using a library for formatting, it should also be used for parsing. Fri Jul 12 2019 is not a format supported for parsing by ECMA-262 (though it is the format for [*toDateString*](http://ecma-international.org/ecma-262/10.0/#sec-date.prototype.todatestring)), so results are implementation dependent. Also, `moment(new Date(date))` should produce identical results to `moment(date)` in this case. – RobG Jul 30 '19 at 10:06
0

You can use MomentJs easy to change the date formats

const date = "Fri Jul 12 2019"
const dateFormat = 'dddd, DD MMMM';
console.log("New Date Format is",moment(new Date(date)).format(dateFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Harsh Makadia
  • 3,263
  • 5
  • 30
  • 42
0

Changing Date format using date_fns

const date = "Fri Jul 12 2019"
const format = 'dddd, DD MMMM';
const newdate = dateFns.format(new Date(date),format);
console.log(newdate);

Calling CDN

<script src="//cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>