Solved

JavaScript: Test if hour inside time span

Posted on 2013-01-24
2
342 Views
Last Modified: 2013-01-28
Using JavaScript, how can I test if an hour is inside a time span?

I cannot just use less than or greater than tests because the hour "0" comes immediately after the hour "23".

If a session begins at 23 (11:00 pm) and lasts for 3 hours, 1 (1:00 am) is inside the session but 9 (9:00 am) is outside the session.

  function checkSessionStatus (SessionStartTime, SessionLength, TimeCurrent) {
   if(1==1) return "Session is active"
   else return "Session is NOT active"
  }

  alert(checkSessionStatus(1, 12, 3));  // Session is active
  alert(checkSessionStatus(22, 12, 3));  // Session is active
  alert(checkSessionStatus(0, 2, 3));  // Session is NOT active
  alert(checkSessionStatus(22, 2, 3));  // Session is NOT active

Open in new window

0
Comment
Question by:hankknight
[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
2 Comments
 
LVL 5

Accepted Solution

by:
Eikroman earned 400 total points
ID: 38818163
Hello,

I'm assuming that the session can last 24 hours max (every hour will then be active)
I'm assuming that start time and current time is in range of [0-23]
The start hour and end hour counts in.

The logic:
If the TimeCurrent is less than SessionStartTime and the sessions spans over two different days (StartTime+Length is greater or equal to 24), then we move the SessionStartTime 24 hours back and recursively call the function.

Then we simply verify if TimeCurrent is greater or equal to SessionStartTime AND is less or equal to end time (SessionStartTime+SessionLength).

The code:
 function checkSessionStatus (SessionStartTime, SessionLength, TimeCurrent) {
 DAY_LENGTH=24;
  	
  	if(SessionStartTime+SessionLength>=DAY_LENGTH&&SessionStartTime>TimeCurrent)
  		return checkSessionStatus(SessionStartTime-DAY_LENGTH, SessionLength, TimeCurrent);
  	else
  		{
  			if(TimeCurrent>=SessionStartTime && TimeCurrent <= SessionStartTime+SessionLength)
  				return "Session is active";
  			else
  				return "Session is NOT active";
  		}
   
  }

Open in new window


Let me know if you need further clarification or have any other question regarding this.
0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 100 total points
ID: 38818303
You should record the start and end date with a script like:
var myStartDate = (StartDate);
var myEndDate = (EndDate);
function CheckSession(myEndDate ) {
	var myDate = new Date(); 
	var myDiv1 = document.getElementById("hideshow1");
	var myDiv2 = document.getElementById("hideshow2");
	if (myDate >myStartDate && myDate<myEndDate) {
		alert("In session");
	} else {
		alert("Out of session");
	}
}

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

732 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