Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created July 15, 2010 12:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save madrobby/476883 to your computer and use it in GitHub Desktop.
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();
}
@gf3
Copy link

gf3 commented Jul 15, 2010

Aww, I could have saved you some trouble: http://github.com/gf3/JS-strftime

@jdalton
Copy link

jdalton commented Jul 15, 2010

+1 gf3 nice (lib agnostic, checks to see if the method exists before paving, some caching.. hawt)

@madrobby
Copy link
Author

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... :)

@jdalton
Copy link

jdalton commented Jul 15, 2010

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

@madrobby
Copy link
Author

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.

@jdalton
Copy link

jdalton commented Jul 15, 2010

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment