-
-
Save madrobby/476883 to your computer and use it in GitHub Desktop.
Object.extend(Date.prototype, { | |
strftime: function(format) { | |
var day = this.getDay(), month = this.getMonth(); | |
var hours = this.getHours(), minutes = this.getMinutes(); | |
return format.gsub(/\%([aAbBcdDHiImMpSwyY])/, function(part) { | |
switch(part[1]) { | |
case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break; | |
case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break; | |
case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break; | |
case 'B': return $w("January February March April May June July August September October November December")[month]; break; | |
case 'c': return this.toString(); break; | |
case 'd': return this.getDate(); break; | |
case 'D': return this.getDate().pad(2); break; | |
case 'H': return hours.pad(2); break; | |
case 'i': return (hours === 12 || hours === 0) ? 12 : (hours + 12) % 12; break; | |
case 'I': return ((hours === 12 || hours === 0) ? 12 : (hours + 12) % 12).pad(2); break; | |
case 'm': return (month + 1).pad(2); break; | |
case 'M': return minutes.pad(2); break; | |
case 'p': return hours > 11 ? 'PM' : 'AM'; break; | |
case 'S': return this.getSeconds().pad(2); break; | |
case 'w': return this.getWeek(); break; | |
case 'y': return (this.getFullYear() % 100).pad(2); break; | |
case 'Y': return this.getFullYear().toString(); break; | |
} | |
}.bind(this)); | |
}, | |
toShort: function() { | |
return this.strftime('%Y-%m-%D'); | |
}, | |
human: function() { | |
var today = Date._today || (Date._today = Date.today().beginningOfDay()), | |
day = this.beginningOfDay(), dayAsTime = day.getTime(), todayAsTime = today.getTime(); | |
if(todayAsTime == dayAsTime){ | |
return "today"; | |
} else if(today.yesterday().getTime() == dayAsTime) { | |
return "yesterday" | |
} else if(dayAsTime > todayAsTime - (180*(24*60*60*1000))) { // half a year ago | |
return day.strftime('%b %d'); | |
} else { | |
return day.strftime('%b %d, %Y'); | |
} | |
}, | |
beginningOfDay: function() { | |
return new Date(this.getFullYear(), this.getMonth(), this.getDate()); | |
}, | |
addDays: function(days){ | |
return new Date(this.getTime() + (days*1000*60*60*24)); | |
}, | |
tomorrow: function(){ | |
return this.addDays(1); | |
}, | |
yesterday: function(){ | |
return this.addDays(-1); | |
}, | |
beginningOfMonth: function(){ | |
return new Date(this.getFullYear(), this.getMonth(), 1); | |
}, | |
endOfMonth: function(){ | |
return new Date(this.getFullYear(), this.getMonth()+1, 0); | |
}, | |
previousMonth: function(){ | |
return new Date(this.getFullYear(), this.getMonth()-1, 1); | |
}, | |
nextMonth: function(){ | |
return new Date(this.getFullYear(), this.getMonth()+1, 1); | |
}, | |
getWeek: function() { | |
var ref = new Date(this.getFullYear(), 0, 1); | |
return Math.ceil((((this - ref) / 86400000) + ref.getDay()) / 7); | |
}, | |
sundayOfWeek: function(){ | |
return this.addDays(-this.getDay()).beginningOfDay(); | |
}, | |
mondayOfWeek: function(){ | |
return this.addDays(-this.getDay()+1).beginningOfDay(); | |
}, | |
previousWeekSunday: function(){ | |
return this.addDays(-(this.getDay()+7)).beginningOfDay(); | |
}, | |
previousWeekMonday: function(){ | |
return this.addDays(-(this.getDay()+6)).beginningOfDay(); | |
}, | |
nextWeekSunday: function(){ | |
return this.addDays(7-this.getDay()).beginningOfDay(); | |
}, | |
nextWeekMonday: function(){ | |
return this.addDays(7-this.getDay()+1).beginningOfDay(); | |
}, | |
isToday: function() { | |
return this.isDate(new Date()); | |
}, | |
isDate: function(date) { | |
return ( | |
this.getFullYear() == date.getFullYear() && | |
this.getMonth() == date.getMonth() && | |
this.getDate() == date.getDate() | |
); | |
} | |
}); | |
Date.today = function(){ | |
return new Date(); | |
} |
Still though, you are releasing it and so endorsing it. It contains rookie stuff like not storing your $w
output in a variable.
It's like if a jQuery plugin author kept calling $('.foo') instead of storing it in a var :/
You should take this thang to the next level, JS Master :D
I'm not "releasing" it, I'm posting it to a paste site to share some thoughts. That's quite different. :)
On the topic of $w-- we don't call that strftime that often, so it really wouldn't make much sense to store it in extra vars and unnecessarily make the code more verbose and longer. Premature optimization.
Also, I'm a Ninja, not a Master.
My fav quote of all time for when you guys bellow "Premature optimization":
It's unacceptable to use the aforementioned "don't optimize prematurely" mantra as an excuse to write bad code, which brings me to this point. While your code should be as readable as possible, it shouldn't be blatantly un-optimized.
It's just some code we've used for 2 years now and it works nice for us -- it's not intended to be a real library... :)