Friday, 26 July 2013

Set Default Current Date and Time To a Text Box by jQuery

Set Default Date Time to a Text Box



Code :-.....

<div id="left">
    From : <input type="text" name="FromDate" id="FromDate" style="width:130px"/>
</div>


<script type="text/javascript">
   $(function () {
     $("#FromDate").datetimepicker({ dateFormat: 'dd-M-yy' });
     $("#FromDate").datetimepicker('setDate', new Date());
     $('#Time').val('24');
   });

</script>

In Red Color  "$("#FromDate").datetimepicker({ dateFormat: 'dd-M-yy' });" To initialize the DateTime picker.

In Blue Color "$("#FromDate").datetimepicker('setDate', new Date());" To Set the Current Date and Time in the Text Box

"$('#Time').val('24');"  This is to Set a default Value.

We can also write a single line function to set the current date and time as a default value

$("#FromDate").datetimepicker({ dateFormat: 'dd-M-yy' }).datetimepicker('setDate', new Date())
 

No comments:

Post a Comment