Poetry of Programming

Its about Ruby on Rails – Kiran Soumya

By

Rails’ time extensions ported to JavaScript

Ström have ported Rails’ Numeric Time extensions to JavaScript since he needed to do some date calculations for a project. Now one should be able to calculate dates in JS like this:
(5).seconds() // => 5000
(2).weeks() + (2).hours() + (15).seconds() // => 1216815000
(1).week().fromNow().toDate() // => Fri Sep 22 2006 19:16:32 GMT+0200 (CEST)
var date = new Date(2000, 11, 18, 18, 15, 23);
(2).years().since(date).toDate() // => Thu Dec 19 2002 06:15:23 GMT+0100 (CET)
Thought it could be useful for others as well.
You’ll need the latest prototype and scripta.culo.us libraries, unit tests are included.

Leave a Reply

Your email address will not be published. Required fields are marked *