Expressions/Scripts/Presets

Moderators: Disciple, zlovatt

 
ritka
Topic Author
Posts: 10
Joined: Thu Mar 25, 2010 10:30 am

Leap year calculation

Thu Oct 26, 2017 3:31 pm

I would like to create an animation, where one frame are represents one day and the date are displayed.
But i have problems with the leap year calculations.

I have two constant input values from two slider: the starting year and the starting day of the year. These values are increment with based on (time/thisComp.frameDuration) - but i can't solve the problem of the different length of february in leap years...
Do you have any idea?
 
User avatar
CodingAe
Posts: 7
Joined: Wed Aug 30, 2017 8:35 pm
Location: Detroit, Mi
Contact:

Re: Leap year calculation

Thu Nov 02, 2017 5:52 pm

By using the date object already program in Expression, it will automatically give you the leap year once you reach a year that has a leap year.


var d = new Date(2016, 1); // new Date(Year, Month) You can pick any year and month you want to start with. 



Pick the previous month from the month you want to use.
In computer programming it's common to start counting from 0, so 0 represents January.
 

var timer = time / thisComp.frameDuration;
d.setDate(timer + 1); // setDate() allows you to add to the preexisting date.


Adding 1 is going to give you the first date of the month if you are at frame 0.
If you are at the 0 frame and you don't add 1 it's going to give you the date from the previous month.


d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();


2016 was a leap year, and once you reach next by scrolling the time indicator in 2017 February is back at 28 days.

Whole code:

var d = new Date(2016, 1);
var timer = time / thisComp.frameDuration;
d.setDate(timer + 1);
d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
 
ritka
Topic Author
Posts: 10
Joined: Thu Mar 25, 2010 10:30 am

Re: Leap year calculation

Sat Nov 04, 2017 11:21 am

This is perfect, thank you!
 
User avatar
CodingAe
Posts: 7
Joined: Wed Aug 30, 2017 8:35 pm
Location: Detroit, Mi
Contact:

Re: Leap year calculation

Sat Nov 04, 2017 2:51 pm

No problem, I'm glad I was able to help out.

Who is online

Users browsing this forum: No registered users and 4 guests