EasyUI Forum

General Category => General Discussion => Topic started by: zombie86 on November 19, 2018, 07:25:22 PM



Title: datebox formatter & parser
Post by: zombie86 on November 19, 2018, 07:25:22 PM
Dear All,

am trying to use the following:
        function myformatter(date){
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            return y.toString().substr(-2)+'.'+(m<10?('0'+m):m)+'.'+(d<10?('0'+d):d);
        }
        function myparser(s){
            if (!s) return new Date();
            var ss = (s.split('.'));
            var y = parseInt(ss[0],10);
            var m = parseInt(ss[1],10);
            var d = parseInt(ss[2],10);
            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
                return new Date(d,m-1,y);
            } else {
                return new Date();
            }
        }

its working properly when I choose a date from the calendar, but the issue occurs once I try to write the date directly to datebox field, it will not change to appropriate date such as typing "18.01.15".....

please help me to solve this issue :(


Title: Re: datebox formatter & parser
Post by: battlezad on November 20, 2018, 12:43:19 AM
Your year is only 2 digit value? "18.01.15"


Title: Re: datebox formatter & parser
Post by: zombie86 on November 20, 2018, 07:43:02 AM
yes it 2 digits, can you help me on this?


Title: Re: datebox formatter & parser
Post by: stworthy on November 20, 2018, 05:32:23 PM
Please try this code:
Code:
$('#db').datebox({
   formatter: function(date){
      var y = date.getFullYear();
      var m = date.getMonth()+1;
      var d = date.getDate();
      return y.toString().substr(-2)+'.'+(m<10?('0'+m):m)+'.'+(d<10?('0'+d):d);
   },
   parser: function(s){
      if (!s) return new Date();
      var ss = (s.split('.'));
      var y = parseInt(ss[0],10)+2000;
      var m = parseInt(ss[1],10);
      var d = parseInt(ss[2],10);
      if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
          return new Date(y,m-1,d);
      } else {
          return new Date();
      }
   }
})


Title: Re: datebox formatter & parser
Post by: zombie86 on November 21, 2018, 05:16:13 PM
Thanks it works, the trick is to add the 2000 to year value