55

Since I've started to use jQuery, I have been doing a lot more JavaScript development.

I have the need to parse different date formats and then to display them into another format.

Do you know of any good tool to do this?

Which one would you recommend?

Elijah Manor
  • 17,923
  • 17
  • 72
  • 79

4 Answers4

39

2014 update: Moment.js is an excellent date manipulation library, which includes parsing functions. It doesn't include automatic date format detection, but you can specify multiple parsing patterns.

Do you know the format of the dates before hand though, or are they user input?


A deprecated library for parsing dates is Datejs. It hasn't been updated since 2007.

Craig
  • 36,306
  • 34
  • 114
  • 197
  • 1
    MomentJS does not actually parse strings to dates, though: https://github.com/moment/moment/issues/1407 – nnyby Nov 19 '14 at 00:47
10

If you already use jqueryUI on the page, than $.datepicker has parseDate() and formatDate() functions that enough in most cases. As for DateJS - profile yu code and you'll see how it baggy and how much time it spends in handling exception

user545829
  • 151
  • 2
  • 8
  • 1
    the date.js library seems buggy and I had problems implementing it while using the jquery ui datepicker, this seems a better supported solution. – Liam Mar 12 '12 at 15:15
  • @Liam this has only date what in case we want some time also to come with it.... – Varun Jun 05 '12 at 06:37
5

I've found the jQuery Globalization Plugin date parsing to work best. Other methods had cross-browser issues and stuff like date.js had not been updated in quite a while.

You also don't need a datePicker on the page. You can just call something similar to the example given in the docs:

$.parseDate('yy-mm-dd', '2007-01-26');
Brian Ellis
  • 1,829
  • 1
  • 19
  • 24
3

Just another option (which I wrote - full disclosure):

DP_DateExtensions Library

Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc. It's liberally open sourced.

I really like Date.js but find some of the syntax cumbersome. Most of the functions in mine are based (at least in part) on the rich set of functions in CFML - some people may find it a bit more comfortable.

No reason to consider it if you're already using a framework (they're all capable), but if you just need to quickly add date manipulation to a project give it a chance.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jim Davis
  • 1,230
  • 6
  • 11