Default Current Date & Time into a Field with Enate's Advanced Custom Cards

Code and walkthrough of how you can default the current system date/time into a custom data field onto your Card.

The following code can be added to a custom card to set the default value of a date/time field to the current date/time. In order to do this we need to use a third party object called 'moment' which lives in your browser, and will return the current datetime if we include it as a variable at the beginning of our code. This sample will assume the following:

  • You have a custom card open

  • It is set to 'Customised' in order to show the additional code tabs

  • You have added a DateTime Field to your card - here we will give it a name of 'CurrentDate' - you will have to adjust this name of the field referenced in your code in step 2 in order to match with your own specific field's name.

When you set your card to 'Customised', all the previously auto-generated for the standard form layout remains for your use, so there are only two small lines of code to add, in two places. Follow the steps below:

1. Declare 'moment' as a vairable In the Typescript tab, add the following line of code immediately after line 8, to declare this 'moment' as a variable for subsequent use:

declare var moment; // Include moment in TypeScript

It should look like this in your code..

2. Add your custom code in the dedicated section to set your field value to the 'moment' value. Further down in the Typescript code, add the following line immediately after the '//YOUR CUSTOM CODE BEGINS' line:

this.Packet.DataFields['CurrentDate'] = moment().format("YYYY-MM-DD")+'T00:00:00';

It should look like this in your code..

Note that you can adjust the date format to fit your needs.

Save your card, and you're finished.

Check out our Advanced Custom Cards section for more information about what they are and how to use them:

Last updated