Time Differnce in minutes using javascript

Hi - thanks for looking at my question.

I have 2 fields:

1 as a start date/time.
1 as a finish date/time.

The format will be:  

DD/MM/YY HH:MM

I would like the script to do 2 things:

1. Make sure the start date is is before the finish date
2. Get the time difference in minutes.

Many thanks

 Screenshot
DeveloperUserAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shock112Commented:
function getTheDiffTime(dateone, datetwo,format){
//format = “Days”, Hours, Minutes, Seconds
if(dateone > datetwo) {
var seconds = dateone.getTime() – datetwo.getTime();
} else {
var seconds = datetwo.getTime() – dateone.getTime();
}
var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
if(format==”Days”){
var rformat = Math.floor(seconds / day);
seconds -= rformat * day;
//alert(“days: “+rformat);
}else if(format==”Hours”){
// find the hours
rformat = translate(seconds / hour);
//alert(“hours: “+rformat);
}else if (format==”Minutes”){
//find the mintues
rformat= translate(seconds / minute);
//alert(“minutes: “+ rformat);
}else if(format==”Seconds”){
//find the seconds
rformat = translate(seconds / second);
//alert(“seconds: “+rformat);
}

return rformat
//alert(rformat);

}



//for calculating the current month in mintue
currentMonth = new Date();
plus_oneMonth = new Date();
plus_oneMonth.setMonth(plus_oneMonth.getMonth()+1);

getTheDiffTime(plus_oneMonth,currentMonth,”Minutes”);

You will get return the minutes.

0
Salih MaouiSenior Software DeveloperCommented:

1-

function check(){
var SDate = document.form.previous_date.value;
var EDate = document.form.next_date.value;

if (SDate == '' || EDate == '') {
alert("Plesae enter both....");
return false;
}

var endDate =new Date.parse(EDate);
var startDate =new Date.parse.Date(SDate);


if(startDate > endDate)
{
alert("Please ensure that the End Date is greater than or equal to the Start Date.");
document.calldate.next_day.focus();
return false;
}
}

Open in new window


2- For Get the time difference in minutes.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22106644.html
http://purab.wordpress.com/2009/10/07/get-time-difference-in-minutes/


good luck
0
HonorGodSoftware EngineerCommented:
This article should help: http://www.experts-exchange.com/A_483.html
0
Robert SchuttSoftware EngineerCommented:
Hi 'DeveloperUser', you haven't been back for answers or you may prefer a more direct answer...?
[ Note: this is my first answer ever on EE ]

<html>
<head>

<script type="text/javascript">

var arrMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function validateForm() {
	try {
		var f = document.forms['UserInput'];
		var d1 = checkDate(f.elements['StartDateTime'].value);
		var d2 = checkDate(f.elements['EndDateTime'].value);
		//alert(d1 + "\n" + d2);
		if ("" == d1 || "" == d2) {
			alert("Please supply the 'Start Date/Time' and 'Finish Date/Time'.");
			return false;
		}
		if (d1 > d2) {
			alert("'Start Date/Time' must be before 'Finish Date/Time'.");
			return false;
		}

		if ("undefined" != typeof f.elements["DiffMinutes"]) {
			f.elements["DiffMinutes"].value = Math.round((d2 - d1) / 60000); // difference in minutes
		}

		return false; // for now, only show result, REMOVE THIS LINE after testing

		return true;
	}
	catch (e) {
		alert(e);
		return false;
	}
}

function checkDate(d) {
	var r = d.match(/([0-9]+)\/([0-9]+)\/([0-9]+) ([0-9]+):([0-9]+)/);
	if (r && 6 == r.length) {
		var dn = new Date();
		return Date.parse(arrMonths[r[2]-1]+" "+r[1]+", "+(r[3]<50?"20":(r[3]<100?"19":""))+r[3]+" "+r[4]+":"+r[5]+":00");
	} else {
		return "";
	}
}

</script>

</head>
<body>

<form name="UserInput" method="post" action="post.asp" onSubmit="return validateForm()">
<table border=0 cellpadding=2 cellspacing=1>
<tr><td>Start Date/Time</td><td><input type="text" name="StartDateTime"></td><td>(DD/MM/[CC]YY hh:mm)</td></tr>
<tr><td>Finish Date/Time</td><td><input type="text" name="EndDateTime"></td><td>(DD/MM/[CC]YY hh:mm)</td></tr>
<tr><td>...</td><td>...</td></tr>
<tr><td>difference in minutes</td><td><input type="text" name="DiffMinutes" readonly></td></tr>
<tr><td colspan="2" align="right"><input type="submit" value="Submit Data"></td></tr>
</table>
</form>

</body>
</html>

Open in new window


Depending on what you want with the result you could put it in a hidden field (just change the type of element DiffMinutes) or inform the user with an alert for example.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shock112Commented:
refer : http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_483-2-Dates-What%27s-the-difference.html
function getTheDiffTime(dateone, datetwo,format){
//format = “Days”, Hours, Minutes, Seconds
if(dateone > datetwo) {
var seconds = dateone.getTime() – datetwo.getTime();
} else {
var seconds = datetwo.getTime() – dateone.getTime();
}
var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
if(format==”Days”){
var rformat = Math.floor(seconds / day);
seconds -= rformat * day;
//alert(“days: “+rformat);
}else if(format==”Hours”){
// find the hours
rformat = translate(seconds / hour);
//alert(“hours: “+rformat);
}else if (format==”Minutes”){
//find the mintues
rformat= translate(seconds / minute);
//alert(“minutes: “+ rformat);
}else if(format==”Seconds”){
//find the seconds
rformat = translate(seconds / second);
//alert(“seconds: “+rformat);
}

return rformat
//alert(rformat);

}



//for calculating the current month in mintue
currentMonth = new Date();
plus_oneMonth = new Date();
plus_oneMonth.setMonth(plus_oneMonth.getMonth()+1);

getTheDiffTime(plus_oneMonth,currentMonth,”Minutes”);

You will get return the minutes.

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.