Cant populate date field with date today using Javascript?

Hi

I'm trying to set a specific date field with the date today using Javascript.

But I can't get it to populate the the date field on load

$(document).on('knack-view-render.view_6', function(event, view, data) {
var d = new Date()
var StringDate = "0" + d.getDate() + "/0" + (d.getMonth()+1) + "/" + d.getFullYear();
$('#view_6-field_28-time').timepicker('setDate', StringDate);

});

the field is configured this way in the builder:

- format is dd/mm/yyyy

- time is ignored

 

I can not use the set to current date in the builder, since i use the same field in another view, where the date field should be blank on load.

view and field number are correct - have checked many times :)

First, you need to build the date using padStart to apply zeroes only where necessary.

Then, you should only need to use the .val function to set that date.
Note, commented line “alert(date);” was just to make sure the date was actually being formatted how I wanted.

$(document).on(‘knack-view-render.view_6’, function(event, scene) {
var date = new Date();
var dd = String(date.getDate()).padStart(2, ‘0’);
var mm = String(date.getMonth() + 1).padStart(2, ‘0’); //January is 0!
var yyyy = date.getFullYear();

date= mm + '/' + dd + '/' + yyyy;

//alert(date);

$(’#view_6-field_28-time’).val(date);

});

I use a function similar to this that sets a Field named “Week Ending” to the next Friday from the current date.