Datetimepicker slowness in jquery: set date and time format

109 Views Asked by At

I have a drop down list , when changing value displaying date and some other fields. Currently its executing 1s in 8GB , some other PC with 2G or 4G it takes 4s to execute the code.

My code :

 obj ="2015-06-13 11:47:27"
 var str = $.trim(obj).substr (0,$.trim(obj).lastIndexOf (" ") + 1);
 var date = str.split("-");
 var str1 = $.trim(obj).substr ($.trim(obj).lastIndexOf (" ") + 1,$.trim(obj).length);
 var time = str1.split(":");
 $("#date").datetimepicker
({
    dateFormat: 'yy-mm-dd H:i'
  }).datetimepicker("setDate",new Date(date[0],date[1]-1,date[2],time[0],time[1] ));

I try to refine this code below code is faster but i need value in date and time

$("#date").val($.datepicker.formatDate('yy-mm-dd ', $myDate));

im expecting this like

   $("#date").val($.datepicker.formatDate('yy-mm-dd hh:mm', $myDate));

Please anyone help me to improve this code.

1

There are 1 best solutions below

0
On

I'll do it in here, it's easier to set an example. What the code below does is, it parses the date to a simplified ISO 8601 Extended format subform (YYYY-MM-DDTHH:mm:ss). This requires your blank space to be replaced with a T which is done using the replace function. Then the parsed Date object is injected to the datetimepicker as value of "setDate".

obj = "2015-06-13 11:47:27";
dateobj = Date.parse(obj.replace(" ", "T"));

$("#date").datetimepicker({
    dateFormat: 'yy-mm-dd H:i'
}).datetimepicker("setDate", dateobj);