311

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/

0

7 Answers 7

632

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, only this one answers. Parsing string to date (meaning input is str, out is date)
excellent answer, thank you very much as this solved my issue. I needed the JavaScript date object
283

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

6 Comments

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?????
try ''var dateCalendarPart = moment(date,'YYYY/MM/DD'); alert(date); ''
@AndrewDay ... "01-02-2017", "January 2" or "February 1" ? Momentjs can't read your mind.
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.
It returns string not a date as OP asked. Weird it gets upvotes.
|
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

4 Comments

That's the truth, no need for moment.js if you have a ISO date string... You could also use Date.parse to be more clear that you're working with ISO strings (since the constructor can take other formats)
Doesn't work in IE8... What a surprise... Gives NaN.
@JuanMendes, your link says, that Date.parse calls new Date, so it should not make a difference.
Sorry to resurrect this 10 years later, but I was researching the same thing today. This answer is fine, if the value provided to the Date constructor is always going to be an ISO date string. In my case, the value could be an ISO string, or any other string, or any number. new Date(value) will happily turn just the number 3 into a Date, which is not what I needed.
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!");
  }
}

Comments

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);

Comments

1

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

or

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

2 Comments

Internet Explorer is no longer.
oh how i dream of this privilege
-1

moment("2020-01-01").utc().format('DD-MM-YYYY')

moment().format(); // 2013-02-04T10:35:24-08:00 moment.utc().format(); // 2013-02-04T18:35:24+00:00

Is giving the correct output for me, where "2020-01-01" can be any date even in datetime format will also work.

1 Comment

this is about parsing and not format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.