Created
December 21, 2011 23:20
-
-
Save fauxparse/1508172 to your computer and use it in GitHub Desktop.
CoffeeScript date utilities
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
Number::pad = (digits, signed) -> | |
s = Math.abs(@).toString() | |
s = "0" + s while s.length < digits | |
(if @ < 0 then "-" else (if signed then "+" else "")) + s | |
Date.months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] | |
Date.weekdays = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ] | |
Date.formats = | |
"a": -> Date.weekdays[@getDay()].substring(0, 3) | |
"A": -> Date.weekdays[@getDay()] | |
"b": -> Date.months[@getMonth()].substring(0, 3) | |
"B": -> Date.months[@getMonth()] | |
"c": -> @toLocaleString() | |
"d": -> @getDate().toString() | |
"F": -> "#{@getFullYear()}-#{@getMonth() + 1}-#{@getDate()}" | |
"H": -> @getHours().pad(2) | |
"I": -> "#{(@getHours() % 12) || 12}" | |
"j": -> @getDayOfYear() | |
"L": -> @getMilliseconds().pad(3) | |
"m": -> (@getMonth() + 1).pad(2) | |
"M": -> @getMinutes().pad(2) | |
"N": -> @getMilliseconds().pad(3) | |
"p": -> if @getHours() < 12 then "AM" else "PM" | |
"P": -> if @getHours() < 12 then "am" else "pm" | |
"S": -> @getSeconds().pad(2) | |
"s": -> Math.floor(@getTime() / 1000) | |
"U": -> @getWeekOfYear() | |
"w": -> @getDay() | |
"W": -> @getWeekOfYear(1) | |
"y": -> @getFullYear() % 100 | |
"Y": -> @getFullYear() | |
"x": -> @toLocaleDateString() | |
"X": -> @toLocaleTimeString() | |
"z": -> Math.floor((z = -@getTimezoneOffset()) / 60).pad(2, true) + (Math.abs(z) % 60).pad(2) | |
"Z": -> /\(([^\)]*)\)$/.exec(@toString())[1] | |
Date::format = (fmt) -> | |
parts = (fmt || "%c").split "%%" | |
for own char, callback of Date.formats | |
r = new RegExp("%#{char}", "g") | |
parts = (part.replace(r, => callback.apply(this)) for part in parts) | |
parts.join "%" | |
Date::getDayOfYear = -> Math.ceil((@getTime() - new Date(@getFullYear(), 0, 1).getTime()) / 24 / 60 / 60 / 1000) | |
Date::getWeekOfYear = (start = 0) -> | |
Math.floor((@getDayOfYear() - (start + 7 - new Date(@getFullYear(), 0, 1).getDay()) % 7) / 7) + 1 |
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
// Automatically compiled to JavaScript | |
var __hasProp = Object.prototype.hasOwnProperty; | |
Number.prototype.pad = function(digits, signed) { | |
var s; | |
s = Math.abs(this).toString(); | |
while (s.length < digits) { | |
s = "0" + s; | |
} | |
return (this < 0 ? "-" : (signed ? "+" : "")) + s; | |
}; | |
Date.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
Date.weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; | |
Date.formats = { | |
"a": function() { | |
return Date.weekdays[this.getDay()].substring(0, 3); | |
}, | |
"A": function() { | |
return Date.weekdays[this.getDay()]; | |
}, | |
"b": function() { | |
return Date.months[this.getMonth()].substring(0, 3); | |
}, | |
"B": function() { | |
return Date.months[this.getMonth()]; | |
}, | |
"c": function() { | |
return this.toLocaleString(); | |
}, | |
"d": function() { | |
return this.getDate().toString(); | |
}, | |
"F": function() { | |
return "" + (this.getFullYear()) + "-" + (this.getMonth() + 1) + "-" + (this.getDate()); | |
}, | |
"H": function() { | |
return this.getHours().pad(2); | |
}, | |
"I": function() { | |
return "" + ((this.getHours() % 12) || 12); | |
}, | |
"j": function() { | |
return this.getDayOfYear(); | |
}, | |
"L": function() { | |
return this.getMilliseconds().pad(3); | |
}, | |
"m": function() { | |
return (this.getMonth() + 1).pad(2); | |
}, | |
"M": function() { | |
return this.getMinutes().pad(2); | |
}, | |
"N": function() { | |
return this.getMilliseconds().pad(3); | |
}, | |
"p": function() { | |
if (this.getHours() < 12) { | |
return "AM"; | |
} else { | |
return "PM"; | |
} | |
}, | |
"P": function() { | |
if (this.getHours() < 12) { | |
return "am"; | |
} else { | |
return "pm"; | |
} | |
}, | |
"S": function() { | |
return this.getSeconds().pad(2); | |
}, | |
"s": function() { | |
return Math.floor(this.getTime() / 1000); | |
}, | |
"U": function() { | |
return this.getWeekOfYear(); | |
}, | |
"w": function() { | |
return this.getDay(); | |
}, | |
"W": function() { | |
return this.getWeekOfYear(1); | |
}, | |
"y": function() { | |
return this.getFullYear() % 100; | |
}, | |
"Y": function() { | |
return this.getFullYear(); | |
}, | |
"x": function() { | |
return this.toLocaleDateString(); | |
}, | |
"X": function() { | |
return this.toLocaleTimeString(); | |
}, | |
"z": function() { | |
var z; | |
return Math.floor((z = -this.getTimezoneOffset()) / 60).pad(2, true) + (Math.abs(z) % 60).pad(2); | |
}, | |
"Z": function() { | |
return /\(([^\)]*)\)$/.exec(this.toString())[1]; | |
} | |
}; | |
Date.prototype.format = function(fmt) { | |
var callback, char, part, parts, r, _ref; | |
parts = (fmt || "%c").split("%%"); | |
_ref = Date.formats; | |
for (char in _ref) { | |
if (!__hasProp.call(_ref, char)) continue; | |
callback = _ref[char]; | |
r = new RegExp("%" + char, "g"); | |
parts = (function() { | |
var _i, _len, _results, | |
_this = this; | |
_results = []; | |
for (_i = 0, _len = parts.length; _i < _len; _i++) { | |
part = parts[_i]; | |
_results.push(part.replace(r, function() { | |
return callback.apply(_this); | |
})); | |
} | |
return _results; | |
}).call(this); | |
} | |
return parts.join("%"); | |
}; | |
Date.prototype.getDayOfYear = function() { | |
return Math.ceil((this.getTime() - new Date(this.getFullYear(), 0, 1).getTime()) / 24 / 60 / 60 / 1000); | |
}; | |
Date.prototype.getWeekOfYear = function(start) { | |
if (start == null) start = 0; | |
return Math.floor((this.getDayOfYear() - (start + 7 - new Date(this.getFullYear(), 0, 1).getDay()) % 7) / 7) + 1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment