EasyUI Forum
September 13, 2025, 04:12:27 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Initialize datebox with blank and format data as dd-mm-yyyy (British format)  (Read 11553 times)
thecyberzone
Full Member
***
Posts: 176



View Profile Email
« 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.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 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;
}
}
});
Logged
thecyberzone
Full Member
***
Posts: 176



View Profile Email
« Reply #2 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.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!