0

I am developing a website for our school and we need to change schedules based on terms. I need to switch the variable term to

fall on September 1st,

winter on January 1st, and

spring on March 15th.

How would I accomplish this on Javascript? Any help is appreciated. Thank you very much.

Teddy Hwang
  • 131
  • 9
  • 3
    Please show [what you've attempted to do](http://idownvotedbecau.se/noattempt/) and [any research you've done on the subject](http://idownvotedbecau.se/noresearch/). Stack Overflow is not designed to be a code writing service. – Heretic Monkey May 07 '19 at 00:01
  • 1
    Figure out how to get [dates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), then how to [compare dates](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript), and you should be able to figure this out – adeneo May 07 '19 at 00:02
  • I suggest that you learn about `if` statements. – Code-Apprentice May 07 '19 at 00:13

2 Answers2

2

In order to do this, you have to create an instance of the built-in Date object and call its getMonth() and getDate() method.

var termUpdater = new Date();
updateDate = termUpdater.getDate();
updateMonth = termUpdater.getMonth();

getDate() returns 1-31 based on the current date. getMonth() returns 0-11 based on the current month. By default, the Date object will refer to the current date and time. But you can also pass an optional argument for it to force to refer to some other date which is unnecessary in your case but still here's how it's done:

var dateObj = ("January 1, 1983 01:15:00");

Now, you can say:

if (updateMonth == 0 || updateMonth == 1){ //0 means Jan
      term = winter;
}
else if ((updateDate >= 15&& updateMonth==2) ||  (updateMonth <= 7 && updateMonth > 2  )){
      term = spring;
}
else if ( updateMonth >= 8){
     term = fall;
}
Amal K
  • 4,359
  • 2
  • 22
  • 44
1

Start by describing the rules in more detail. For example

if date is between Sept 1 and Jan 1
  then term is "fall"
else if data is between Jan 1 and Mar 15
  then term is "winter"
else if date is between Mar 15 and ?? (is it all the way until Sep 1?)
  then term is "spring"

Note that there is still some missing information that needs to be filled in. The next step is to translate this into JavaScript. The first part to notice is that I intentionally use the word "if" many times in the above description. That is because I know that JavaScript has the handy if keyword that I can use for situations like this. If you are unfamiliar with if statements, then you definitely need to learn about them as they are one of the most important parts of JavaScript.

The next step is to figure out how to figure out if the current date is between two dates. The first step is to get the current date. I suggest you use google to find more information about how to do this step. From there you should also google how to compare dates in JavaScript.

Good luck!

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268