Javascript, Hour comparisson

Having two strings (start and end time) in such form "16:30", "02:13" I want to compare them and check if the gap is greater than 5 mins.

How can this be achieved in Javascript in an easy way?

--------------Solutions-------------

function parseTime(time) {
var timeArray = time.split(/:/);
// Using Jan 1st, 2010 as a "base date". Any other date should work.
return new Date(2010, 0, 1, +timeArray[0], +timeArray[1], 0);
}

var diff = Math.abs(parseTime("16:30").getTime() - parseTime("02:13").getTime());
if (diff > 5 * 60 * 1000) { // Difference is in milliseconds
alert("More that 5 mins.");
}

Do you need to wrap over midnight? Then this is more difficult. For example, 23:59 and 00:01 will produce a difference of 23 hours 58 minutes and not 2 minutes.

If that's the case you need to define your case more closely.

You can do as following:

if (((Date.parse("16:30") - Date.parse("02:13")) / 1000 / 60) > 5)
{
}

// time is a string having format "hh:mm"
function Time(time) {
var args = time.split(":");
var hours = args[0], minutes = args[1];

this.milliseconds = ((hours * 3600) + (minutes * 60)) * 1000;
}

Time.prototype.valueOf = function() {
return this.milliseconds;
}

// converts the given minutes to milliseconds
Number.prototype.minutes = function() {
return this * (1000 * 60);
}

Subtracting the times forces the object to evaluate it's value by calling the valueOf method that returns the given time in milliseconds. The minutes method is another convenience method to convert the given number of minutes to milliseconds, so we can use that as a base for comparison throughout.

new Time('16:30') - new Time('16:24') > (5).minutes() // true

This includes checking whether midnight is between the two times (as per your example).

var startTime = "16:30", endTime = "02:13";

var parsedStartTime = Date.parse("2010/1/1 " + startTime),
parsedEndTime = Date.parse("2010/1/1 " + endTime);

// if end date is parsed as smaller than start date, parse as the next day,
// to pick up on running over midnight
if ( parsedEndTime < parsedStartTime ) ed = Date.parse("2010/1/2 " + endTime);

var differenceInMinutes = ((parsedEndTime - parsedStartTime) / 60 / 1000);
if ( differenceInMinutes > 5 ) {
alert("More than 5 mins.");
}

Category:javascript Time:2010-06-18 Views:1

Related post

  • Javascript date and time comparisson 2012-03-07

    How can I compare these two date and time values: date 1 = 2012-03-07 11:55:18 date 2 = 2012-01-02 11:02:44 and find the time difference between them? --------------Solutions------------- you can get seconds difference with below code,you can convert

  • Why is accessing dimensions of the image so expensive in JavaScript on IE8? 2010-10-12

    I have to process loads of images. First, I need to check if the size of the image is greater than 50x60 and appropriately increasing the counter of bad images. The problem I have is that the speed of n.width / n.height on Internet Explorer 8 is extr

  • Best Debugging Tools for JavaScript/xulrunner Development 2008-08-07

    In the past few months I began my first serious JavaScript development, as I began developing XUL applications. I use Notepad++ as my editor, but I was pretty appalled to find (as far as I can tell) that Venkman/Javascript Debugger is the tool for de

  • How can I create Prototype Methods (like JavaScript) in C#.Net? 2008-08-07

    How is it possible to make prototype methods in C#.Net? In JavaScript, I can do the following to create a trim method for the string object: String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); } How can I go about doing this in

  • Javascript keyboard events primer? (or rather: help me with my custom dropdown) 2008-08-07

    I need help finishing my custom built ajax [div] based dynamic dropdown. Basically, I have an [input] box which; onkeyup, runs an Ajax search which returns a bunch of results in divs and are drawn back in using innerHTML. These divs all have highligh

  • Length of a JavaScript object (that is, associative array) 2008-08-07

    If I have a JavaScript associative array, say: var myArray = new Object(); myArray["firstname"] = "Gareth"; myArray["lastname"] = "Simpson"; myArray["age"] = 21; Is there a built-in or accepted best practice way to get the length of this array? JavaS

  • How to specify javascript to run when ModalPopupExtender is shown 2008-08-07

    The ASP.NET AJAX ModalPopupExtender has OnCancelScript and OnOkScript properties, but it doesn't seem to have an OnShowScript property. I'd like to specify a javascript function to run each time the popup is shown. In past situations, I set the Targe

  • Graphing JavaScript Library 2008-08-08

    I'm creating a web application on Google App Engine for fun and I'd like to include graphs so users can see some stats. Are there any free (either as in beer or speech) JavaScript libraries that can take a table or make some AJAX call and display a g

  • Reference that lists available JavaScript events? 2008-08-09

    I'm aware of things like "onchange", "onmousedown" and "onmouseup" but is there a good reference somewhere that lists all of them complete with possibly a list of the elements that they cover? --------------Solutions------------- W3Schools seems to h

  • Graph visualization library in JavaScript 2008-08-10

    I have a data structure that represents a directed graph, and I want to render that dynamically on an HTML page. These graphs will usually be just a few nodes, maybe ten at the very upper end, so my guess is that performance isn't going to be a big d

  • Debugging JavaScript in Internet Explorer and Safari 2008-08-10

    Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and Safari. In Firefox, you can use Firebug's logging feature and command Line functions. However, this doesn't help me when I move to other browsers. ---------

  • Some kind of task manager for JavaScript in Firefox 3? 2008-08-11

    Recently I have been having issues with Firefox 3 on Ubuntu Hardy Heron. I will click on a link and it will hang for a while. I don't know if its a bug in Firefox 3 or a page running too much client side JavaScript, but I would like to try and debug

  • Can I use JavaScript to create a client side email? 2008-08-11

    I want to create a client side mail creator web page. I know the problems of using the mailto action in an html form (not standard, no default mail appication set on the client). But the web page isn't very important, and they don't care very much. T

  • Change visibility of ASP.NET label with JavaScript 2008-08-11

    I have a ASP.NET page with an asp:button that is not visible. I can't turn it visible with JavaScript because it is not rendered to the page. What can I do to resolve this? --------------Solutions------------- If you need to manipulate it on the clie

  • Is there something like "Firebug for IE" (for debugging JavaScript)? 2008-08-12

    I'm trying to fix some JavaScript bugs. Firebug makes debugging these issues a lot easier when working in Firefox, but what do you do when the code works fine on Firefox but IE is complaining? --------------Solutions------------- you can also check o

  • How do I debug JavaScript in Visual Studio 2005? 2008-08-12

    I just saw this mentioned in Stack Overflow question Best WYSIWYG CSS editor and didn't know it could be done. I'm a Visual Studio newbie, so how do you do it? Is there a separate debugger for JavaScript? I know how to work the one for code-behind pa

  • Add a bookmark that is only javascript, not a URL 2008-08-12

    I'm thinking that the reason I can't do this is because it might be a huge security hole, but here goes... I want to have a bookmark on my browser (FF3, preferably) that is only a snippet of javascript code. It would merely construct a URL based on t

  • Running Javascript after control's selected value has been set 2008-08-13

    Simple asp.net application. I have two drop-down controls, on the first-drop down I have a javascript onChange event. The javascript enables the second drop-down and removes a value from it (the value selected in the first drop-down). If they click t

  • How do I add an additional window.onload event in Javascript 2008-08-13

    In my asp.net User Control I'm adding some script to the window.onload event like so: if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), onloadScriptName)) Page.ClientScript.RegisterStartupScript(this.GetType(), onloadScriptName, "windo

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.176 (s). 13 q(s)