Created
May 12, 2011 03:57
-
-
Save xjamundx/967906 to your computer and use it in GitHub Desktop.
Example of a client/server module
This file contains 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
// NOTE: Avoid using any requires up here as | |
// this file may be included on the client. | |
function DateTime (dateOrString, time) { | |
var time; | |
this._date; | |
this.offset; | |
// if you're a DateTime object or an old timemodule object | |
if (dateOrString instanceof DateTime) return dateOrString; | |
// if you're a string with time | |
else if (dateOrString && time) { | |
this._date = DateTime.dateFromString(dateOrString, time); | |
} | |
// if you're a dateTime string | |
else if (dateOrString && (typeof dateOrString === 'string')) { | |
this._date = DateTime.dateFromString(dateOrString); | |
} | |
else if (dateOrString instanceof Date) { | |
this._date = dateOrString; | |
} | |
else if (dateOrString) { | |
// Assume it is a number Pass it straight to date | |
this._date = new Date(dateOrString); | |
} | |
// why not generate a new date time :) | |
else { | |
this._date = new Date(); | |
this._date.setUTCMilliseconds(0); | |
this._date.setUTCSeconds(0); | |
} | |
} | |
DateTime.Second = 1000; | |
DateTime.Minute = 60 * DateTime.Second; | |
DateTime.HalfHour = 30 * DateTime.Minute; | |
DateTime.Hour = 2 * DateTime.HalfHour; | |
DateTime.Day = 24 * DateTime.Hour; | |
DateTime.months = [ | |
"January", | |
"February", | |
"March", | |
"April", | |
"May", | |
"June", | |
"July", | |
"August", | |
"September", | |
"October", | |
"November", | |
"December" | |
]; | |
DateTime.shortMonths = [ | |
"Jan", | |
"Feb", | |
"Mar", | |
"Apr", | |
"May", | |
"Jun", | |
"Jul", | |
"Aug", | |
"Sep", | |
"Oct", | |
"Nov", | |
"Dec" | |
]; | |
DateTime.daysOfWeek = [ | |
"Sunday", | |
"Monday", | |
"Tuesday", | |
"Wednesday", | |
"Thursday", | |
"Friday", | |
"Saturday" | |
]; | |
DateTime.shortDaysOfWeek = [ | |
"Sun", | |
"Mon", | |
"Tue", | |
"Wed", | |
"Thu", | |
"Fri", | |
"Sat" | |
]; | |
DateTime.dateFromString = function (dateString, timeString) { | |
// Needs review: I know this version works. | |
// Not sure the setUTC stuff does | |
var string = dateString.replace(/\-/g, "/"); | |
if (timeString) string += " " + timeString; | |
string += " UTC"; | |
return new Date(string) | |
// var dates = dateString.split("-"), | |
// times = timeString.split(":"), | |
// date = new Date(2000,0,1); | |
// | |
// date.setUTCMinutes(times[1]); | |
// date.setUTCHours(times[0]) | |
// date.setUTCDate(dates[2]); | |
// date.setUTCMonth(parseInt(dates[1]) - 1); | |
// date.setUTCFullYear(dates[0]); | |
// | |
// return date; | |
} | |
DateTime.numberToString = function (num) { | |
if (!num) return "00"; | |
return num.toString().length === 1 ? "0" + num : num; | |
} | |
DateTime.prototype.year = function () { | |
return this._date.getUTCFullYear(); | |
} | |
DateTime.prototype.month = function () { | |
return this._date.getUTCMonth() + 1; | |
} | |
DateTime.prototype.localDate = function () { | |
return this._date.getDate(); | |
} | |
DateTime.prototype.localMonth = function () { | |
return this._date.getMonth() + 1; | |
} | |
DateTime.prototype.date = function () { | |
return this._date.getUTCDate(); | |
} | |
DateTime.prototype.minutes = function () { | |
return this._date.getUTCMinutes(); | |
} | |
DateTime.prototype.setMinutes = function (mins) { | |
this._date.setUTCMinutes(mins); | |
return this; | |
} | |
DateTime.prototype.seconds = function () { | |
return this._date.getUTCSeconds(); | |
} | |
DateTime.prototype.ms = function () { | |
return this._date.getUTCMilliseconds(); | |
} | |
DateTime.prototype.value = function () { | |
return this._date.getTime(); | |
} | |
DateTime.prototype.addDays = function(days) { | |
this._date.setTime(this.value() + (DateTime.Day * days)); | |
return this; | |
} | |
DateTime.prototype.subtractDays = function(days) { | |
this._date.setTime(this.value() - (DateTime.Day * days)); | |
return this; | |
} | |
DateTime.prototype.addMinutes = function (mins) { | |
this._date.setTime(this.value() + (DateTime.Minute * mins)); | |
return this; | |
} | |
DateTime.prototype.subtractMinutes = function (mins) { | |
this._date.setTime(this.value() - (DateTime.Minute * mins)); | |
return this; | |
} | |
DateTime.prototype.roundDownToHalfHour = function () { | |
var min = this.minutes(); | |
if (min < 30) this.setMinutes(0); | |
else this.setMinutes(30); | |
return this; | |
} | |
DateTime.prototype.roundToHalfHour = function () { | |
var min = this.minutes(); | |
if (min < 15) this.setMinutes(0); | |
else if (min > 45) this.setMinutes(60); | |
else this.setMinutes(30); | |
return this; | |
} | |
DateTime.prototype.copy = function () { | |
return new DateTime(this.value()); | |
} | |
DateTime.prototype.toString = function() { | |
return ((this.dateString()) + " " + (this.timeString())); | |
} | |
DateTime.prototype.localPrettyDate = function() { | |
return this.localLongDay() + ", " + this.localShortMonth() + " " + this.localDate(); | |
} | |
DateTime.prototype.localPrettyDateTime = function() { | |
return this.localPrettyDate() + " " + this.localTimeString() + " " + this.localAmPm() | |
} | |
DateTime.prototype.toLocalString = function(amPm) { | |
var str = this.localHours().toString() + ":" + DateTime.numberToString(this.localMinutes()); | |
if (amPm) str += " " + this.localAmPm(); | |
return str; | |
} | |
DateTime.prototype.localTimeString = function(amPm) { | |
var str = this.localHours().toString() + ":" + DateTime.numberToString(this.localMinutes()); | |
if (amPm) str += " " + this.localAmPm(); | |
return str; | |
} | |
// defaults to twelve hour time | |
DateTime.prototype.hours = function(military) { | |
if (typeof military === "undefined") military = true; | |
var hrs = this._date.getUTCHours(); | |
if (!military && hrs > 12) hrs -= 12; | |
if (!military && hrs === 0) hrs = 12; | |
return hrs; | |
} | |
// defaults to twelve hour time | |
DateTime.prototype.localHours = function(military) { | |
var hrs = this._date.getHours(); | |
if (!military && hrs > 12) hrs -= 12; | |
if (!military && hrs === 0) hrs = 12; | |
return hrs; | |
} | |
DateTime.prototype.localAmPm = function() { | |
var hrs = this.localHours(true); | |
return hrs >= 12 ? "PM" : "AM"; | |
} | |
DateTime.prototype.localMinutes = function() { | |
return this._date.getMinutes(); | |
} | |
DateTime.prototype.toStringWithSeconds = function() { | |
return ((this.dateString()) + " " + (this.timeStringWithSeconds())); | |
} | |
DateTime.prototype.localDateString = function() { | |
return ((this.year()) + "-" + (DateTime.numberToString(this.localMonth())) + "-" + (DateTime.numberToString(this.localDate()))); | |
} | |
DateTime.prototype.dateString = function() { | |
return ((this.year()) + "-" + (DateTime.numberToString(this.month())) + "-" + (DateTime.numberToString(this.date()))); | |
} | |
DateTime.prototype.timeString = function() { | |
return DateTime.numberToString(this.hours()) + ":" + DateTime.numberToString(this.minutes()); | |
} | |
DateTime.prototype.timeString12 = function() { | |
return this.hours(false) + ":" + DateTime.numberToString(this.minutes()); | |
} | |
DateTime.prototype.timeStringWithSeconds = function() { | |
return ((this.timeString()) + ":" + (DateTime.numberToString(this.seconds()))); | |
} | |
DateTime.prototype.beginingOfDay = function () { | |
this._date.setHours(0); | |
this._date.setMinutes(0); | |
this._date.setSeconds(0); | |
this._date.setMilliseconds(0); | |
return this; | |
} | |
DateTime.prototype.epoch = function() { | |
return Math.round(this._date.getTime() / 1000); | |
} | |
//gets tomorrow's date | |
DateTime.prototype.tomorrow = function(){ | |
var tomorrow_date = new DateTime(this._date); | |
tomorrow_date.addDays(1); | |
return tomorrow_date.dateString(); | |
} | |
// WARNING: THIS IS NOT CORRECT - doesn't account for time zones | |
// pass in a date from the client instead | |
DateTime.prototype.tomorrowEndOfDay = function(){ | |
return this.tomorrow() + " 23:59"; | |
} | |
// takes another DateTime and calculates the minute difference between the 2 | |
DateTime.prototype.minuteOffsetFromDateTime = function (offsetDt) { | |
return (this.value() / DateTime.Minute) - (offsetDt.value() / DateTime.Minute); | |
} | |
DateTime.prototype.localShortMonth = function() { | |
return DateTime.shortMonths[this._date.getMonth()]; | |
} | |
DateTime.prototype.shortMonth = function() { | |
return DateTime.shortMonths[this._date.getUTCMonth()]; | |
} | |
DateTime.prototype.localLongMonth = function() { | |
return DateTime.months[this._date.getMonth()]; | |
} | |
DateTime.prototype.longMonth = function() { | |
return DateTime.months[this._date.getUTCMonth()]; | |
} | |
DateTime.prototype.amPm = function() { | |
var hrs = this._date.getUTCHours(); | |
return hrs >= 12 ? "PM" : "AM"; | |
} | |
DateTime.prototype.day = function() { | |
return this._date.getUTCDay() + 1; | |
} | |
DateTime.prototype.localLongDay = function() { | |
return DateTime.daysOfWeek[this._date.getDay()]; | |
} | |
DateTime.prototype.longDay = function() { | |
return DateTime.daysOfWeek[this._date.getUTCDay()]; | |
} | |
DateTime.prototype.localShortDay = function() { | |
return DateTime.shortDaysOfWeek[this._date.getDay()]; | |
} | |
DateTime.prototype.shortDay = function() { | |
return DateTime.shortDaysOfWeek[this._date.getUTCDay()]; | |
} | |
DateTime.prototype.getTimezoneOffset = function() { | |
return this._date.getTimezoneOffset(); | |
} | |
DateTime.getTimezoneOffset = function() { | |
if (!this.offset) { | |
this.offset = new Date().getTimezoneOffset(); | |
} | |
return this.offset; | |
} | |
if (typeof module !== 'undefined') module.exports = DateTime; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment