Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

More fun with adding dates

Avatar

Level 10

This is driving me crazy.

I've got some code that works:

//DateTimeField1 initialisation:

$.rawValue = Num2Date( Date(), "DD-MM-YYYY")

//Button to add 365 days:

DateTimeField1.rawValue = Num2Date((Date2Num(DateTimeField1.rawValue,"DD-MM-YYYY") + 365), "DD-MM-YYYY")

I'm trying to modify this to work with a date field where the date is entered instead of initalised, and auto-calculates another date field with a date a year in the future.

So, I've tried modifying this several ways and either get nothing, or Dec 31, 1900. The following code gives that date but at least it's in the correct format.

ContactReviewDate.rawValue = Num2Date((Date2Num($.rawValue,"DD-MM-YYYY") + 365), "DD-MM-YYYY")

I've tried breaking it up with variables, different display formats, IsoDate2Num instead of Date2Num...

I've tried variations of the following, which was working for me in a form I was testing out a few months ago and can't get it going either:

var d = IsoDate2Num($)
Date2.rawValue = Num2Date(d + 365)

I'm thinking it has to do with the date formatting, but can't nail it down.

Form attached. It's in the very top section on the right - "Request Date" is the date field that gets entered and "Request Review/Closed Date" is the field that gets a year added to it.

Just below there in bright orange, is the field + button from the first code block above, which works fine.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Jono,

I have always found dates a bit of a nightmare.

if (HasValue($)) then

var vDate = Date2Num($,"YYYY-MM-DD", "en_US")

var vAddYear = vDate + 365

ContactReviewDate = Num2Date(vAddYear, "YYYY-MM-DD", "en_US")

endif

I know the locale is optional, but I find that if I have it in and the pattern set to "YYYY-MM-DD" irrespective of the pattern of the field it works.

Good luck,

Niall

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Hi Jono,

I have always found dates a bit of a nightmare.

if (HasValue($)) then

var vDate = Date2Num($,"YYYY-MM-DD", "en_US")

var vAddYear = vDate + 365

ContactReviewDate = Num2Date(vAddYear, "YYYY-MM-DD", "en_US")

endif

I know the locale is optional, but I find that if I have it in and the pattern set to "YYYY-MM-DD" irrespective of the pattern of the field it works.

Good luck,

Niall

Avatar

Level 10

Thanks Niall!

I figured it had to be the display pattern screwing things up - it was the only difference from my previous experiments, but just couldn't wrap my head around it.

I hope they make dates easier to deal with in the future...