Skip to content Skip to sidebar Skip to footer

Datepicker: Setting Input Value To Current Date

I am using this plugin for Bootstrap, for date picking http://eternicode.github.io/bootstrap-datepicker/ I have it working here FIDDLE But I dont know if this is the right way to d

Solution 1:

Your method is good.

You could, however, reduce it a couple of characters - if you'd like - to something like this:

var d = newDate(),
    output = [
        ('0' + (d.getMonth() + 1)).substr(-2), 
        ('0' + d.getDate()).substr(-2), 
        d.getFullYear()
    ].join('/');

$('.datepicker').datepicker().val(output);

Also - if you're doing more work with dates - it might be a good idea to have a look at some date libraries, like date.js or sugar.js, then you could do something like this:

$('.datepicker').val(newDate().toString('MM/dd/yyyy'));        // date.js
$('.datepicker').val(Date.create().format('{MM}/{dd}/{yyyy}')); // sugar.js

Post a Comment for "Datepicker: Setting Input Value To Current Date"