Created
April 1, 2013 20:02
-
-
Save ashblue/5287311 to your computer and use it in GitHub Desktop.
Custom JavaScript prototype class for generating dates
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
| var wk = wk || {}; | |
| $(document).ready(function () { | |
| var _monthNames = [ | |
| 'January', | |
| 'February', | |
| 'March', | |
| 'April', | |
| 'May', | |
| 'June', | |
| 'July', | |
| 'August', | |
| 'September', | |
| 'October', | |
| 'November', | |
| 'December' | |
| ]; | |
| /** | |
| * Set to public since this method is accessed in a few places | |
| * @param {number} month Month in digit format form 0 - 11 | |
| * @return {string} | |
| */ | |
| wk.dateGetMonthName = function (month) { | |
| return _monthNames[month]; | |
| }; | |
| /** | |
| * Constructor for a date | |
| * @param {string} date Date processed by Date.new() | |
| * @returns {self} | |
| */ | |
| wk.Date = function (date) { | |
| this.setDate(date); | |
| return this; | |
| }; | |
| /** | |
| * Uses a UTC date | |
| * @url http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s | |
| * @param date | |
| * @returns {*} | |
| */ | |
| wk.Date.prototype.setDate = function (date) { | |
| this.timestamp = new Date(date); | |
| return this; | |
| }; | |
| wk.Date.prototype.getTimestamp = function () { | |
| return this.timestamp; | |
| }; | |
| wk.Date.prototype.getUTCTimestamp = function () { | |
| return this.timestamp.toUTCString(); | |
| }; | |
| wk.Date.prototype.getTimestampTomorrow = function () { | |
| return new Date(this.timestamp.getTime() + (24 * 60 * 60 * 1000)); | |
| }; | |
| wk.Date.prototype.getMonthName = function () { | |
| return _monthNames[this.timestamp.getUTCMonth()]; | |
| }; | |
| wk.Date.prototype.getFriendly = function () { | |
| return this.timestamp.getUTCFullYear() + '-' + ('0' + (this.timestamp.getUTCMonth() + 1)).slice(-2) + '-' + ('0' + this.timestamp.getUTCDate()).slice(-2); | |
| }; | |
| wk.Date.prototype.getFancy = function () { | |
| var month = _monthNames[this.timestamp.getUTCMonth()].substr(0, 3), | |
| day = this.timestamp.getUTCDate(), | |
| year = this.timestamp.getUTCFullYear(); | |
| return month + ' ' + day + ', ' + year; | |
| }; | |
| wk.Date.prototype.subtractDays = function (days) { | |
| this.timestamp.setDate(this.timestamp.getUTCDate() - days); | |
| return this.timestamp; | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment