Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Megan Weber
Megan Weber
32,795 Points

Why doesn't my date calculation give an integer?

When I try to calculate the number of days in the current month, I get a number which is very close to an integer, but isn't one. Why is this? Here's my code:

let now = new Date();
let past = new Date(now.getFullYear(), now.getMonth(), 1);
let future = new Date(now.getFullYear(), now.getMonth() + 1, 1);
console.log(((((future - past)/1000)/60)/60)/24);

The console says 30.958333333333332. I'm using Chrome.

Jason Anders
Jason Anders
Treehouse Moderator 145,863 Points

You haven't attached any code? Please post the code you are trying so we may be able to assist you. Make sure the code is formatted correctly for the Community Forum. Refer to the Markdown Cheatsheet link above the Post button if you are unsure.

:) :dizzy:

1 Answer

Steven Parker
Steven Parker
243,656 Points

The calculations are based on local time, and this month includes a switchover to daylight saving time. So if your system timezone setting includes daylight time (which it apparently does), not all days have 24 hours this month.

This calculation will produce an integer for other months without DST switchovers..

Megan Weber
Megan Weber
32,795 Points

That makes sense. Rounding will always give the correct number, right?

Steven Parker
Steven Parker
243,656 Points

Rounding should work well, since it's only 1/24 of a day off during a shift month.