Skip to main content
Participant
January 9, 2023
Answered

Adobe Date compassion script

  • January 9, 2023
  • 1 reply
  • 346 views

Hi, 

 

Need some assistance with a Java Script to compare dates. 

 

Date calculation depends on another date.

Todays Date = dd/mm/yyyy  [currentDate_af_date]

Current Expiry Date = dd/mm/yyyy [currentExpiryDate_af_date]

New expiry Date = dd/mm/yyyy [expiryDate_af_date]

e.g. If Todays Date is within +/- 30 days of Current Expiry Date; New expiry Date = Current Expiry Date + 6 months.

Else if Todays Date is outside +/- 30 days of Current Expiry Date; New Expiry Date = Todays Date + 6 months.

    Correct answer Anselm Hannemann

    Hi,

     

    if you want to know more, it’s probably a good idea to also search yourself on StackOverflow a bit and try to understand the solutions. Here’s how to roughly implement it yourself but please note that this is not a perfect solution since we’re calculating the date with average days instead of running through month-tables as it would be required to calculate `+6 months` correctly:

    https://cdpn.io/pen/debug/yLqgxXd?authentication_hash=NQAzYomQPpOr

    const currentExpiryDate = new Date('2023-01-10T00:00:00');
    const inTimeframe = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
    let now = Date.now(); // Current unix timestamp
    let expiryExtensionTime = 182.625 * 24 * 60 * 60 * 1000; // + ~6 months, months don’t exist in Computer time so this is the rough estimate in days for the equal in days.
    
    if (now <= (currentExpiryDate + inTimeframe) && now >= (currentExpiryDate - inTimeframe)) {
      currentExpiryDate = currentExpiryDate + new Date(currentExpiryDate + expiryExtensionTime));
    } else {
      currentExpiryDate = now + new Date(now + expiryExtensionTime));
    }

     

    A more complete solution would be to use the Moment.js library or respectively the newer Luxon library to calculate the dates: https://moment.github.io/luxon/#/?id=luxon

    1 reply

    Anselm Hannemann
    Community Expert
    Anselm HannemannCommunity ExpertCorrect answer
    Community Expert
    January 9, 2023

    Hi,

     

    if you want to know more, it’s probably a good idea to also search yourself on StackOverflow a bit and try to understand the solutions. Here’s how to roughly implement it yourself but please note that this is not a perfect solution since we’re calculating the date with average days instead of running through month-tables as it would be required to calculate `+6 months` correctly:

    https://cdpn.io/pen/debug/yLqgxXd?authentication_hash=NQAzYomQPpOr

    const currentExpiryDate = new Date('2023-01-10T00:00:00');
    const inTimeframe = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
    let now = Date.now(); // Current unix timestamp
    let expiryExtensionTime = 182.625 * 24 * 60 * 60 * 1000; // + ~6 months, months don’t exist in Computer time so this is the rough estimate in days for the equal in days.
    
    if (now <= (currentExpiryDate + inTimeframe) && now >= (currentExpiryDate - inTimeframe)) {
      currentExpiryDate = currentExpiryDate + new Date(currentExpiryDate + expiryExtensionTime));
    } else {
      currentExpiryDate = now + new Date(now + expiryExtensionTime));
    }

     

    A more complete solution would be to use the Moment.js library or respectively the newer Luxon library to calculate the dates: https://moment.github.io/luxon/#/?id=luxon