Auto populate current date on date field

Lydon
Mega Expert

Hello 

I need help auto-populating the current date on a form using onChange. 

find_real_file.png

This is my current script and I just need the current date to display on the above form , I attempted 

var fecha = new Date();
	alert(fecha);

Not sure if that is the correct way . Thank you 

find_real_file.png

 

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Try below code:

var today = new Date();
var todayStr = formatDate(today, g_user_date_format);
g_form.setValue("your_date_field", todayStr);
Best Regards
Aman Kumar

View solution in original post

7 REPLIES 7

Jaspal Singh
Mega Patron
Mega Patron

Hi Lydon,

 

Any specific reason to make it work onChange()? Is this having any dependancy on the field to populate date?

If not, you can simply pass default value to the Date field as

javascript: new GlideDateTime().getDate();

 

If so, i.e. it has dependancy then you need a combination of Script include & GlideAjax as in thread. It works on load but you can include it in your existing onChange() client script.

@Jaspal Singh 

 

I just thought it will be easier to include it with my current onChange and the reason behind that is because when the form loads, those fields on the script auto-populates on the fields with previously entered data.  But for the date, I will like for it to default to the current date. 

 

I hope that explains my ask a bit more 

 

Thank you 

You can control it onLoad() as well by simply checking if Date field is empty or not.

Something as 

if(g_form.getValue('date')!='')//repale date with field name here
{//set date code here
}
else{//ignore
}

 

@Jaspal Singh 

 

I guess my concern is why do an onLOad when I am currently doing an onChange for the other fields and it is working as intended