Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

How to make date field reflect current year if user enters only m/d?

Avatar

Level 1

I have a date field that needs to act like an Excel cell; e.g., if user enters 7/21, the date displays the current year as well(7/21/15).  I can't figure out how to use edit patterns to do this (and I'm not very familiar with FormCalc or JS).  Thank you!

1 Reply

Avatar

Level 10

Hi,

Try this code in the exit event of the date field.

if (this.formattedValue == this.rawValue) {

  var parts = this.rawValue.split('/');

  if (parts.length == 2) {

  var d = new Date()

  d.setMonth(parts[0]-1, parts[1]);

  this.rawValue = util.printd('yyyy-mm-dd', d);

  }

}

The this.formattedValue == this.rawValue part is a way of testing for an invalid date, if it was value was valid then the rawValue would be in the data format which defaults to yyyy-mm-dd, if its not valid then the invalid value gets copied across. Then we split the value into two and update the current date with the day and month and update the field.

Bruce