Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can you use AE expressions to make Date calculations?

New Here ,
Apr 07, 2023 Apr 07, 2023

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!

TOPICS
Expressions , How to
913
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2023 Apr 07, 2023
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 08, 2023 Apr 08, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2023 Apr 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2023 Apr 08, 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)); 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 10, 2023 Apr 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2023 Apr 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)); 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2023 Apr 13, 2023
LATEST

Awesome! Thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines