I'm trying to convert a MM/DD/YYYY date to a long date. So for example, 02/12/2013
would convert to something like Tuesday February 12 2013
.
I've looked at MomentJS and other JS methods, but nothing really did what I wanted. Or at least, I didn't think so.
Is there a way to make this date conversion accurately?
Using moment.js,
You can do it like this with a JavaScript Date object
var date = new Date(2013, 1, 12);
console.log(moment(date).format('dddd MMMM D Y'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
If you want to convert a date string to a long date format string you can do it like this.
var longDateStr = moment('02/12/2013', 'M/D/Y').format('dddd MMMM D Y');
console.log(longDateStr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
If you don't want any other scripts you could use Date
and some arrays
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var now = new Date('02/12/2013');
console.log(days[now.getDay()] + ' ' + months[now.getMonth()] + ' ' + now.getDate() + ' ' + now.getFullYear()); //Tuesday February 12 2013
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With