Skip to main content
Participant
April 7, 2023
Question

Can you use AE expressions to make Date calculations?

  • April 7, 2023
  • 2 replies
  • 992 views

There are two functions I'm trying to have AE perform and it would be great to know if there are expressions  that would make these possible. Since we're talking about calendar dates it's not a straight math problem. Here's what I'd like to be able to do

1. Create a Date from 3 inputted values (year, month, day)

2. Calculate the number of days between two such dates.

 

The functions: CreateDate and DateDiff would be perfect for this task but they both generate errors in the expression window ("[expression] is not a function"). 

 

Any insight would be greatly appreciated!

This topic has been closed for replies.

2 replies

Dan Ebberts
Community Expert
Community Expert
April 8, 2023

I think something like this should work:

d1 = new Date('4/8/2022').getTime();
d2 = new Date('4/8/2023').getTime();
Math.ceil(Math.abs(d2-d1)/ (1000 * 3600 * 24)); 

 

JoshK2Author
Participant
April 10, 2023

Thanks Dan, this is very helpful! Is there a way to have the start and end dates be inputs linked to an expression control rather than a date that I would have to type into the expression box itself?

Dan Ebberts
Community Expert
Community Expert
April 10, 2023

Like this maybe:

y1 = effect("Year 1")("Slider").value;
m1 = effect("Month 1")("Slider").value;
d1 = effect("Day 1")("Slider").value;
y2 = effect("Year 2")("Slider").value;
m2 = effect("Month 2")("Slider").value;
d2 = effect("Day 2")("Slider").value;

d1 = new Date(m1 + '/' + d1 + '/' + y1).getTime();
d2 = new Date(m2 + '/' + d2 + '/' + y2).getTime();
Math.ceil(Math.abs(d2-d1)/ (1000 * 3600 * 24)); 
JoshK2Author
Participant
April 8, 2023

Hmmm...it doesn't look like this is taking into account the different days of the month and leap years....am I missing something?

Community Expert
April 8, 2023

That expression is just a start. I don't have time to figure out the modifications for your specific requirements. Maybe Dan Ebberts will contribute something to the thread. He could probably do it without even opening AE.