set date field based on other field selection

  • 15 February 2017
  • 6 replies
  • 52 views

Badge +5

I want to set a date/time field to the current systems dated & time when a user checks a Yes/No field. For example, "[Yes] - Equipment was collected" set the PickUpDate Field (Date/Time) with the current system date. I am not a developer so I am looking for a simply solution


6 replies

Badge +6
  1. Created a button(Check box in your case) on the form, with the action of 'JavaScript'

    Form Date.png

  2. Given the date/time control a Client ID
    dateTime Control.png
  3. Created the following function within the 'Custom JavaScript' section in Form Settings:
  1. function setDateTime()  
  2. {   
  3. var fullDate = new Date()  
  4. var twoDigitMonth = ("0" + (fullDate.getMonth() + 1)).slice(-2)  
  5. var hours = fullDate.getHours();  
  6. var minutes = fullDate.getMinutes();  
  7. var currentDate = fullDate.getDate() + "/" + twoDigitMonth + "/" + fullDate.getFullYear();  
  8.   
  9. if (hours   < 10) { hours   = "0" + hours;   }  
  10.   
  11. /** Round down to the nearest 5 minutes. This is due to us rounding the minutes but not the hours. For example, 09:58 could be rounded up to 09:60 (invalid). Instead, 09:58 becomes 09:55. 
  12. **/  
  13. minutes = minutes - 2.5;  
  14. minutes = 5 * Math.round( minutes / 5 );  
  15.   
  16. // Set the date field  
  17. NWF$('#' + fldDateClosed).val(currentDate);  
  18. // Set the time (hours + minutes) fields  
  19. NWF$('#' + fldDateClosed + ' + IMG + SELECT').val(hours);  
  20. NWF$('#' + fldDateClosed + ' + IMG + SELECT + SELECT').val(minutes);  
  21. // Set Incident Status  
  22. NWF$('#' + fldStatus).val('Completed');  
  23. }  

 


Upon previewing, and clicking the 'Now' button on the form, it fills the input with the following:

Date Time on Form.png

 

Here is how to modify the hours and minutes on the form?

Here is a sample to set time to 10 AM : 45

 

NWF$('#' + fldDate + " + IMG + SELECT").val('10 AM');

NWF$('#' + fldDate + " + IMG + SELECT + SELECT").val('45');

 

Regards
Bashya Rajan A
Badge +5

Thanks for this....I will try it. It seems a lot of work just to get a default date....but thank you

Userlevel 6
Badge +15

Another way to do this could be to have a simple single line of text field, with the "Common" variables of Current Date / Current Time. If you want it to fit into a date / time field, you'd need to run a workflow after the form is submitted in order to convert that to a date / time field. I'm not sure how simple it would be to convert it to a date / time field from a single line of text, but since it'd be in "computer speak", probably not too hard.

If you just need the date that the checkbox was checked, you could instead run a workflow when the form is submitted - and, if "Yes" is checked, the workflow could populate that field for you. I believe that simple workflows (under 5 actions) are now free, too, so this wouldn't hit that. Conditional run, get current date / time, update field, save!

Badge +5

The problem is, I need to set this date during the completion of the form. Using InfoPath, you can run rules to set fields, etc., before the form is submitted.

Badge +3

Just stumbled across this  trying to achieve the same thing, Its an old post however thought I'd  share the solution.

To capture the "current date" that an item is closed and to prevent the date being recorded if the action is left open I used an a calculated value for the "Date Closed" field. 

If(Status!="Closed","N/A",formatDate(Current Date, "dd/MMM/yyyy"))

Current date if Item closed

Badge +6

Hi

Please check this

https://community.nintex.com/thread/16781-calculate-date-based-on-another-date 

Thanks

Bashya Rajan

Reply