EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: galcott on March 24, 2023, 08:39:42 AM



Title: Default century in datebox
Post by: galcott on March 24, 2023, 08:39:42 AM
I have dateboxes using US format (MM/DD/YYYY). If the user doesn't use the calendar, and types in the year as 2 digits, it defaults to 20th century dates (1923 instead of 2023). This seems like a bug. Can it be fixed?


Title: Re: Default century in datebox
Post by: galcott on March 29, 2023, 02:10:46 PM
I posted this 5 days ago and haven't had an answer. Can you please reply? Thanks.

I have dateboxes using US format (MM/DD/YYYY). If the user doesn't use the calendar, and types in the year as 2 digits, it defaults to 20th century dates (1923 instead of 2023). This seems like a bug. Can it be fixed?


Title: Re: Default century in datebox
Post by: jarry on March 29, 2023, 07:23:11 PM
This is the default behaviour of the javascript object Date. To solve this issue, please override the $.fn.datebox.defaults.parser function to fill with '20' as the prefix while typing only 2 digital number.
Code:
$.extend($.fn.datebox.defaults, {
parser: function (s) {
var CDate = $.fn.calendar.defaults.Date;
if ($(this).data('datebox')) {
CDate = $(this).datebox('calendar').calendar('options').Date;
}
if (!s) return new CDate();
var ss = s.split('/');
var m = parseInt(ss[0], 10);
var d = parseInt(ss[1], 10);
var y = parseInt(ss[2], 10);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
if (y < 100) {
y = 2000 + y;
}
return new CDate(y, m - 1, d);
} else {
return new CDate();
}
}
})