EasyUI Forum
October 18, 2025, 02:17:51 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: transform 2 digit year to 4 digit year  (Read 13290 times)
leela
Newbie
*
Posts: 29


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


View Profile Email
« Reply #1 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();
}

}
})
Logged
leela
Newbie
*
Posts: 29


View Profile Email
« Reply #2 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!
Logged
leela
Newbie
*
Posts: 29


View Profile Email
« Reply #3 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.
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!