Convert MM/DD/YYYY date to Month Day Year
Asked Answered
B

8

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?

Bedplate answered 14/9, 2016 at 16:3 Comment(2)
You want to use moment.js or want only pure javascript?Stichomythia
something like or accurately?Basilicata
C
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>
Craggie answered 14/9, 2016 at 16:29 Comment(1)
Really helps...ThanksUnshapen
A
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
Arvell answered 14/9, 2016 at 16:17 Comment(0)
G
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>
Gongorism answered 12/12, 2017 at 9:31 Comment(0)
V
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>
Vertievertiginous answered 14/9, 2016 at 16:8 Comment(0)
C
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"
Cobos answered 14/9, 2016 at 16:8 Comment(3)
OP is not asking for "something similar".Flinch
"...So for example, 02/12/2013 would convert to SOMETHING LIKE Tuesday February 12 2013." Also a quick String.replace() could fix the format without the need of an external libraryCobos
Parsing strings with the Date constructor (or Date.parse) is not recommended as it's largely implementation dependent and unreliable.Bibliolatry
B
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);
Basilicata answered 14/9, 2016 at 16:32 Comment(2)
Not all current browsers support the Intl object (mobile support is particularly poor), nor do many browsers in current use.Bibliolatry
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. github.com/andyearnshaw/Intl.js Like everything, support will get better. I don't know if transpilers support it.Basilicata
C
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

Cocktail answered 14/9, 2016 at 16:17 Comment(0)
L
-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"

Lamdin answered 14/9, 2016 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.