Search code examples
javascriptautomationafter-effects

Javascript: After Effects and Date() function returns undefined


Here's the situation.

  1. My team produces an announcement video that plays every Wednesday. The intro is always the same, except for the date (text layer as "[Long Month] xx, 20xx").
  2. I'm trying to write some simple automation that makes the date always the correct Wednesday, regardless of export date (before Wednesday forces date ahead, after Wednesday forces date ahead to next Wednesday).

My code works in a browser, but it's not working in After Effects. In AE it returns as undefined. Help! (Running After Effects 14.2.1.34)

var today, day, date, month

today = new Date(Date(0));
day = today.getDay();
date = today.getDate();
month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];


function printDate() {
  return month[today.getMonth()] + " " + date + ", " + today.getFullYear();
}


function forceWednesday() {
  if (day === 3) {
    printDate();
  } else if (day < 3) {
    var x = day;
    x = 3 - day;
    return month[today.getMonth()] + " " + (date + x) + ", " + today.getFullYear();
  } else if (day > 3) {
    var dayDiff, dateDiff, nextWed, newDate, wed
    dayDiff = day - 3;
    dateDiff = date - dayDiff;
    nextWed = today.setDate(dateDiff + 7);
    newDate = new Date(nextWed);
    wed = newDate.getDate();
    return month[newDate.getMonth()] + " " + wed + ", " + newDate.getFullYear();
  }
}

forceWednesday();


Solution

  • Fixed it. See it live on Github.