Leap year calculation

Moderators: Disciple, zlovatt

Post Reply
ritka
Posts: 10
Joined: March 25th, 2010, 10:30 am

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: 21
Joined: August 30th, 2017, 8:35 pm
Location: Detroit, Mi
Contact:

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.

Code: Select all

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.
 

Code: Select all

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.

Code: Select all

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:

Code: Select all

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

This is perfect, thank you!
User avatar
CodingAe
Posts: 21
Joined: August 30th, 2017, 8:35 pm
Location: Detroit, Mi
Contact:

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