EasyUI Forum
April 19, 2024, 06:49:58 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Issue with datetimebox  (Read 4505 times)
fonzie
Newbie
*
Posts: 9


View Profile
« on: May 16, 2017, 09:53:56 AM »

I am wanting the easyui-datetimebox to display the date and time in UK format which is "dd mm yyyy", for example today would be displayed as "16/5/2017 17:46:53".

I can get this to work (code below), but whenever I click on the calendar icon in the textbox to open the small calendar, the time displayed at the bottom is always "00:00:00" and even if I change it to something else, it doesn't get updated correctly, the current time replaces it (which I realise is because of my code).



This page/site is for UK usage only, therefore if I need to change the original source code to reflect UK dates, that won't be an issue.

Code :-
<input class="easyui-datetimebox" value="16/5/2017" data-options="label:'mm/dd/YYYY',labelPosition:'top',formatter:ukformatter,parser:ukparser" style="width:100%;">

<script type="text/javascript">
 function ukformatter(date){
       var y = date.getFullYear();
             var m = date.getMonth() + 1;
             var d = date.getDate();
            //var t = date.GetTime();
           var dt = new Date();
          var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
            
            
            // date format dd/mm/yyy
             var r = (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y + ' ' + time;
            //var r = (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y
             return r;
   }
   function ukparser(s){
   if (!s) {
                 return new Date();
             }
            // date format dd/mm/yyyy
             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();
             }
         }
         

</script>


Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: May 17, 2017, 01:04:24 AM »

The 'ukformatter' and 'ukparser' functions should be defined as:
Code:
function ukformatter(date){
var y = date.getFullYear();
var m = date.getMonth()+1;
var d = date.getDate();
var h = date.getHours();
var M = date.getMinutes();
var s = date.getSeconds();
function formatNumber(value){
return (value < 10 ? '0' : '') + value;
}
return formatNumber(d)+'/'+formatNumber(m)+'/'+y+
' '+formatNumber(h)+':'+formatNumber(M)+':'+formatNumber(s);
}
function ukparser(s){
if ($.trim(s) == ''){
return new Date();
}
var dt = s.split(' ');
var ss = dt[0].split('/');
var d = parseInt(ss[0],10);
var m = parseInt(ss[1],10)-1;
var y = parseInt(ss[2],10);
if (dt.length >= 2){
var tt = dt[1].split(':');
var hour = parseInt(tt[0], 10) || 0;
var minute = parseInt(tt[1], 10) || 0;
var second = parseInt(tt[2], 10) || 0;
} else {
var hour = 0;
var minute = 0;
var second = 0;
}
return new Date(y, m, d, hour, minute, second);
}
« Last Edit: May 17, 2017, 05:40:16 PM by stworthy » Logged
fonzie
Newbie
*
Posts: 9


View Profile
« Reply #2 on: May 17, 2017, 03:22:31 AM »

Thank you for the help, but unfortunately the code isn't working correctly.

The time is now displayed at the bottom of the calendar is now correct, but if I choose a different time, this isn't reflected in the text area, it also isn't choosing the correct date, if I choose today, instead of displaying "17/05/2017" it shows "17/06/2017".



Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 on: May 17, 2017, 05:46:01 PM »

Please use the 'formatter' and 'parser' functions in the previous post. This is an example that works fine.
http://code.reloado.com/eqilas/edit#javascript,html
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!