-1

I have a function where I wanted to get the week number of today's date but I can't print the value on my textbox on button click. Im new to javascript and I've been debugging this for weeks. Please help

function getWeekNumber(d) {
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    return [d.getUTCFullYear(), weekNo];
 document.getElementById("week").value = getWeekNumber(new Date());
 
}
<td>WEEK: </td>
   <td><input type="text" value= "" style= "width: 230px; padding-left: 3px" id="week" name="week"></td>
          <input type="button" value="Get Week" onclick= "getWeekNumber()">
Blue Minnie
  • 231
  • 1
  • 12
  • 1
    you call `function getWeekNumber(d)` like `getWeekNumber()` so `d` is undefined ... what should it be? – Jaromanda X Jun 14 '19 at 01:08
  • This is my reference code https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php – Blue Minnie Jun 14 '19 at 01:10
  • so? use the code in that answer correctly - you're also returning before the end of the function, fortunately, since you'd end up with infinite recursion – Jaromanda X Jun 14 '19 at 01:13

1 Answers1

2

Firstly, your function "getWeekNumber()" requires a parameter d when called, but when you call it onclick of your input button, you give it no such parameter. I think rather, you should set up an event listener for when the input button is clicked, and from there you should call the getWeekNumber() function and pass it "new Date()" as the parameter d, so it would look like the below code where I added an event listener.

You were also calling the function itself from within, but doing so after a return statement which was redundant. Instead, you should get the result weekNo and set the innerHTML of some div to it, as I did. I don't think you should try to fill in a input box, as that doesn't seem conventional. I changed it to a div to make the process simpler and easier:

let get_week_button = document.getElementById("get_week");

get_week_button.addEventListener("click", function() {
  getWeekNumber(new Date());
});

function getWeekNumber(d) {
  d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
  d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
  var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
  var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);

  document.getElementById("week").value = weekNo;

}
<td>WEEK: </td>
<td>
  <input type="text" value= "" style= "width: 230px; padding-left: 3px" id="week" name="week">
</td>
</tr>
<input id="get_week" type="button" value="Get Week">
Musilix
  • 330
  • 2
  • 14
  • But I needed the value to be in textbox @Musilix – Blue Minnie Jun 14 '19 at 01:17
  • Alright, if that's what you truly need I edited the code to acknowledge that. I just changed the div I made back to the input element you were using, and instead of setting innerHTML, I set its value attribute. – Musilix Jun 14 '19 at 01:20
  • Still can't get it to work. Am I missing something with this? @Musilix – Blue Minnie Jun 14 '19 at 01:23
  • It's working in the above snippet. Make sure to designate your js and html in separate files with the correct extensions, and make sure script tags are set correctly in the html file to reference the js file. There is no need for jQuery, this is vanilla js; unless you are using jQuery for something else in your file. – Musilix Jun 14 '19 at 01:25