Reputation: 804
I'm using momentjs to format my dates. However it is displaying the date a day off and I'm not sure why. Here is the code I'm using:
moment(date).format('MMMM do YYYY, h:mma')
where date = Sat May 05 2018 00:26:53 GMT-0400 (Eastern Daylight Time)
However the result is May 6th 2018, 12:26am
- showing may 6 instead of may 5.
Does anyone know why this may be? Thanks.
Upvotes: 1
Views: 1394
Reputation: 23859
You're passing non-ISO string without specifying the format, which will result in a warning on the console, and may show the wrong date.
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. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Always pass the format as a preventive measure. Coming to your problem, to display the date correctly, you need to use Do
specifier, instead of do
. The former is for Day of Month but latter is for Day of Week.
var date = moment('Sat May 05 2018 00:26:53 GMT-0400 (Eastern Daylight Time)', 'ddd MMM DD YYYY HH:mm:ss');
console.log(date.format('MMMM Do YYYY, h:mma'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Upvotes: 4