0

How can I insert tomorrow's day's startTime and endTime into the content message for the setupOrderingNotAvailable function if today's day has an endTime that has expired? Right now the message setupOrderingNotAvailable says "...again tomorrow from 11:00am to 9:00pm" but that is not true for every day; this is where I would like to insert tomorrow's times.

Code

var days = {
    '1': {startTime: '4:00 PM', endTime: '8:00 PM'},
    '2': {startTime: '11:00 AM', endTime: '9:00 PM'},
    '3': {startTime: '11:00 AM', endTime: '9:00 PM'},
    '4': {startTime: '11:00 AM', endTime: '4:00 PM'},
    '5': {startTime: '11:00 AM', endTime: '10:00 PM'},
    '6': {startTime: '12:00 PM', endTime: '10:00 PM'},
    '7': {startTime: '12:00 PM', endTime: '8:00 PM'},
};

var curr_day = new Date().getDay();
var curr_time = getval();
var orderIsAvailable = false;
var day = days[curr_day];

// 
function setupOrderingAvailable() {
    document.querySelector('#alert-success').onclick = function() {
        $.sweetModal({
            content: '<h2>Online Ordering is Available!</h2>' +
                'Please click the button below to begin your online order.' +
                '<div class="sweet-modal-buttons"><a href="http://pizzospizzeria.hungerrush.com" target="_blank" class="button greenB">Start Order</a></div>',
            // icon: $.sweetModal.ICON_SUCCESS
        });
    };
}

// Need to make this a function that inserts tomorrows startTime and endTime into message if todays endTime has passed
function setupOrderingNotAvailable() {
    document.querySelector('#alert-success').onclick = function() {
        $.sweetModal({
            content: '<h2>Online Ordering is Not Available</h2>' +
                'Online ordering will be available again tomorrow from 11:00am to 9:00pm',
            buttons: [{
                label: 'Close',
                classes: 'redB'
            }]
        });
    };
}
if (day) {
    if (get24Hr(curr_time) > get24Hr(day.startTime) && get24Hr(curr_time) < get24Hr(day.endTime)) {
        orderingIsAvailable = true;
    } else {
        document.querySelector('#alert-success').onclick = function() {
            $.sweetModal({
                content: '<h2>Online Ordering is Not Available</h2>' +
                    'Online ordering will be available again tomorrow from 11:00am to 9:00pm',
                buttons: [{
                    label: 'Close',
                    classes: 'redB'
                }]
            });
        }
    }
}

if (orderingIsAvailable) {
    setupOrderingAvailable();
} else {
    setupOrderingNotAvailable();
}
6
  • so, uhm... where's the json string? did you mean object? the two are not the same. Commented Apr 5, 2017 at 18:52
  • sorry, the content message. Commented Apr 5, 2017 at 18:53
  • Get the times from tomorrow's day and concatenate then into the message. Are you not sure how to combine strings? Commented Apr 5, 2017 at 19:29
  • thanks @MikeMcCaughan - I know how to combine strings, just not clear on how to display tomorrows data based on todays. Commented Apr 5, 2017 at 19:47
  • Well, it appears as though you're getting today's day by using new Date().getDay(), so tomorrow's day would use new Date().getDay() + 1, right? Commented Apr 5, 2017 at 19:49

2 Answers 2

1

I cleaned up the time comparison code a little by determining the time in ms for the start and end time. This allows a cleaner (IMO) comparison of the time right now as a number vs the start and end times. The day integer returned from the Date object ranges from 0-6, so I updated that too.

From there you do the comparison and if ordering can't happen now

Here's a fiddle, albeit with the sweetModal aspect stripped out, but the string is created just the same no matter how you display it.

https://jsfiddle.net/wx97g6x1/2/

var days = {
  0: {
    startTime: '4:00 PM',
    endTime: '8:00 PM'
  },
  1: {
    startTime: '11:00 AM',
    endTime: '9:00 PM'
  },
  2: {
    startTime: '11:00 AM',
    endTime: '9:00 PM'
  },
  3: {
    startTime: '11:00 AM',
    endTime: '4:00 PM'
  },
  4: {
    startTime: '11:00 AM',
    endTime: '10:00 PM'
  },
  5: {
    startTime: '12:00 PM',
    endTime: '10:00 PM'
  },
  6: {
    startTime: '12:00 PM',
    endTime: '8:00 PM'
  }
};

var now = new Date();
var time = now.getTime();
var day = days[now.getDay()];

var startToday = Date.parse(now.toDateString() + ' ' + day.startTime);
var endToday = Date.parse(now.toDateString() + ' ' + day.endTime);

function setupOrderingAvailable() {
  console.log('Online Ordering is Available!');
}

function setupOrderingNotAvailable() {
  var tomorrow = days[now.getDay() + 1];
  console.log('Online ordering will be available again tomorrow from '+ tomorrow.startTime +' to '+ tomorrow.endTime)
}

if (time > startToday && time < endToday) {
  setupOrderingAvailable();
} else {
  setupOrderingNotAvailable();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Look into Template Literals

const days = {
    '1': { start: 1, end: 2 },
    '2': { start: 3, end: 4 }
}
const string = `day 2 starts at: ${days['2'].start} and ends at: ${days['2'].end}`
console.log(string); // "day 2 starts at: 3 and ends at: 4"

In addition to using the fact that each day's "value" is 1 greater than the previous.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.