Created
March 20, 2012 23:04
-
-
Save thomas4g/2142245 to your computer and use it in GitHub Desktop.
Date Prettier
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
var DATE_PRETTIFIER = { | |
date: null, | |
parse: function(input) { | |
if(input.length >= 8) { | |
var day, month, year; | |
day = parseInt(input.substring(6,8), 10); | |
month = parseInt(input.substring(4,6), 10); | |
year = parseInt(input.substring(0,4), 10); | |
this.date = new Date(year, month, day); | |
} | |
else | |
{ | |
throw new Error("Invalid Format"); | |
} | |
}, | |
slash: function() { | |
return this.date.getDate() + "/" + this.date.getMonth() + "/" + this.date.getFullYear(); | |
}, | |
pretty: function() { | |
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
return months[this.date.getMonth() - 1] + " " + this.date.getDate() + ", " + this.date.getFullYear(); | |
} | |
}; | |
var pretty = Object.create(DATE_PRETTIFIER); //in case of non-ES5-supporting browsers, scrap this line and replace it with var pretty = DATE_PRETTIFIER or something | |
pretty.parse("20120502"); | |
console.log(pretty.slash()); //gives "2/5/2012" | |
console.log(pretty.pretty()); //gives "May 2, 2012" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment