Javascript, Dates, Times, and One Man’s Dream

Posted: September 16th, 2008 | Author: | Filed under: coding, javascript | Tags: , , , , | 534 Comments »

OK, so the title of this post might be a bit overstating.

But, if you work with Javascript and need to use dates/times, you should absolutely check out a JS library for formatted dates and times by Steven Levithan (who is obviously cool; his blog is titled “Flagrant Badassery”).

It’s a library that extends the stock Date object in Javascript to let you easily generate nicely formatted dates. Just put the code in a file and load it up. Then you can do neat things like this (adapted from his blog)

var now = new Date();

now.format(“m/dd/yy”);
// Returns, e.g., 6/09/07

// You can use one of several named masks
now.format(“isoDateTime”);
// 2007-06-09T17:46:21

// …Or add your own
dateFormat.masks.hammerTime = ‘HH:MM! “Can\’t touch this!”‘;
now.format(“hammerTime”);
// 17:46! Can’t touch this!


534 Comments on “Javascript, Dates, Times, and One Man’s Dream”

  1. 1 Greg said at 7:26 pm on September 16th, 2008:

    Yes, but can it do cool things like python?

    datetime.today()+datetime.timedelta(weeks=-1) # 1 week ago

  2. 2 Tim said at 9:30 pm on September 16th, 2008:

    Nope. Still the dark ages of Unix timestamps. But, if you have to use Unix timestamps, you may as well put a nice wrapper around it.

  3. 3 Billy said at 8:52 pm on September 17th, 2008:

    dark ages of Unix timestamps? please. more like wonderfully persistent and delicious ages of Unix timestamps.

  4. 4 Tim said at 7:57 am on September 18th, 2008:

    Delicious for the next 30 years. Of course, I wouldn’t mind getting some Y2K-style paychecks in about 30 years when everyone goes “I can’t believe we’re still running code from 2010!!” :D


Leave a Reply

You must be logged in to post a comment.