Aritra
Aritra

Reputation: 23

Closest next day and time in javascript

The scenario is : On hover of a button, next available time the offer will come will be seen. I used the code

function closest (num, arr) {
        var closestNext=10000;
            for (var val = 0; val < arr.length; val++) {
              if (arr[val] > num) {
                if (arr[val] < closestNext)
                {
                    closestNext = arr[val];
                    }
              }

            }
            if (closestNext==10000){
                closestNext=arr[0];
            }
            return closestNext;

        }

This lets me display for a single day(I am taking the page load tome in javascript). But now problem is if an offer comes say every thurs and fri between 10am to 11 am. How to display that data when the offer is closed? Elaborately : Suppose an offer comes everyday at 4pm. Rest of the time on hover of the offer display button it shows "next offer ay 4pm". This I have solved by using an array and the current time to calculate and display. Now the problem is suppose an offer comes every thurs fri at 10am, then on wed it must show offer comes at "thurs 10am".

Upvotes: 0

Views: 149

Answers (1)

Anurag
Anurag

Reputation: 3114

If I am able to understood your problem correctly then here is the function which takes the next offer day and time and return appropriate msg. Here day is 1-7 (mon-sun) and time in 24 hour format.

function getNextOffer(offer_day, offer_time) //offer_day = 4, offer_time = 16
{
    var msg = '';
    var today = new Date();
    var day_today = today.getDay();
    var time_today = today.getHours()

    if(offer_day != day_today) //different day
    {
        msg = "thurs " + offer_time; //this msg can be manipulated
    }
    else
    {
        if(time_today > offer_time) //time elapsed
        {
            msg = "next thurs " + offer_time;
        }
        else
        {
            msg = "next offer by " + offer_time;
        }
    }

    return msg;
}

EDIT: for multiple values changed the logic little bit

//example array of offers [Fri, 12:00pm], [Thurs, 1:00pm], [Sat, 7:00pm]
var days = [[5, 12], [4, 13], [6, 19]];

function getNextOffer(days)
{
    var msg = '';
    var today = new Date();
    var day_today = today.getDay();
    var time_today = today.getHours();
    var pre_offer_day = 8;
    var pre_offer_time = 25;

    for(i=0; i < days.length; i++)
    {
        var offer_day = days[i][0];
        var offer_time = days[i][1];

        if(pre_offer_day >= offer_day)
        {
            if(offer_day > day_today)
            {
                msg = "next " +  offer_day + " at " + offer_time;
                pre_offer_day = offer_day;
                pre_offer_time = offer_time;
            }
            else if(offer_day == day_today && offer_time > time_today)
            {
                msg = "next " +  offer_day + " at " + offer_time;
                pre_offer_day = offer_day;
                pre_offer_time = offer_time;
            }
        }
    }

    \\if no more offers in this week set the default msg
    if(pre_offer_day == 8 && pre_offer_time == 25)
    {
        msg = msg = "next offer by thurs 10am";
    }
    return msg;
}

Upvotes: 1

Related Questions