Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 3336
In Process
Anonymous
Anonymous
Asked: August 22, 20222022-08-22T08:44:05+00:00 2022-08-22T08:44:05+00:00

Javascript, hide/show div based on array of dates

I’m working for a delivery company and the boss wants me to add the daily schedule of the “delivery bus” to the website. The boss wants it to only show the schedule (“bus stops”) for the current day. While not a programer myself I found some code online and adjusted it to what I need.

So what I did is put the schedule in different divs, hid them all and use jquery to only show divs based on the date.

The thing is the way I did it, each day is a new date() and I’d need to add about 150 lines of code for each day the bus goes on the road. So, I’m looking for a better solution, perferably to the existing code, because I sort of understand it. I’m looking into creating a array of dates, but so far I’m hitting a wall. As I said I’m not a programer. Any ideas?

Working fiddle https://jsfiddle.net/ezdv8rwL/

Just change the date if you’re not looking at it today

javascript
  • 0
  • 1 1 Answer
  • 19 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Simmon
    2022-08-22T08:44:21+00:00Added an answer on August 22, 2022 at 8:44 am

    The JavaScript Date constructor accepts the following for dates based on numbers: new Date(year, monthIndex, day). You may be able to use a for loop to more dynamically create a bunch of dates.

    const days = {}
    const today = new Date();
    
    for(let i = 0; i < 7; i++) {
        day[i]= today.getTime() + i * 24 * 60 * 60 * 1000
    }
    

    I’m guessing though this is a weekly schedule, so it may be easier to set it to figure the weekday?

    const schedule = {
        0: Poljubinj,
        1: Čezsoča,
        2: Podmelec,
        3: Volce,
        4: nic
    }
    
    const today = new Date().getDay();
    

    The .getDay() function gets the day as an integer, where Sunday is considered the first day (with value 0).

    Quick lookup:

    • Sunday: 0
    • Monday: 1
    • Tuesday: 2
    • Wednesday: 3
    • Thursday: 4
    • Friday: 5
    • Saturday: 6

    You can replace the check if (current.getDate() == day.getDate()) with a simple if (schedule[today] === [name of the day).

    More advanced would be to replace all the if statements with a switch - case

    switch(schedule[today]) {
        case [name of the day]:
            showDay(today);
            break;
        default:
            showDay(// number of default day?);
            break;
    }
    

    You may add as many “case” lines as you want. A common pitfall with switch-case is that the break is required, as a thing called fallhrough would allow to have code of multiple consecutive cases executed until a break or return is reached.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.