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

Countdown date to deadline

Avatar

Level 3

How do I implement a countdown date to a specified dealine which will show on the form and auto adjust to NY timezone?

The field should show days left to submission deadline.

1 Accepted Solution

Avatar

Correct answer by
Level 6

I don't believe that there is timezone info available in FormCalc, so I worked this out in javascript.

Javascript is able to get the local system time and timezone of the machine that the script is running on.  There is no way to obtain the timezone for NY from a random system, so it needs to be set as a constant in the script.  This will change from GMT-4 to GMT-5 depending on whether daylight savings time is in force, but you might want to ignore the hour difference.

Here's the script to do the calculation.  You will just need to change the NY_TARGET_DATE constant to whatever date you want.

// The following two constants must be set for your current situation

// NY_TIME_ZONE_DATE_OFFSET = timezone difference between GMT and NY

// NY_TARGET_DATE = the deadline date

var NY_TIME_ZONE_OFFSET = -4;

var NY_TARGET_DATE = "4/8/2010";

var ONE_HOUR_IN_MS = 1000*60*60;

var ONE_DAY_IN_MS = ONE_HOUR_IN_MS * 24;

var nyTimeZoneOffsetMs = NY_TIME_ZONE_OFFSET * ONE_HOUR_IN_MS;

// Obtain target NY date/time as milliseconds

var targetNYDate = new Date(NY_TARGET_DATE);

var targetNYTimeMs = targetNYDate.getTime();

// Get current local date/time from the system as milliseconds

var currentLocalDate = new Date();

var currentLocalTimeMs = currentLocalDate.getTime();

// Get offset of local system timezone from UTC. This

// will be in minutes; convert to milliseconds by multiplying

// by 60000 (60 sec/minute, 1000ms/second)

var localTzOffset = currentLocalDate.getTimezoneOffset() * 60000;

// Add local timezone offset to current local time to convert our local time to UTC

var utcDate = new Date(currentLocalTimeMs += localTzOffset);

// Calculate current NY time by adding NY offset to UTC

var currentNYTimeMs = utcDate.getTime()+ nyTimeZoneOffsetMs;

// Get difference between the target NY date/time

// and current NY date/time in milliseconds

var timeDifferenceMs = targetNYTimeMs - currentNYTimeMs

// Divide by number of milliseconds in a day to get number of days.

// Math.ceil will round up. Will return a positive number of days until

// target date; zero if same day as target date, and

// negative number if past the target date.

var numDays = Math.ceil(timeDifferenceMs / ONE_DAY_IN_MS);

this.rawValue = numDays;

View solution in original post

6 Replies

Avatar

Correct answer by
Level 6

I don't believe that there is timezone info available in FormCalc, so I worked this out in javascript.

Javascript is able to get the local system time and timezone of the machine that the script is running on.  There is no way to obtain the timezone for NY from a random system, so it needs to be set as a constant in the script.  This will change from GMT-4 to GMT-5 depending on whether daylight savings time is in force, but you might want to ignore the hour difference.

Here's the script to do the calculation.  You will just need to change the NY_TARGET_DATE constant to whatever date you want.

// The following two constants must be set for your current situation

// NY_TIME_ZONE_DATE_OFFSET = timezone difference between GMT and NY

// NY_TARGET_DATE = the deadline date

var NY_TIME_ZONE_OFFSET = -4;

var NY_TARGET_DATE = "4/8/2010";

var ONE_HOUR_IN_MS = 1000*60*60;

var ONE_DAY_IN_MS = ONE_HOUR_IN_MS * 24;

var nyTimeZoneOffsetMs = NY_TIME_ZONE_OFFSET * ONE_HOUR_IN_MS;

// Obtain target NY date/time as milliseconds

var targetNYDate = new Date(NY_TARGET_DATE);

var targetNYTimeMs = targetNYDate.getTime();

// Get current local date/time from the system as milliseconds

var currentLocalDate = new Date();

var currentLocalTimeMs = currentLocalDate.getTime();

// Get offset of local system timezone from UTC. This

// will be in minutes; convert to milliseconds by multiplying

// by 60000 (60 sec/minute, 1000ms/second)

var localTzOffset = currentLocalDate.getTimezoneOffset() * 60000;

// Add local timezone offset to current local time to convert our local time to UTC

var utcDate = new Date(currentLocalTimeMs += localTzOffset);

// Calculate current NY time by adding NY offset to UTC

var currentNYTimeMs = utcDate.getTime()+ nyTimeZoneOffsetMs;

// Get difference between the target NY date/time

// and current NY date/time in milliseconds

var timeDifferenceMs = targetNYTimeMs - currentNYTimeMs

// Divide by number of milliseconds in a day to get number of days.

// Math.ceil will round up. Will return a positive number of days until

// target date; zero if same day as target date, and

// negative number if past the target date.

var numDays = Math.ceil(timeDifferenceMs / ONE_DAY_IN_MS);

this.rawValue = numDays;

Avatar

Level 3

Kevin,

Is there a method for setting the deadline date by the user for your script?

This is due to the changing deadlines based on events.

I am interested in a drop-down date field which will update the deadline date in the script provided.

This script works perfectly by the way, thank you very much.

Avatar

Level 6

Yes, not a problem to implement using a date from a dropdown.  I'll post an updated script shortly; a little busy now...

Avatar

Level 3

Thank you Kevin,

I look forward to the script.

Avatar

Level 3

Kevin,

Will you have time to provide the code to modify the deadline date within the script in the next day or two?

Thank you for your time,

Avatar

Level 6

I have an updated example but it looks like file attachments have been disabled again.  Send me a private message with your email address and I'll forward the example.