How to display current date in the form?

cancel
Showing results for 
Search instead for 
Did you mean: 
alfresco1576
Active Member

How to display current date in the form?

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
EddieMay
Alfresco Employee

Re: How to display current date in the form?

Hi @alfresco1576,

Aren't you just assigning myDate to be today

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

Re: How to display current date in the form?

Hi,

Where you are writing above code?

alfresco1576
Active Member

Re: How to display current date in the form?

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.

alfresco1576
Active Member

Re: How to display current date in the form?

Hi @sanjaybandhniya 

I am writing the code inside JavaScript in the form. 

sanjaybandhniya
Intermediate

Re: How to display current date 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;
}