Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Adding 00s to DAYS in a counter timer using format YEARS:DAYS:HOURS:MINUTES:SECONDS

  • Adding 00s to DAYS in a counter timer using format YEARS:DAYS:HOURS:MINUTES:SECONDS

    Posted by Simon Peller on August 15, 2020 at 12:36 am

    Hi everyone.
    Long time lurker. First time poster ☺
    Usually I can hash together an expression using what I can find online (mostly from this website tbh) but I’m having trouble with this particular one.

    I’m making a counter using the expression below. The time format I’m using is YEARS:DAYS:HOURS:MINUTES:SECONDS

    As you can see I’m using a function to add 0 to n if n is less than 10.
    This works fine for the 2 digit numbers (YEARS, HOURS, MINUTES, SECONDS) but not with the 3 digit number (DAYS).

    I would like to achieve YY:DDD:HH:MM:SS
    For example 01:009:23:59:59. Where the DAYS always has 3 digits whether its 001, 999 or anything in between.

    Thank you kindly for your help. I hope in the future, as I grow in confidence using expressions, I’ll be able to help someone else here in return.
    Much love and respect.

    Simon.

    slider = effect("Point Control")("Point")[0]

    sec = Math.floor(slider)%60;
    min = Math.floor(slider/60)%60;
    hour = Math.floor(slider/3600)%24;
    days = Math.floor(slider/86400)%365;
    years = Math.floor(slider/31536000);

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

    addZero(years) + ":" + addZero(days) + ":" + addZero(hour) + ":" + addZero(min) + ":" + addZero(sec)

    Simon Peller replied 5 years, 10 months ago 3 Members · 4 Replies
  • 4 Replies
  • Chaz Chester

    August 15, 2020 at 1:46 am

    Like this?
    slider = effect(“Slider Control”)(“Slider”);

    sec = Math.floor(slider)%60;
    min = Math.floor(slider/60)%60;
    hour = Math.floor(slider/3600)%24;
    days = Math.floor(slider/86400)%365;
    years = Math.floor(slider/31536000);

    function addZero(n) {
    if (n < 10) return “0” + n else return n;
    }

    function addTwoZero(n) {
    if (n < 10) return “00” + n;
    if (n >= 10 && n < 100) return “0” + n;
    else return n;
    }

    addZero(years) + “:” + addTwoZero(days) + “:” + addZero(hour) + “:” + addZero(min) + “:” + addZero(sec)

  • Filip Vandueren

    August 15, 2020 at 7:48 am

    A compact Javascript way to add leading zeroes is to always add more than you need, then slice the string length to what you need from the right like this:

    (“00″+days).slice(-3);

    and

    (“0″+months).slice(-2);

    This works if you’re sure days is never more than 3 digits, months never more than 2
    Because for example
    (“00″+123).slice(-2)
    would return 23, not 123

  • Simon Peller

    August 15, 2020 at 2:28 pm

    Wonderful. Perfect. Thank you.

    Not only has this solved my initial problem but by reverse engineering and tweaking your additional bit of code it has allowed me to solve a couple of other problems I was having trouble with in other projects.

    Really, thanks so much for taking the time to reply so quickly.
    Simon

  • Simon Peller

    August 15, 2020 at 2:37 pm

    Thank you for your swift reply.
    Unfortunately I don’t fully understand this due to my lack of Java knowledge. No doubt as I become more familiar with expressions and Java your reply will make a lot more sense ☺

    For now my problem was solved with the help of Chaz Chester in one of the replies above.

    Thanks again
    Simon

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