The Hacker said about 1 year ago permalink Comment? (0)
Tagged: rails javascript ruby sugar

Flexible date input and manipulation in javascript with date.js

date.js is a great little JavaScript library that can make your life a lot easier.

If your used to ruby’s date functions then date.js will make you feel right at home.

date.js can:

  • Parse strings into dates.
  • Add and Subtract time in days,hours,months or years.
  • Easily return “x” “day of week” of “month”
  • Boolean assertions for day, week, month, year.
  • Turn you into a JavaScript ninja.

Note: Some of the syntactic goodies require ‘sugar.js’

Going into the future is no problem for date.js

Date.today().add(5).days(); Date.today().next().friday();

Interested in this Friday? April of this year?

Date.friday(); Date.april();

What about the first friday of april? No Sweat!

Date.april().first().friday();

Assert any date properties you want.

Date.today().is().friday(); // returns true orfalse

It can parse just about anything you throw at it.

Date.parse(‘today’); Date.parse(‘tomorrow’); Date.parse(‘July 8’); Date.parse(‘July 8th, 2007’); Date.parse(‘July 8th, 2007, 10:30 PM’); // Even crazy! things like Date.parse(‘last april’); Date.parse(‘+2days’);

There are also some fun number functions.

(8).days().fromNow(); (2).months().ago();

For more reading, checkout: date.js examples