EasyUI Forum

General Category => General Discussion => Topic started by: leela on May 30, 2014, 07:06:44 AM



Title: transform 2 digit year to 4 digit year
Post by: leela on May 30, 2014, 07:06:44 AM
Hi,

 When the user enters a value manually in the datebox field as 2/2/14, right now the calendar points to Feb 2nd 1914.
 How can I instruct it to point to 20th century? Also if the user enters the value as 2/2/14,
 it should be transformed to mm/dd/yyyy as 02/02/2014. How can I achieve this?


Title: Re: transform 2 digit year to 4 digit year
Post by: stworthy on May 31, 2014, 07:26:20 AM
To custom the default behavior of datebox, you have to override the 'formatter' and 'parser' functions. Try the code below:
Code:
$('#dd').datebox({
formatter:function(date){
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 new Date();
var ss = (s.split('/'));
var d = parseInt(ss[0],10);
var m = parseInt(ss[1],10);
var y = parseInt(ss[2],10);
y = parseInt('2000'.substr(0, 4-String(y).length)+y);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
return new Date(y,m-1,d);
} else {
return new Date();
}

}
})


Title: Re: transform 2 digit year to 4 digit year
Post by: leela on June 02, 2014, 09:59:36 AM
Hi,

 When the user enters the value as 2/3/14 manually,
 how do I transform and display it to mm/dd/yyyy format as 02/02/2014?
 Is it possible?

Thanks in advance!


Title: Re: transform 2 digit year to 4 digit year
Post by: leela on June 02, 2014, 01:37:51 PM
I did the following to make the formatter work for both selected date from calendar, and user entered input and also apply validation for both


Code:
onHidePanel:function(){
     $(this).datebox('setValue', validateDate($(this).datebox('getValue')));
}

function validateDate(value){
if (!value){
return;
}
var date;
var ss = (value.split('/'));
var m = parseInt(ss[0],10);
var d = parseInt(ss[1],10);
var y = parseInt(ss[2],10);
y = parseInt('2000'.substr(0, 4-String(y).length)+y);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
date = new Date(y,m-1,d);
} else {
return;
}
if(isNaN(date.getFullYear())){ 
   return;
}
var s = dateFormatter(date);
return s;
}

function dateFormatter(date){
var y = date.getFullYear();
y = parseInt('2000'.substr(0, 4-String(y).length)+y);
var m = date.getMonth()+1;
var d = date.getDate();
return  (m<10?('0'+m):m) + '/' + (d<10?('0'+d):d) + '/' + y;
}

Please feel free to write your comments.