Created
January 22, 2013 14:17
-
-
Save henrahmagix/4594946 to your computer and use it in GitHub Desktop.
A lightweight alternative to the Date object when only dealing with time in hours and minutes
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 TimeInMinutes = function(hours, minutes) { | |
return this.init(hours, minutes); | |
}; | |
TimeInMinutes.prototype = { | |
hours: 0, | |
minutes: 0, | |
days: 0, | |
time: 0, | |
init: function(hours, minutes) { | |
if (hours) this.hours = hours; | |
if (minutes) this.minutes = minutes; | |
this.refresh(true); | |
return this; | |
}, | |
refresh: function(refreshTime) { | |
var normalised = this.normaliseTime(this.time); | |
if (refreshTime) { | |
this.time = (this.days * 24 * 60) + (this.hours * 60) + this.minutes; | |
} | |
this.hours = parseInt(normalised / 60); | |
this.minutes = normalised % 60; | |
this.updateDays(); | |
}, | |
updateDays: function() { | |
this.days = parseInt(this.time / 1440); | |
if (this.time < 0) { | |
this.days -= 1; | |
} | |
}, | |
resetDays: function() { | |
this.setDays(0); | |
}, | |
normaliseTime: function(time) { | |
if (time < 0) { | |
time += 1440; | |
time = this.normaliseTime(time); | |
} | |
return time; | |
}, | |
setHours: function(hours) { | |
this.hours = hours; | |
this.refresh(true); | |
}, | |
setMinutes: function(minutes) { | |
this.minutes = minutes; | |
this.refresh(true); | |
}, | |
setTime: function(time) { | |
this.time = time; | |
this.refresh(false); | |
}, | |
setDays: function(days) { | |
this.days = days; | |
this.refresh(true); | |
}, | |
getHours: function() { | |
return this.hours; | |
}, | |
getMinutes: function() { | |
return this.minutes; | |
}, | |
getTime: function() { | |
return this.time; | |
}, | |
getDays: function() { | |
return this.days; | |
}, | |
toString: function() { | |
return this.hours + ':' + this.minutes; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment