Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-15-2002, 07:21 AM   PM User | #1
sharil_hashim
New to the CF scene

 
Join Date: Jun 2002
Location: Kuala Lumpur
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
sharil_hashim is an unknown quantity at this point
Calculate the difference btw 2 dates

Here is the script to calculate the difference between 2 dates:
the date format is in this form; date=dd/mm/yyyy and time=hh:mm
the difference will be in the form of hours.minutes

<script language=javascript>
function daysDiff(eD, eT, sD, sT) {
var endDate = eD.split("/");
var dy2 = endDate[0];
var mo2 = endDate[1];
var yr2 = endDate[2];
var startDate = sD.split("/");
var dy1 = startDate[0];
var mo1 = startDate[1];
var yr1 = startDate[2];
var endTime = eT.split(":");
var hr2 = endTime[0];
var mn2 = endTime[1];
var startTime = sT.split(":");
var hr1 = startTime[0];
var mn1 = startTime[1];
return diff = daysElapsed(new Date(yr2,mo2,dy2,hr2,mn2),new Date(yr1,mo1,dy1,hr1,mn1));
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function daysElapsed(date1,date2) {
var difference =
Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),0 )
- Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),date2.getHours(),date2.getMinutes(),0 );
var retVal = difference/1000/60;
return retVal;
}
</script>

However i found that for following date the calculation is not correct
>31/12/2001 12:00 - 01/01/2002 12:00 = 24 hours (correct)
>31/01/2002 12:00 - 01/02/2002 12:00 = -48 hours (incorrect)
>28/02/2002 12:00 - 01/03/2002 12:00 = 96 hours (incorrect)
>31/03/2002 12:00 - 01/04/2002 12:00 = 0 hours (incorrect)
>30/04/2002 12:00 - 01/05/2002 12:00 = 48 hours (incorrect)
>31/05/2002 12:00 - 01/06/2002 12:00 = 0 hours (incorrect)
>30/06/2002 12:00 - 01/07/2002 12:00 = 48 hours (incorrect)
>31/07/2002 12:00 - 01/08/2002 12:00 = 24 hours (correct)
>31/08/2002 12:00 - 01/09/2002 12:00 = 0 hours (incorrect)
>30/09/2002 12:00 - 01/10/2002 12:00 = 48 hours (incorrect)
>31/10/2002 12:00 - 01/11/2002 12:00 = 0 hours (incorrect)
>30/11/2002 12:00 - 01/12/2002 12:00 = 48 hours (incorrect)
>31/12/2002 12:00 - 01/01/2003 12:00 = 24 hours (correct)

if anyone have ever found solution for this problem please update this script. Thank you
__________________
as cool as ice
sharil_hashim is offline   Reply With Quote
Old 06-15-2002, 09:41 AM   PM User | #2
joh6nn
wei wu wei


 
joh6nn's Avatar
 
Join Date: Jun 2002
Location: 72° W. 48' 57" , 41° N. 32' 04"
Posts: 1,887
Thanks: 0
Thanked 1 Time in 1 Post
joh6nn is an unknown quantity at this point
the following method will return the number of milliseconds between january first, 1970, and a date.

Date.parse(dateString);

if you take your two dates, parse() them, and then divide by 1000, and then divide by 60, you'll have the number of minutes between the two dates.

so,
function daysDiff(date1, date2) {
temp1 = date1.toString():
temp2 = date2.toString();
temp1 = Date.parse(temp1);
temp2 = Date.parse(temp2);
temp1 /= 1000; temp1 /= 60;
temp2 /= 1000; temp2 /= 60;
var temp3 = Math.abs( temp1 - temp2):
return temp3;
}
that will give you then number of minutes between the dates. to get the number of hours, you just divide by 60 again.
__________________
bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

i am a loser geek, crazy with an evil streak,
yes i do believe there is a violent thing inside of me.
joh6nn is offline   Reply With Quote
Old 06-17-2002, 02:39 AM   PM User | #3
sharil_hashim
New to the CF scene

 
Join Date: Jun 2002
Location: Kuala Lumpur
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
sharil_hashim is an unknown quantity at this point
difference btw 2 dates

