Skip to content

Instantly share code, notes, and snippets.

@raytiley
Created July 22, 2013 22:22
Show Gist options
  • Save raytiley/6058254 to your computer and use it in GitHub Desktop.
Save raytiley/6058254 to your computer and use it in GitHub Desktop.
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
Ember.Model.dataTypes[Date] = {
deserialize: function(string) {
if(!string) { return null; }
if(string.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)) {
var now = new Date(),
offset = now.getTimezoneOffset() / 60;
var offsetString = pad(Math.abs(offset), 2) + '00';
string = string + (offset > 0 ? '-' : '+') + offsetString;
}
return new Date(string);
},
serialize: function (date) {
if(!date) { return null; }
return date.toISOString();
},
isEqual: function(obj1, obj2) {
if (obj1 instanceof Date) { obj1 = this.serialize(obj1); }
if (obj2 instanceof Date) { obj2 = this.serialize(obj2); }
return obj1 === obj2;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment