Working with Dates in JavaScript

Given the quite limited built-in functionality for working with dates in JavaScript, here are some bits and pieces I have found useful.

Comparing two dates by value:
oDateOne - oDateTwo === 0

Checking if a string converts to a valid date:
var d = new Date("foo");
test using === "[object Date]" && !isNaN(d.getTime())

Formatting dates is easy if you’re using jQuery UI:
$.datepicker.formatDate('yy-mm-dd', new Date(2007, 1 - 1, 26));
Alternatively, the date formatting capabilities of Moment.js are very useful.


About Jennifer Phillips Campbell

Software Developer and Medieval Historian
This entry was posted in JavaScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s