Solved

Time Differnce in minutes using javascript

Posted on 2011-02-28
5
282 Views
Last Modified: 2012-05-11
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
Comment
Question by:DeveloperUser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:shock112
ID: 34996061
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
 
LVL 3

Expert Comment

by:maosalah
ID: 34996103

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
 
LVL 41

Expert Comment

by:HonorGod
ID: 34996633
This article should help: http://www.experts-exchange.com/A_483.html
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 34997846
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
 
LVL 1

Expert Comment

by:shock112
ID: 35014531
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

724 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