Sharon Sharon - 3 months ago 60
jQuery Question

set textbox with current date onload when using xdsoft datetimepicker

i have a textbox with xdsoft datetimepicker jquery plugin. Here is the code.

<asp:TextBox class="spacial_text calendar" ID="txtDate" runat="server"></asp:TextBox>

<script>
$(document).ready(function () {
$('#txtDate').datetimepicker({
mask: true,
timepicker: false,
format: 'Y/m/d'
});
});
</script>


i want to load the current date to textbox
txtDate
while loading the page.

i tried
$('#txtDate').val(new date());


setDate: new Date() //inside datetimepicker function- but not working

onGenerate: function (ct) {
jQuery(this).val(new Date());
} //not working


any way to set current date to textbox on load???

Answer

According to the documentation the value option will be used if set and ignore the input's value. To get the current date in the format you want I would use (d.getUTCFullYear())+'/'+(d.getUTCMonth()+1)+'/'+(d.getUTCDate()).

$(document).ready(function () {
    var d = new Date(),
        date = (d.getUTCFullYear())+'/'+(d.getUTCMonth()+1)+'/'+(d.getUTCDate());

    $('#txtDate').datetimepicker({             
        mask: true,
        timepicker: false,
        format: 'Y/m/d',
        value: date
    });           
});
Comments