0

Although there are similar threads regarding this issue, I am more concerned about a specific problem and displaying it as a text and not as an alert

So far, I have learned an adequate portion of HTML (I'm still a newbie though). The main reason behind this is because I am publishing a website for my classroom in my school and am constantly looking for ways to add new elements.

My 'Homework' Page has one piece of Homework that we get every week on the same day. Instead of me having to rewrite the due day for the Homework, I want to find a way that will change the date using JavaScript.This is how it looks like currently on my website but I have to manually change it every week.

The date (for example) will have "Monday, 12th of March, 2018" or a similar variation. The coding aspect goes: if we reach the above date in real time, the date is to change to the same day however in the next week. Then the date will say "Monday, 19th of March, 2018" or a similar variation. Is there anyway to achieve this?

Thanking in advance,

Sincerely,

Shafquat T.

  • 1
    Welcome to Stack Overflow. This site is intended to help with fixing issues you're having with code you've written, so you should first write some code, then ask questions about how to fix issues you're having. I suggest you first look at the Date object on [*MDN*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), then see questions here on how to [*add days to a date*](https://stackoverflow.com/questions/563406/add-days-to-javascript-date?s=1|550.8030). Then write some code and see how far you get, post again when you have issues. Good luck! – RobG Mar 10 '18 at 06:52

1 Answers1

-1

This can fairly be achieved using Date.js

Include <script src="https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js" type="text/javascript"></script> to your project.

Since you said the due date is always Monday of the next week, you can do something like this

<script>
  // Date format
  var date_format = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

  // Gets the next Monday
  var dueDate = Date.next().monday().toLocaleDateString('en-US', date_format);

  // Outputs to page
  document.getElementById("due_date").innerHTML = dueDate;
</script>

Get date.min.js

Good Luck!

crazychukz
  • 676
  • 1
  • 4
  • 10
  • Just to clarify, I'm using Google Sites to achieve this. When I insert the script source and script onto Sites, it is empty/blank. Also, I think I have explained it incorrectly; the JavaScript needs to identify the day presently. The new due date only comes up once we have established that we have reached the current due date. So, the system needs to first see if it is 12/03/18; only then it will change to the next Monday, and then once next Monday arrives it will alter to the upcoming Monday and so on... Maybe an if/else statement? What should I do? – Shafquat Tameem Mar 11 '18 at 02:59