The question came up about using "European" dates (day/month/year) rather than "American" dates (month/day/year) in flexcal
. The biggest problem is that the built-in Date.parse()
(which is used by new Date(string)
) uses the American format, at least with my browsers and settings.
The code in flexcal
that does the formatting are in the following methods:
format({Date})
- Converts a
Date
into aString
. Used for external formatting: it determines what string is put in the input element after the user selects a date. _date2string({Date})
- Converts a
Date
into aString
. Used for internal formatting: therel
attribute of each day link in the calendar is set with this. _createDate(d, oldd)
- Attempts to coerce
d
into aDate
. If it is a validDate
, just returns that. Otherwise returnsnew Date(d)
. If that does not create a validDate
, returnsoldd
. this._createDate(this._date2string(d))
must equald
in order for the calendar to work, andthis._createDate(this.format(d))
must equald
for the calendar to be able to parse the string in the input element.
So to use European dates, we have to replace each of those methods. I'll use the "dd.mm.yyyy" format.
<input id="eurodate" value="01.02.2014" /> Should start as February first, not January second.
function euroformat (d){
return d.getDate()+"."+(d.getMonth()+1)+"."+d.getFullYear();
}
$.widget('bililite.euroflexcal', $.bililite.flexcal, {
format: euroformat,
_date2string: euroformat,
_createDate: function (d, oldd){
// converts d into a valid date
oldd = oldd || new Date;
if (!d) return oldd;
if (typeof d == 'string' && d.match(/(\d+)\.(\d+)\.(\d+)/)){
d = RegExp.$2 + '/' + RegExp.$1 + '/' + RegExp.$3; // convert to American style
}
if (!(d instanceof Date)) d = new Date(d);
if (isNaN(d.getTime())) return oldd;
return d;
}
});
$('#eurodate').euroflexcal();