EasyUI Forum
October 10, 2024, 11:37:02 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: datebox formatter & parser  (Read 8772 times)
zombie86
Newbie
*
Posts: 24


View Profile WWW Email
« 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 Sad
Logged
battlezad
Newbie
*
Posts: 44


View Profile
« Reply #1 on: November 20, 2018, 12:43:19 AM »

Your year is only 2 digit value? "18.01.15"
« Last Edit: November 20, 2018, 12:44:50 AM by battlezad » Logged
zombie86
Newbie
*
Posts: 24


View Profile WWW Email
« Reply #2 on: November 20, 2018, 07:43:02 AM »

yes it 2 digits, can you help me on this?
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 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();
      }
   }
})
Logged
zombie86
Newbie
*
Posts: 24


View Profile WWW Email
« Reply #4 on: November 21, 2018, 05:16:13 PM »

Thanks it works, the trick is to add the 2000 to year value

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!