cancel
Showing results for 
Search instead for 
Did you mean: 

How to display current date in the form?

alfresco1576
Champ on-the-rise
Champ on-the-rise

I want to display the current date in the form. So whenever you create new process instance, it should fetch the current date and display in the form. 

Issue: I am able to get and display current date but that code is running everytime I open the form and display that date. (If I create new process instance today, today's date is displaying in the form but if I open form tomorrow then it will display tomorrow date)

How can I make sure that it should only display the date when the form is created for particular process instance?

Code to get current date: 

var myDate = scope.findFormFieldWithId(scope.allFormFields, 'myDate');

var today = new Date();

var dd = String(today.getDate()).padStart(2, '0');

var mm = String(today.getMonth() + 1).padStart(2, '0');

var yyyy = today.getFullYear();

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

myDate.value = today;

Then I am using "Display text" form field to display 'myDate'

Thanks in advance.

5 REPLIES 5

EddieMay
World-Class Innovator
World-Class Innovator

Hi @alfresco1576,

Aren't you just assigning myDate to be today

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!

Hi @EddieMay 

new Date() will store current date into variable "today". 

And I am assigning it to field called "myDate". 

I need to make sure new Date() should not run everytime I open the form and store new date into variable.

sanjaybandhaniya
Elite Collaborator
Elite Collaborator

Hi,

Where you are writing above code?

Hi @sanjaybandhniya 

I am writing the code inside JavaScript in the form. 

In your code, place condition and check that your field has a value or not and base on that write your code.

var myDate = scope.findFormFieldWithId(scope.allFormFields, 'myDate');
if(myDate.value==null){
	var today = new Date();
	var dd = String(today.getDate()).padStart(2, '0');
	var mm = String(today.getMonth() + 1).padStart(2, '0');
	var yyyy = today.getFullYear();
	today = mm + '/' + dd + '/' + yyyy;
	myDate.value = today;
}