6

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?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
SteveV
  • 429
  • 3
  • 8
  • 18

8 Answers8

8

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>
Abdul Mateen Mohammed
  • 1,864
  • 1
  • 12
  • 21
7

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
depperm
  • 10,606
  • 4
  • 43
  • 67
5

i think there have some easy way to do this.

function getData() {
  var d = new Date("02/12/2013");
  var options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  };
  var n = d.toLocaleDateString('en-US', options);
  var t = d.toLocaleTimeString('en-GB')
  var replase = n.replace(new RegExp(',', 'g'), ' ')
  document.getElementById("demo").innerHTML = replase
}
getData()
<span id="demo"></span>
depperm
  • 10,606
  • 4
  • 43
  • 67
isuru sampath
  • 106
  • 1
  • 5
3

With moment.js it's quite simple:

console.log(moment('02/12/2013', 'MM/DD/YYYY').format('dddd MMMM D Y'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
2

You can use the plain Javascript Date methods for something similar:

var date  = new Date("01/01/2000")
date.toDateString()   //"Sat Jan 01 2000"
StackOverMySoul
  • 1,957
  • 1
  • 13
  • 21
2

On a modern browser you can use the Intl API

The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The constructors for Collator, NumberFormat, and DateTimeFormat objects are properties of the Intl object.

Parsing is performed manually, Date.parse

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

Date.UTC is used to provide a date from parts for Intl.DateTimeFormat#format.

The Date.UTC() method accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time

Finally the ,'s are removed from the en-US formatted string so that it matches your requirement.

const parts = '02/12/2013'.split('/');
parts.unshift(parts.pop());
parts[1] -= 1;
const dateString = new Intl.DateTimeFormat('en-US', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  timeZone: 'UTC'
}).format(Date.UTC(...parts)).replace(/,/g, '');
console.log(dateString);
Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Not all current browsers [*support the Intl object*](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Intl#Browser_compatibility) (mobile support is particularly poor), nor do many browsers in current use. – RobG Sep 14 '16 at 22:56
  • All major browsers except Safari support its basic implementation, mobiles I have no idea about, but like with ES5, ES6 and ES7 there is a shim/polyfill for it, but I've never tested it. https://github.com/andyearnshaw/Intl.js/ Like everything, support will get better. I don't know if transpilers support it. – Xotic750 Sep 14 '16 at 23:06
0

FullJS :

function mydate(date)
{
  var
    month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    days  = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  ;
  return days[date.getDay()]+' '+month[date.getMonth()]+' '+date.getDate()+' '+date.getFullYear()
}

console.log(mydate(new Date()));

Reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Liberateur
  • 1,337
  • 1
  • 14
  • 33
-1

using moment,

moment().format('dddd MMMM Do YYYY');

on the main page of momentJS, you can see a list of all the possible date formats (http://momentjs.com/).

Do adds "nth" to the date, make it D to only have a number showing, but since youre writing all in words, its better to use "nth"

Bamieh
  • 10,358
  • 4
  • 31
  • 52