A couple of new date formats: 'yy' & 'd'

Hi,

First, let me say that scal has worked great for me. I hope you guys keep up the great work.

I wanted to use a couple of date formats that weren't supported in scal, so I added them in myself. The formats are 'yy' for a 2-digit year (e.g. 2008--> 08) and 'd' for a date without a leading 0 (e.g. '09' --> '9').

Maybe others will find this useful, too. I just modified the first few lines of scal.js:

Object.extend(Date.prototype, {
monthnames: ['January','February','March','April','May','June','July','August','September','October','November','December'],
daynames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
succ: function(){
var sd = new Date(this.getFullYear(),this.getMonth(),this.getDate()+1);
sd.setHours(this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());
return sd;
},
firstofmonth: function(){
return new Date(this.getFullYear(),this.getMonth(),1);
},
lastofmonth: function(){
return new Date(this.getFullYear(),this.getMonth()+1,0);
},
formatPadding: true,
format: function(f){
if (!this.valueOf()) { return ' '; }
var d = this;
var formats = {
'yyyy' : d.getFullYear(),
// custom format 'yy' : 2008 --> 08
'yy' : d.getFullYear().toString().substr(2, 2),
'mmmm': this.monthnames[d.getMonth()],
'mmm': this.monthnames[d.getMonth()].substr(0, 3),
'mm': this.formatPadding ? ((d.getMonth()).succ()).toPaddedString(2) : (d.getMonth()).succ(),
'dddd': this.daynames[d.getDay()],
'ddd': this.daynames[d.getDay()].substr(0, 3),
'dd': d.getDate().toPaddedString(2),
// custom format 'd' : 09 --> 9, 15 --> 15
'd': d.getDate(),
'hh': h = d.getHours() % 12 ? h : 12,
'nn': d.getMinutes(),
'ss': d.getSeconds(),
'a/p': d.getHours() < 12 ? 'a' : 'p'
};
// make sure to add any new formats to the string below!
return f.gsub(/(yyyy|yy|mmmm|mmm|mm|dddd|ddd|dd|d|hh|nn|ss|a\/p)/i,
function(match) { return formats[match[0].toLowerCase()]; });
}
});

Thanks,
mt

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options