Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Year countdown timer expression problem

  • Year countdown timer expression problem

    Posted by Graham Harris on September 16, 2017 at 10:58 am

    I’m trying to create a timer that countdown from 1 year and displays in years, months, days, minutes and seconds. I found a suitable expression on an adobe forum but when run it populates the day, minute and second fields with NA. The expression is below. Any help in getting this to work would be gratefully received. As you can guess I know just enough about expressions to make myself look stupid.

    totalTimeInSeconds = 31400000;
    secondsPerMinute = 60;
    secondsPerHour = 60 * 60;
    secondsPerDay = 60 * 60 * 24;
    secondsPerMonth = 60 * 60 * 24 * 30;
    secondsPerYear = 60 * 60 * 24 * 30 * 12;

    secondsRemaining = totalTimeInSeconds;

    years = Math.floor(secondsRemaining / secondsPerYear);
    secondsRemaining = secondsRemaining % years;

    months = Math.floor(secondsRemaining / secondsPerMonth);
    secondsRemaining = secondsRemaining % months;

    days = Math.floor(secondsRemaining / secondsPerDay);
    secondsRemaining = secondsRemaining % days;

    hours = Math.floor(secondsRemaining / secondsPerHour);
    secondsRemaining = secondsRemaining % hours;

    minutes = Math.floor(secondsRemaining / secondsPerMinute);
    secondsRemaining = secondsRemaining % minutes;

    seconds = secondsRemaining;

    txt = "";
    txt += yearleadingZeros(years);
    txt += leadingZeros(months);
    txt += leadingZeros(days);
    txt += leadingZeros(hours);
    txt += leadingZeros(minutes);
    txt += leadingZeros(seconds);

    txt;

    function yearleadingZeros(i){
    x = "0" + Math.floor(i);
    return x;

    }

    function leadingZeros(i){
    x = "0" + Math.floor(i);
    x = ":" + x.substr(x.length-2, 2);
    return x;

    }

    Dan Ebberts replied 7 years, 11 months ago 4 Members · 6 Replies
  • 6 Replies
  • Andrei Popa

    September 18, 2017 at 6:26 am

    The aN is from the NaN error, which means Not a Number. That is because by the time you get to the days you already are trying to do 0%0, which does not have a result. The problem is that you use the number of years, months, etc in your % operation. This should work.totalTimeInSeconds = 31400000;
    secondsPerMinute = 60;
    secondsPerHour = 60 * 60;
    secondsPerDay = 60 * 60 * 24;
    secondsPerMonth = 60 * 60 * 24 * 30;
    secondsPerYear = 60 * 60 * 24 * 30 * 12;

    secondsRemaining = totalTimeInSeconds;

    years = Math.floor(secondsRemaining / secondsPerYear);
    secondsRemaining = secondsRemaining % secondsPerYear;

    months = Math.floor(secondsRemaining / secondsPerMonth);
    secondsRemaining = secondsRemaining % secondsPerMonth ;

    days = Math.floor(secondsRemaining / secondsPerDay);
    secondsRemaining = secondsRemaining % secondsPerDay ;

    hours = Math.floor(secondsRemaining / secondsPerHour);
    secondsRemaining = secondsRemaining % secondsPerHour;

    minutes = Math.floor(secondsRemaining / secondsPerMinute);
    secondsRemaining = secondsRemaining % secondsPerMinute ;

    seconds = secondsRemaining;

    txt = "";
    txt += yearleadingZeros(years);
    txt += leadingZeros(months);
    txt += leadingZeros(days);
    txt += leadingZeros(hours);
    txt += leadingZeros(minutes);
    txt += leadingZeros(seconds);

    txt;

    function yearleadingZeros(i){
    x = "0" + Math.floor(i);
    return x;

    }

    function leadingZeros(i){
    x = "0" + Math.floor(i);
    x = ":" + x.substr(x.length-2, 2);
    return x;

    }

    Andrei

  • Graham Harris

    September 19, 2017 at 6:22 pm

    Thanks Andrei, I really appreciate you taking the time to look at this. It now displays numbers instead of NA! Unfortunately it doesn’t count down when I run it. Would you have any idea why that is?

  • Graham Harris

    September 19, 2017 at 6:52 pm

    Sorry! It’s been a long day, I realised I had hard coded the time to a single variable so the expression is unable to count up or down. I altered the expression as below and it appears to do what I want, at least I can render it out and then reverse it in premiere to do what I want. Thanks again for your help Andrei.

    countspeed = 100;
    clockStart = 31102010;

    function times(n){
    if (n < 10) return "0" + n else return "" + n
    }

    clockTime = clockStart +countspeed*(time - inPoint);

    if (clockTime < 0){
    minus = "-";
    clockTime = -clockTime;
    }else{
    minus = "";
    }

    totalTimeInSeconds=clockTime
    ;

    secondsPerMinute = 60;
    secondsPerHour = 60 * 60;
    secondsPerDay = 60 * 60 * 24;
    secondsPerMonth = 60 * 60 * 24 * 30;
    secondsPerYear = 60 * 60 * 24 * 30 * 12;

    secondsRemaining = totalTimeInSeconds;

    years = Math.floor(secondsRemaining / secondsPerYear);
    secondsRemaining = secondsRemaining % secondsPerYear;

    months = Math.floor(secondsRemaining / secondsPerMonth);
    secondsRemaining = secondsRemaining % secondsPerMonth ;

    days = Math.floor(secondsRemaining / secondsPerDay);
    secondsRemaining = secondsRemaining % secondsPerDay ;

    hours = Math.floor(secondsRemaining / secondsPerHour);
    secondsRemaining = secondsRemaining % secondsPerHour;

    minutes = Math.floor(secondsRemaining / secondsPerMinute);
    secondsRemaining = secondsRemaining % secondsPerMinute ;

    seconds = secondsRemaining;

    txt = "";
    txt += yearleadingZeros(years);
    txt += leadingZeros(months);
    txt += leadingZeros(days);
    txt += leadingZeros(hours);
    txt += leadingZeros(minutes);
    txt += leadingZeros(seconds);

    txt;

    function yearleadingZeros(i){
    x = "0" + Math.floor(i);
    return x;

    }

    function leadingZeros(i){
    x = "0" + Math.floor(i);
    x = ":" + x.substr(x.length-2, 2);
    return x;

    }

  • Tristan Copley smith

    September 7, 2018 at 2:13 am

    I just tired using your code above and keep getting the following error:

    After Effects warning: Expression Disabled. Error at line 5 in property ‘Source Text’ of layer 1 (‘TIMER’) in comp ‘timer’.
    Expected: ).

    I’m quite certain I’m less qualified than yourself in these things — is this a quick fix? Any suggestions?

    Thanks!

  • Dan Ebberts

    September 7, 2018 at 6:16 am

    In the two lines that have < try substituting < and see if that fixes it.

    Dan

  • Dan Ebberts

    September 7, 2018 at 6:19 am

    That didn’t come out right. The problem is that < gets converted to ampersand+lt; if you preview a message before you post it. So just find the places where that happened and switch back to <.

    Dan

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy