Skip to content

Instantly share code, notes, and snippets.

@johnnncodes
Forked from jayuen/gist:6787780
Created March 13, 2016 01:42
Show Gist options
  • Save johnnncodes/2275096cd3409da2ee73 to your computer and use it in GitHub Desktop.
Save johnnncodes/2275096cd3409da2ee73 to your computer and use it in GitHub Desktop.
DateWithTimezone
define("DateWithTimezone", function(module) {
var DateWithTimezone = function(moment){
this.moment = moment
moment.tz(DateWithTimezone.getTimezone())
}
_.extend(DateWithTimezone.prototype, {
toISO: function(){
return this.format()
},
format: function(format) {
return this.moment.format(format)
},
localEquivalent: function(){
return moment(this.format(), "YYYY-MM-DDTHH:mm:ss").toDate()
},
add: function(field, value) {
var cloned = this.moment.clone()
cloned.add(field, value)
return new DateWithTimezone(cloned)
},
hours: function() {
return this.moment.hours()
},
minutes: function() {
return this.moment.minutes()
},
isLaterThan: function(compareTo) {
return this.moment.valueOf() > compareTo.moment.valueOf()
}
})
_.extend(DateWithTimezone, {
fromISO: function(dateTimeString){
return new DateWithTimezone(moment(dateTimeString))
},
fromLocalEquivalent: function(localEquivalent){
var strWithoutTimezone = moment(localEquivalent).format("YYYY-MM-DDTHH:mm:ss")
var timezone = moment.tz(DateWithTimezone.getTimezone()).format("Z")
return new DateWithTimezone(moment(strWithoutTimezone + timezone))
},
getTimezone: function(){
return DateWithTimezone.timezone || "Etc/UTC"
},
now: function(){
return new DateWithTimezone(moment())
}
})
module.exports = DateWithTimezone
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment