# Cant populate date field with date today using Javascript?

**URL:** <https://forums.knack.com/t/cant-populate-date-field-with-date-today-using-javascript/8479>\
**Category:** API & Custom Code\
**Created:** [April 2, 2020, 8:10pm UTC](https://forums.knack.com/t/cant-populate-date-field-with-date-today-using-javascript/8479 "2020-04-02T20:10:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonJakob21471](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SimonJakob21471](https://forums.knack.com/u/SimonJakob21471)\
**Post date:** [April 2, 2020, 8:10pm UTC](https://forums.knack.com/t/cant-populate-date-field-with-date-today-using-javascript/8479/1 "2020-04-02T20:10:55Z")

</div>

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

&nbsp;

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.

---

<div class="post-metadata">

**Author:** ![SimonJakob21471](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SimonJakob21471](https://forums.knack.com/u/SimonJakob21471)\
**Post date:** [April 2, 2020, 8:12pm UTC](https://forums.knack.com/t/cant-populate-date-field-with-date-today-using-javascript/8479/2 "2020-04-02T20:12:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Erik](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/erik/32/982_2.png) [@Erik](https://forums.knack.com/u/Erik)\
**Post date:** [September 23, 2021, 8:58pm UTC](https://forums.knack.com/t/cant-populate-date-field-with-date-today-using-javascript/8479/3 "2021-09-23T20:58:46Z")

</div>

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.
