Tuesday, April 15, 2014

jquery-simple-datetimepicker appendDtpicker to get current date from textbox

jquery-simple-datetimepicker appendDtpicker to get current date from textbox      

   $(function () {            
             $('#img_strdate').click(function () {   
//get the date from text box txtStrDate and save the date in YYYY-MM-DD HH:MM format              
                 var str2 = new Date();;
                 if ($("#<%=txtStrDate.ClientID %>").val() != null && $("#<%=txtStrDate.ClientID %>").val() != "") {
                     str2 = $("#<%=txtStrDate.ClientID %>").val();
                 }
                 var re = /^(\d{1,2})[-/](\d{1,2})[-/](\d{2,4}) (\d{1,2}):(\d{1,2})$/;
                 var m = re.exec(str2);
                 var currentDate = m[3]+'-'+m[2]+'-'+m[1] + ' ' + m[4]+':'+m[5];
                 $("#<%=txtStrDate.ClientID %>").val('');
                 $("#<%=txtStrDate.ClientID %>").appendDtpicker({
                     "dateFormat": "DD/MM/YYYY hh:mm",                
                     //"dateOnly": true,                       
                     //"current": '2012-01-01 00:00',
              //get the current date of calender will be updated from text box. And it will not go to today's date
                     "current": currentDate,
                     "onInit": function (handler) {                        
                         handler.show();                         
                     },                   
                     "onHide": function (handler) {
                         var str1 = $("#<%=txtStrDate.ClientID %>").val();
                         var d = Date.parseExact(str1, 'dd/MM/yyyy H:mm');
                         var no_days = parseInt($("#<%=txt_dur.ClientID %>").val());
                         // window.alert(d);              
                         if ($("#<%=rd_yes.ClientID %>").is(':checked')) {
                             $("#<%=txtEndDate.ClientID %>").val(d.add({ days: no_days }).toString('dd/MM/yyyy 23:59'));
                         }
                         else {
                             $("#<%=txtEndDate.ClientID %>").val(d.add({ minutes: no_days }).toString('dd/MM/yyyy HH:mm'));
                         }
                         handler.destroy();
                     }
                 });
             });

The Datetimepicker source location: https://github.com/mugifly/jquery-simple-datetimepicker