Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calaculate Number of Days

Status
Not open for further replies.

garfield11

IS-IT--Management
Jul 4, 2002
44
SG
Hi all,

I wanna calculate the number of days between 2 input dates.

Below r my codes:

function date(LeaveFrom, LeaveTo, DaysonLeave)
{
var x = new Number(new Date(LeaveFrom.value));
var y = new Number(new Date(LeaveTo.value));

DaysonLeave.value = (y - x)/86400000;
}

This code works only when i input the date as in mm/dd/yy
Example: LeaveFrom: 02/12/02
LeaveTo: 02/19/02
DaysonLeave: 7 (19-12)

But i dun wan user to input the date as in mm/dd/yy but in dd/mm/yy format, how can i go about doing so?

Can anyone plese kindly advise?

Thanks!

Love-Garfield
 
Do not let the user type anything, I have had tons of troubles with this as the inserted dates needed to go into a database (sql serv 7).
I made a vb calendar because all users have IE 5+. It is vb script but I am working on a java script one that works in IE and Mozilla 5.0.


(as far as I am concerned NS 4 does not excist anymore)
 
Hmm... cos me doing an online leave system so i need users (or rather employees) to input the start date n end date of leave then system will automatically calculate the number of days in int (end date - start date).

So can it be done?

Love_Garfield
 
If you don't want to get into complex scripts with date conversion, you can do this - instead of using one text field for date, use three separate ones:
day [__] month [__] year [__]

Then in your function combine 3 string values into one and use the script you have. Using this you don't give an option for date format error input.

Also: you named your function as date(). It is not equal to the standatd Date() function - JS is case-sensitive - but you can occasionally change case and get errors. You better change the name of your function.

 
starway, you are correct this is a fast and good sollution. I have used it myselve.
Beleve it or not but users can be stupid (or have a bad day) but with this system and with select boxes for day month and year I still had a problem (31 of feb).
The easyest way was with the calendar not only for the programmer but allso for the user.

I don't think garfield11 understoud my answer, I did not suggest that you should't use dates alltoghether. I suggested that you should not let the user type that date but give the user the opportunety to Click a date from a calendar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top