Solved

Calculate date difference in YMD format "20081008"

Posted on 2008-10-30
2
361 Views
Last Modified: 2012-05-05
On a submit page, I have 2 Select's - startdate, enddate.
Each holds a calculated value and allows the user to select a startdate and a enddate, submit the form and retrieve data.

Now I need to verify the form before submitting it, that startdate < enddate AND the difference between the dates are <= 4 weeks/31 days.

The function I've made so far is below - but I need osme help on how to verify the max. date difference.

Cheers,
Mark

function valiDates(){
		var maxtime = 60*60*24*31;
		var btn = document.getElementById("submit");
		var strSd = document.getElementById("startdate").value;
		var strEd = document.getElementById("enddate").value;
		var sd = new Date( strSd.substring(0,3) +"/"+ strSd.substring(4,5) +"/"+ strSd.substring(6,7) );
		var ed = new Date( strEd.substring(0,3) +"/"+ strEd.substring(4,5) +"/"+ strEd.substring(6,7) );
		if (sd > ed ){
			btn.disabled = true;
			btn.value = 'Slut dato skal være senere end start dato, max 4 uger statistik kan dannes !';
			document.getElementById("alert1").style.visibility = 'visible';
			} else {
			btn.disabled = false;
			btn.value = 'Hent data';
			document.getElementById("alert1").style.visibility = 'hidden';
			}			
	}

Open in new window

0
Comment
Question by:trg_dk
2 Comments
 
LVL 1

Accepted Solution

by:
1eEurope earned 500 total points
ID: 22839385
just add this at the bottom of your function and your good to go
//Set 1 day in milliseconds
var one_day=1000*60*60*24;
 
//Calculate difference btw the two dates, and convert to days
var dif = Math.ceil((ed.getTime()-sd.getTime())/one_day);
 
if(dif > 31) {
     btn.disabled = true;
     btn.value = '...!';
     document.getElementById("alert1").style.visibility = 'visible';
}                        

Open in new window

0
 
LVL 2

Author Closing Comment

by:trg_dk
ID: 31511589
YOU da' man :-)

Thanks for your quick solution - works perfectly !!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What does GoogleTagMgr javascripts below do 5 24
Html Newline 7 20
Do alert on select 6 13
JS Event Does not Trigger From File 2 23
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question