294

I want to parse the following string with moment.js 2014-02-27T10:00:00 and output day month year (14 march 2014) I have been reading the docs but without success http://momentjs.com/docs/#/parsing/now/

reachify
  • 3,657
  • 2
  • 19
  • 22

6 Answers6

597

I always seem to find myself landing here only to realize that the title and question are not quite aligned.

If you want a moment date from a string:

const myMomentObject = moment(str, 'YYYY-MM-DD')

From moment documentation:

Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object.

If you instead want a javascript Date object from a string:

const myDate = moment(str, 'YYYY-MM-DD').toDate();
Stephen Paul
  • 37,253
  • 15
  • 92
  • 74
280

You need to use the .format() function.

MM - Month number

MMM - Month word

var date = moment("2014-02-27T10:00:00").format('DD-MM-YYYY');
var dateMonthAsWord = moment("2014-02-27T10:00:00").format('DD-MMM-YYYY');

FIDDLE

David East
  • 31,526
  • 6
  • 67
  • 82
  • What about existing date variables. `var date = moment("2014-02-27T10:00:00").format('DD-MM-YYYY'); var dateCalendarPart = moment(date).format('YYYY/MM/DD'); alert(date); alert(dateCalendarPart);` Gives an invalid date error????? – Andrew Day Jun 15 '16 at 11:17
  • 3
    try ''var dateCalendarPart = moment(date,'YYYY/MM/DD'); alert(date); '' – ranjan May 04 '17 at 06:09
  • 1
    @AndrewDay ... `"01-02-2017"`, "January 2" or "February 1" ? Momentjs can't read your mind. –  Jul 01 '17 at 05:39
  • 4
    This answer only works because the OP's string is in a standard Date format. Stephen Paul's answer shows how to convert any string, given a specific format, to a date that can be manipulated. – Agamemnus Mar 23 '19 at 21:05
  • It returns string not a date as OP asked. Weird it gets upvotes. – Saulius Feb 25 '21 at 12:20
  • ```.format()``` return a string, he is asking to get Date object from string – AdamWist Oct 18 '21 at 14:23
31

No need for moment.js to parse the input since its format is the standard one :

var date = new Date('2014-02-27T10:00:00');
var formatted = moment(date).format('D MMMM YYYY');

http://es5.github.io/#x15.9.1.15

  • 1
    That's the truth, no need for moment.js if you have a ISO date string... You could also use [Date.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) to be more clear that you're working with ISO strings (since the constructor can take other formats) – Ruan Mendes Mar 04 '14 at 22:58
  • 4
    Doesn't work in IE8... What a surprise... Gives `NaN`. –  Mar 05 '14 at 22:56
  • @JuanMendes, your link says, that `Date.parse` calls `new Date`, so it should not make a difference. – sk904861 Aug 03 '15 at 09:14
9

moment was perfect for what I needed. NOTE it ignores the hours and minutes and just does it's thing if you let it. This was perfect for me as my API call brings back the date and time but I only care about the date.

function momentTest() {

  var varDate = "2018-01-19 18:05:01.423";
  var myDate =  moment(varDate,"YYYY-MM-DD").format("DD-MM-YYYY");
  var todayDate = moment().format("DD-MM-YYYY");  
  var yesterdayDate = moment().subtract(1, 'days').format("DD-MM-YYYY");   
  var tomorrowDate = moment().add(1, 'days').format("DD-MM-YYYY");

  alert(todayDate);

  if (myDate == todayDate) {
    alert("date is today");
  } else if (myDate == yesterdayDate) {
    alert("date is yesterday");
  } else if (myDate == tomorrowDate) {
    alert("date is tomorrow");
  } else {
    alert("It's not today, tomorrow or yesterday!");
  }
}
gareth
  • 199
  • 1
  • 3
3
  • How to change any string date to object date (also with moment.js):

let startDate = "2019-01-16T20:00:00.000"; let endDate = "2019-02-11T20:00:00.000"; let sDate = new Date(startDate); let eDate = new Date(endDate);

  • with moment.js:

startDate = moment(sDate); endDate = moment(eDate);

MorLavender
  • 106
  • 1
  • 8
1

Maybe try the Intl polyfill for IE8 or the olyfill service ?

or

https://github.com/andyearnshaw/Intl.js/

Davet
  • 334
  • 1
  • 3
  • 15