Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert MM/DD/YYYY date to Month Day Year

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?

like image 501
SteveV Avatar asked Dec 05 '22 16:12

SteveV


2 Answers

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>
like image 182
Abdul Mateen Mohammed Avatar answered Dec 08 '22 05:12

Abdul Mateen Mohammed


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
like image 25
depperm Avatar answered Dec 08 '22 07:12

depperm