Created
October 2, 2013 01:22
-
-
Save jayuen/6787780 to your computer and use it in GitHub Desktop.
DateWithTimezone
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
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