To get the difference between the 2 dates is not a problem. However if we refer back to the main question, the problem occurs when i want to get the differences between dates of different months. Please refer to the example inthe main question.

Thank you,
Sharil Md Hashim
__________________
as cool as ice
sharil_hashim is offline   Reply With Quote
Old 06-17-2002, 08:19 AM   PM User | #4
joh6nn
wei wu wei


 
joh6nn's Avatar
 
Join Date: Jun 2002
Location: 72° W. 48' 57" , 41° N. 32' 04"
Posts: 1,887
Thanks: 0
Thanked 1 Time in 1 Post
joh6nn is an unknown quantity at this point
::sigh::

i'm pretty sure that your problem is composed of multiple things.

first, you're using Date.getYear(), and then trying to convert it for Y2K, which won't work. there's a Y2K method name getFullYear(), which you should use instead. also, you've forgotten to account for the possibility of subtracting a larger date from a smaller date, which would result in a negative number. you have to use Math.abs() to make sure that the number is always positive.

all of that, is in the function that i wrote for you.
__________________
bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

i am a loser geek, crazy with an evil streak,
yes i do believe there is a violent thing inside of me.
joh6nn is offline   Reply With Quote
Old 06-17-2002, 10:42 AM   PM User | #5
sharil_hashim
New to the CF scene

 
Join Date: Jun 2002
Location: Kuala Lumpur
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
sharil_hashim is an unknown quantity at this point
Smile Yes.. found the solution

Hi..

thanks mate.. i've found the solution. Somehow i found that the months value i pass into the variables MO1 & MO2 must be subsracted by 1. Example:
before:
daysElapsed(new Date(yr2,MO2,dy2,hr2,mn2),new Date(yr1,MO1,dy1,hr1,mn1));
after:
daysElapsed(new Date(yr2,MO2-1,dy2,hr2,mn2),new Date(yr1,MO1-1,dy1,hr1,mn1));

otherwise the month it calculate would be the month after the month i requested. Well thanks again for your help. if you have any curiosity just mail me k.

Thanks again

regards,
Sharil Md Hashim
__________________
as cool as ice
sharil_hashim is offline   Reply With Quote
Old 06-18-2002, 01:47 PM   PM User | #6
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 595
Thanks: 1
Thanked 20 Times in 20 Posts
jalarie is an unknown quantity at this point
What happens when the two dates and times span the Daylight Savings switch points?
jalarie is offline   Reply With Quote
Old 06-19-2002, 02:51 AM   PM User | #7
sharil_hashim
New to the CF scene

 
Join Date: Jun 2002
Location: Kuala Lumpur
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
sharil_hashim is an unknown quantity at this point
Well, frankly speaking, living in tropical country i never experience Daylight Saving time. In fact this is the first time i heard about it.

From my understanding it's just a matter of moving you clock 1 hour foward during summer rite?(correct me if i'm wrong!!). Therefore i think maybe we can add 1 hour extra for those period which falls; i.e. in United States at 2 a.m. on the first Sunday of April and reverts to standard time at 2 a.m. on the last Sunday of October.

Hope this will help & i don't dare to say much as i might giving u the wrong info..

regards,
Sharil Md Hashim
__________________
as cool as ice
sharil_hashim is offline   Reply With Quote
Old 06-19-2002, 02:31 PM   PM User | #8
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 595
Thanks: 1
Thanked 20 Times in 20 Posts
jalarie is an unknown quantity at this point
My DST question was to alert people to the fact that the result will probably be too high or too low by one hour when crossing the DST switch points. The offered scripts need to be aware of this to avoid giving incorrect results.
jalarie is offline   Reply With Quote
Old 06-20-2002, 02:15 AM   PM User | #9
sharil_hashim
New to the CF scene

 
Join Date: Jun 2002
Location: Kuala Lumpur
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
sharil_hashim is an unknown quantity at this point
Well that is understandable. Why not u give me the details on DST and maybe i can add up the features into the script or if u have such scripts, do mind sharing it with me.
__________________
as cool as ice
sharil_hashim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 PM.


Advertisement
Log in to turn off these ads.