开发者

javascript function to check if actual time is between opening and closing time of an event

i'm working on a website where users can choose various events and add them to their timeline. Every event has an opening time and a closing time, they're saved as ints in the db as of now. For example an events open at 9 and closes at 18. I need a javascript function to check if the event is open when the user adds it...to make things simple let's suppose that i alwais check against the actual time. I've got no problems to check in a normal situation (the events start at 9 and closes at 18), i'm finding some problems with events that start at 18 and end at 2 o clock. I've divided events in two classes: those where the end date is > than the starting date and those where the end date is < than the starting date, but i don't know how to check in this second case!

Thx for your help.

EDIT this is the code i used and that worked for me (i was taking the date for checking from html), maybe it helps someone;

        var dataSelezionata = $('#dataSelezionata').html();
        var dataExplode = dataSelezionata.split("/");
        var dataSelezionata = dataExplode[1]+"/"+dataExplode[0]+"/"+dataExplode[2];
        dataSelezionataJavascript = new Date(dataSelezionata);
        var oreAttuali = dataSelezionataJavascript.getHours();
        var oraInizio = parseInt($('#oraInizio').html(), 10);
        var oraFine = parseInt($('#oraFine').html(), 10);
        if (oraInizio && oraFine){
            if (oraFine > oraInizio){
                console.log('ora prima delle 24');
                if (!(oreAttuali >= oraInizio && oreAttuali < oraFine)){
                    alert ("L'evento da lei selezionato non e' disponibile il giorno "+dataSelezionataJavascript.toLocaleDateString()+" alle "+dataSelezionataJavascript.toLocaleTimeString()+"");
                    return;
                }
            }
            if (oraFine < oraInizio){
                console.log('ora fine dopo le 24');
                var tempoApertura = (24 - oraInizio) + oraFine;
                var dataInizio = new Date (dataSelezionataJavascript.getFullYear(), dataSelezionataJava开发者_JS百科script.getMonth(), dataSelezionataJavascript.getDay(), oraInizio);
                var dataFine = new Date (dataSelezionataJavascript.getFullYear(), dataSelezionataJavascript.getMonth(), dataSelezionataJavascript.getDay(), oraInizio+tempoApertura);
                if (!(dataSelezionataJavascript >= dataInizio && dataSelezionataJavascript < dataFine)){
                    alert ("L'evento da lei selezionato non e' disponibile il giorno "+dataSelezionataJavascript.toLocaleDateString()+" alle "+dataSelezionataJavascript.toLocaleTimeString()+"");
                    return;
                }
            }
        }


For events that run over midnight (or any event for that matter), just add the duration of the event to the start time, so for an 8 hour event:

// Set start
var start = new Date(2011, 4, 3, 18);  // 3 May, 1800

// Set finish for 8 hours later
var finish = new Date(2011, 4, 3, 18+8); // 4 May, 0200

And do the date comparison as suggested by thomasa88.


Why not use the built-in javascript Date object: http://www.w3schools.com/js/js_obj_date.asp

Something like:

start = new Date(2011, 3, 10, 8, 0, 0); //Note that the month is 0-indexed
end = new Date(2011, 3, 10, 10, 0, 0);
now = new Date();

if(start <= now && now < end)
{
    // Date & time ok
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