EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: thecyberzone on February 19, 2015, 09:53:28 AM



Title: Initialize datebox with blank and format data as dd-mm-yyyy (British format)
Post by: thecyberzone on February 19, 2015, 09:53:28 AM
Although there were so many threads exist in this forum with this discussion, just I am adding a small request to the developer. To use a databox with dd-mm-yyyy format just I have used following parser and formatter, everything is working fine except whenever a blank date is parsed/formatted within this datebox it always puts current date, (It should be, as per my code it is initialized with new Date), but what I require is to initialized with a blank date. The code I have used is :

Code:
	$.fn.datebox.defaults.formatter = function(date){
if (!date){return ' ';}
var y = date.getFullYear();
var m = date.getMonth()+1;
var d = date.getDate();
return (d<10?('0'+d):d)+'-'+(m<10?('0'+m):m)+'-'+y;
};
$.fn.datebox.defaults.parser = function(s){
if (!s) return new Date();
var ss = s.split('-');
var d = parseInt(ss[0],10);
var m = parseInt(ss[1],10);
var y = parseInt(ss[2],10);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
return new Date(y,m-1,d);
} else {
return new Date();
}
};

Now what I have done in above code, in parser instead of returning simple new Date() I have used new Date(0,0,0) and in formatter function I have check that
Code:
if(date==Date(0,0,0)) {return ' ';}
, but his does not work, instead it returns 31-12-1899 !!! I don't know why.

So please anybody suggest me a way by which I can display a blank date in datebox when it is empty/blank.


Title: Re: Initialize datebox with blank and format data as dd-mm-yyyy (British format)
Post by: stworthy on February 20, 2015, 06:13:23 AM
The date 'new Date(0,0,0)' is not a blank date. Please try to use 'null' value instead. To let the 'parser' function work correctly, the calendar's 'moveTo' method should also be overridden.
Code:
$.extend($.fn.calendar.methods, {
moveTo: function(jq, date){
return jq.each(function(){
if (!date){
var now = new Date();
$(this).calendar({
year: now.getFullYear(),
month: now.getMonth()+1,
current: date
});
return;
}
var opts = $(this).calendar('options');
if (opts.validator.call(this, date)){
var oldValue = opts.current;
$(this).calendar({
year: date.getFullYear(),
month: date.getMonth()+1,
current: date
});
if (!oldValue || oldValue.getTime() != date.getTime()){
opts.onChange.call(this, opts.current, oldValue);
}
}
});
}
});
$.extend($.fn.datebox.defaults, {
formatter: function(date){
if (!date){return ' ';}
var y = date.getFullYear();
var m = date.getMonth()+1;
var d = date.getDate();
return (d<10?('0'+d):d)+'-'+(m<10?('0'+m):m)+'-'+y;
},
parser: function(s){
if (!s) return null;
var ss = s.split('-');
var d = parseInt(ss[0],10);
var m = parseInt(ss[1],10);
var y = parseInt(ss[2],10);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
return new Date(y,m-1,d);
} else {
return null;
}
}
});


Title: Re: Initialize datebox with blank and format data as dd-mm-yyyy (British format)
Post by: thecyberzone on February 20, 2015, 09:19:16 AM
Thanks for your help, after adding moveTo method to the Calender, it works perfectly as I wanted.