0

I am working on a web application, where one of the functionalities is to provide billboard listings based on a date that you insert.

I found this great unofficial API for Billboard charts: http://billboard.modulo.site/

As you can see in the documentation you can provide a date for the REQUEST and get the official "hot 100" listings for that date.

However, one constraint with this is that the date provided has to be a saturday(that's when Billboard updates their charts each week). If you input a date that is not a saturday the response is a json with empty arrays.

If I present the user with the option to select the year, month, and date(or week, doesn't matter) what would be the most efficient solution in Javascript to convert that to the date of the saturday on the same week?

For instance let's say the user inputs the following date: 2016-03-04 (which is a Friday)

How could I check what day of the week 2016-03-04 is - so that I could change that to 2016-03-05(which is the saturday)?

It would be even more pleasant if the user input would be

  • Year: 2016

  • Month: March

  • Week: 9

and then get the date of the saturday of week 9 but not sure if that's possible.

The simplest solutions seems to just see which day of the week a provided date is, but not sure how one could go through with that.. Any ideas?

user3043462
  • 456
  • 1
  • 5
  • 7

3 Answers3

3

You can use this to get the date of the next Saturday for a given date.

var date = new Date();
date.setDate(date.getDate() + (1 + 5 - date.getDay()) % 7);
console.log(date);

Inspiration

Community
  • 1
  • 1
edwarddamato
  • 3,130
  • 1
  • 11
  • 8
1

I would guide the user by providing a date picker and ensuring that only Saturdays can be selected.

The jQuery datepicker can be used, for example, and restricted this way. See this link for a way to do this.

This is good from a user perspective and makes your job easier in terms of not having to calculate the 'nearest Saturday'.

It is pretty easy to do. Here's a working sample:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Datepicker - Default functionality</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        function onlySaturdays(date) {
            return [!!(date.getDay() === 6)];
        }
        $( function() {
            $( "#datepicker" ).datepicker({ beforeShowDay: onlySaturdays });
        } );
    </script>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>


</body>
</html>
Community
  • 1
  • 1
rasmeister
  • 1,986
  • 1
  • 13
  • 19
0

Do something like this:
JavaScript:

var dateString = document.getElementById('input');
var d = new Date(dateString);
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

var n = weekday[d.getDate()];
if n = "Saturday" {
//code here
}
else {
//code here for switching
}
Sam
  • 295
  • 4
  • 17