• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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
0
DeveloperUser
Asked:
DeveloperUser
1 Solution
 
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
 
maosalahCommented:

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
 
HonorGodCommented:
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
 
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

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now