Date parsing with JavaScript, Introducing Moment.js

July 08, 2013

JavaScript already has a Date.parse() method, why would we talk about date parsing?

Because in short the Date.parse() method implementation is dependent on the browser itself, so it could return a different result in chrome/firefox/ie like here**

Introducing Moment.js

Moment.js is a library for parsing, validating, manipulating, and formatting dates.

it has an excellent documentation, it has plenty of helpful examples

Parsing & Validating dates

var isValid = moment('12-252-1995').isValid() //returns false
var date = moment('12-25-1995', 'MM-DD-YYYY') //prase a date using a specify format, if the actual date is in a different format isValid() will return false
var date = moment('12-25-1995', ['MM-DD-YYYY', 'YYYY-MM-DD']) //parse a date with a list of possible formats that it could matches

//using it as a wrapper of a date object
var day = new Date(2011, 9, 16)
var dayWrapper = moment(day)

Bonus for Asp.net MVC users

ASP.NET returns dates in Json as /Date(1198908717056)/ or /Date(1198908717056-0700)/

This will happen often if you try to make an ajax call to an asp.net action and it returns a JsonResult that contains DateTime

var date = moment('/Date(1198908717056-0700)/') // December 28 2007 10:11 PM

Nadeem Khedr

Written by Nadeem Khedr citizen of the world, you can find me on Twitter & Github