By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Calculated Date

P: 5
I have created a data form in which the user will enter in dates, and I wanted to know if there was a simple script that would allow me to take two date fields in the form and figure the days inbetween/ days left of the two dates. So I want something like a count down that will calculate the days left between the two enterd dates
Jul 24 '08 #1
Share this Question
Share on Google+
3 Replies


P: 57
i think you cam try something like:
Expand|Select|Wrap|Line Numbers
  1. var myDate1=new Date(); //todays date
  2. var myDate2=newDate(2008,6,6); //older date, you could also use "setFullYear"
  3. var temp=myDate1.getTime() - myDate2.getTime();
  4.  
  5. var day=24*60*60*1000; //convert milliseconds to days
  6.  
  7. var difference = temp/day;
  8.  
  9.  
Jul 24 '08 #2

RamananKalirajan
100+
P: 607
Hi I hope this code may help you if you are trying get the weekday between two user defined dates. Please ensure that the date format is dd-mon-yyyy.

[HTML]<Html>
<head>
<script language="javascript">
function changeVal(month)
{
if(month=="jan"||month=="Jan"||month=="JAN")
month=0;
if(month=="feb"||month=="Feb"||month=="FEB")
month=1;
if(month=="mar"||month=="Mar"||month=="MAR")
month=2;
if(month=="apr"||month=="Apr"||month=="APR")
month=3;
if(month=="may"||month=="May"||month=="MAY")
month=4;
if(month=="jun"||month=="Jun"||month=="JUN")
month=5;
if(month=="jul"||month=="Jul"||month=="JUL")
month=6;
if(month=="aug"||month=="Aug"||month=="AUG")
month=7;
if(month=="sep"||month=="Sep"||month=="SEP")
month=8;
if(month=="oct"||month=="Oct"||month=="OCT")
month=9;
if(month=="nov"||month=="Nov"||month=="NOV")
month=10;
if(month=="dec"||month=="Dec"||month=="DEC")
month=11;

return(month);
}
function findDiff()
{
var day1, day2;
var month1, month2;
var year1, year2;

value1 = document.getElementById('stDate').value;
value2 = document.getElementById('endDate').value;

day1 = value1.substring (0, value1.indexOf ("-"));
month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
month1 = changeVal(month1);
year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);

day2 = value2.substring (0, value2.indexOf ("-"));
month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
month2 = changeVal(month2);
year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);

var startDate = new Date(year1,month1,day1);
var endDate = new Date(year2,month2,day2);
var wrkdays=0;

while(startDate.getTime()<=endDate.getTime())
{
wrkdays++;
if(startDate.getDay()==0)
{
wrkdays--;
}
else if(startDate.getDay()==6)
{
wrkdays--;
}
else
{
}
var d = new Date(startDate.getTime() + 86400000);
startDate=d;
}
alert("Total Workdays = "+wrkdays);
}

</script>
</head>
<body>
<input type="text" id="stDate"><br/>
<input type="text" id="endDate"><br/>
<input type="button" value="calculate" onclick="findDiff()">
</body>
</html>[/HTML]

If you have any doubts pls ask yaar.

Regards
Ramanan Kalirajan
Jul 25 '08 #3

acoder
Expert Mod 15k+
P: 16,027
That code could be shortened considerably, in particular the month code could be one line via an array. Also, the script language attribute is deprecated, use the type attribute instead.
Jul 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.