Created
July 12, 2011 23:33
-
-
Save timruffles/1079426 to your computer and use it in GitHub Desktop.
coffeescript version of ActiveSupport::Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define [], -> | |
| periods = | |
| second: second = 1000 | |
| minute: min = 60 * second | |
| hour: hour = min * 60 | |
| day: day = hour * 24 | |
| week: week = day * 7 | |
| month: month = day * 29.6 | |
| year: day * 356 | |
| for period, value of periods | |
| do (period, value) -> | |
| Number::[period] = Number::["#{period}s"] = -> value * this | |
| extensions = | |
| Number: | |
| # differences | |
| fromNow: -> this + (+new Date()) | |
| from: (date) -> (+date) + this | |
| before: (date) -> (+date) - this | |
| ago: (date) -> (+new Date()) - this | |
| # conversions | |
| toDate: -> new Date(this) | |
| Date: | |
| secondsAfter: (date) -> ((+this) - (+date)) / 1000 | |
| secondsBefore: (date) -> ((+date) - (+this)) / 1000 | |
| unless Date.now | |
| Date.now = -> new Date() | |
| for type, methods of extensions | |
| for name, method of methods | |
| @[type]::[name] = method | |
| null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment