If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

New to javascript

Hi all,

I am new to the boards and new to Javascript and I am looking for some help for on a Date Difference Calculator. I have already created the basics of the page where you can enter the month, day and year but I am not sure who to do the actual script to have it calculate the difference between the two dates. I have named my function dateDiff and want the return to be in a alert box.

You will need to add validation to check that the dates as entered are valid (not something like 45/27/5008 or even 31/02/2007) and that the second date comes after the first (Actually it works if date2 is before date1).

I am new to the boards and new to Javascript and I am looking for some help for on a Date Difference Calculator. I have already created the basics of the page where you can enter the month, day and year but I am not sure who to do the actual script to have it calculate the difference between the two dates. I have named my function dateDiff and want the return to be in a alert box.

Can anyone help out?

Please be advised that no matter how often you see it done - it is a fatal error to use time functions to calculate dates. There will be circumstances when daylight saving transitions introduce an error.
There are complicated ways of doing it efficiently, however here is a simple albeit slow algorithm for simply counting the days. There is no error checking:

"There will be circumstances when daylight saving transitions introduce an error."

Really? How so? How can a date expressed in days (not hours) be affected by daylight saving?

Lexilou is new to the boards and new to Javascript. He is looking for a (comparatively simple) script that he can understand and apply. I have mentioned before that a good teacher operates at the level of the student. I have to say that I feel that it is inappropriate to offer graduate-level solutions to beginners, especially without any explanation. Otherwise there is a sense that the motivaton is not so much to really help the student but rather to say "look how remarkably clever I am!".

"There will be circumstances when daylight saving transitions introduce an error."

Really? How so? How can a date expressed in days (not hours) be affected by daylight saving?

Simple... If you don't specify the hour, the Date() object will take 0 hours as default. Now, if you try to find how many days are between Saturday before DST and Sunday after DST, if you may find 0 days or 1 day, according to the fact that you will count or not that DST hour...

Simple... If you don't specify the hour, the Date() object will take 0 hours as default. Now, if you try to find how many days are between Saturday before DST and Sunday after DST, if you may find 0 days or 1 day, according to the fact that you will count or not that DST hour...

Sorry, Kor. With all due respect, I am unable to see what you are driving at. I have tested my script with 27/10/2007 and 28/10/2007, also 24/03/2007 and 25/03/2007. In each case I get the correct answer - 1 day. I do not see how any other result could possibly arise.

In the UK summer time begins/ends at 0100 GMT. So 0000 hours are not duplicated.

You misunderstood me... I have not criticized your code. I was to say that, when simply use a math add in building a calendar, the result might be not the one you expect for. The safe way is to use Date() associated methods, even in a loop (because they stand by the internal CU clock), than to calculate in milliseconds, split in years/months/... whichever, and display the result later.

I was thinking on:
"Really? How so? How can a date expressed in days (not hours) be affected by daylight saving?"
Well, yes, it will affect if you calculate in days as 24*60*60*1000 milliseconds, you will get the wrong answer. It is not the case of your code (at least not at my first glance). I meant only your too short note about the DST... It is important, you know...

Yes, I accept what you say, but the issue of DST has no relevance to this thread or to the question asked.

I accept that errors can possibly arise with calendars etc., but when expressed in days the difference bewteen two dates is not affected even if one day has 23 or 25 hours due to DST.

As I say, Lexilou is new to the boards and new to Javascript. He is looking for a (comparatively simple) script that he can understand and apply. I have mentioned before that a good teacher operates at the level of the student. I have to say that I feel that it is inappropriate to offer graduate-level solutions to beginners, especially without any explanation.