EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: A-K on July 20, 2014, 12:22:52 PM



Title: Datetimebox format - day/month/year HH:MM:SS
Post by: A-K on July 20, 2014, 12:22:52 PM
Hey,
Im trying to change how the date is shown in the datetimebox but I have a few problems,
As I have noticed when I created the formatter and created the format above it didnt work and when I checked why I saw
that easyui tries to parse the string he gets from the formatter and since the Date() in js doesnt know how to parse it
it just return new Date() in the end. So i overrided the parse function but then I found a problem that I cant create a date with UTC + 2.

Any help with this will be much appreciated.

Thanks, Alon.


Title: Re: Datetimebox format - day/month/year HH:MM:SS
Post by: jarry on July 20, 2014, 05:48:08 PM
The simple implementation to parse 'day/month/year HH:MM:SS' datetime format shows as below:
Code:
$('#dt').datetimebox({
  formatter:function(date){
    var s1 = [date.getDate(),date.getMonth()+1,date.getFullYear()].join('/');
    var s2 = [date.getHours(),date.getMinutes(),date.getSeconds()].join(':');
    return s1 + ' ' + s2;
  },
  parser:function(s){
    if (!s){return new Date();}
    var dt = s.split(' ');
    var date = new Date(dt[0][2],dt[0][1],dt[0][0]);
    if (dt.length>1){
      date.setHours(dt[1][0]);
      date.setMinutes(dt[1][1]);
      date.setSeconds(dt[1][2]);
    }
    return date;
  }
})


Title: Re: Datetimebox format - day/month/year HH:MM:SS
Post by: A-K on July 21, 2014, 06:44:33 AM
The simple implementation to parse 'day/month/year HH:MM:SS' datetime format shows as below:
Code:
$('#dt').datetimebox({
  formatter:function(date){
    var s1 = [date.getDate(),date.getMonth()+1,date.getFullYear()].join('/');
    var s2 = [date.getHours(),date.getMinutes(),date.getSeconds()].join(':');
    return s1 + ' ' + s2;
  },
  parser:function(s){
    if (!s){return new Date();}
    var dt = s.split(' ');
    var date = new Date(dt[0][2],dt[0][1],dt[0][0]);
    if (dt.length>1){
      date.setHours(dt[1][0]);
      date.setMinutes(dt[1][1]);
      date.setSeconds(dt[1][2]);
    }
    return date;
  }
})

Thanks so much for the answer! It helped a lot!
I would like to add a little fix to the code above because it didnt work right away
In the parser there should be another 2 splits and the parser should look like this:

parser:function(s){
    if (!s){return new Date();}
    var dt = s.split(' ');
    var dateFormat = dt[0].split('/');
    var timeFormat = dt[1].split(':');
    var date = new Date(dateFormat[2],dateFormat[1],dateFormat[0]);
    if (dt.length>1){
      date.setHours(timeFormat[0]);
      date.setMinutes(timeFormat[1]);
      date.setSeconds(timeFormat[2]);
    }
    return date;
  }

Then everything works just fine!
Thanks.