Hello I a very new to Javascript and I need to make a code that allows someone to enter a date and it will result in the season that date is in. I am at a loss at what I should do, an if else statement? This is as far as I got, thank you for everyone's time!
<body>
<h1>Date to Season</h1>
<p align="left">Enter Date:
<input id="season" maxlength="4" size="24"/>
</p>
<input type="button" value="Submit" onclick="Submit()"/><br/>
<textarea cols="40" rows="3" id="output"></textarea></body>
<script type="text/javascript">
function Submit(){
var date = document.getElementById('season').value;
var seasons =
if (
Note, there are saner ways of doing this (for instance, using a date object for instance), that would probably be more useful/flexible, especially if you want to determine by the actual season start/stop date (March 28th, for example). This is just to demonstrate a starting point.
Here's a very simple example, using a swtich() to return a season according to a numeric month:
<form name="date">
<input type="text" name="month"/>
<input type="button" value="Season?" onClick="getSeason()"/>
</form>
function getSeason() {
month = document.forms.date.month.value;
season = '';
switch(month) {
case '12':
case '1':
case '2':
season = 'winter';
break;
case '3':
case '4':
case '5':
season = 'spring';
break;
case '6':
case '7':
case '8':
season = 'summer';
break;
case '9':
case '10':
case '11':
season = 'fall';
break;
}
alert(season);
}
Here's a little bit more complex example, showing the short/long month plus the numeric month.
function getSeason() {
month = document.forms.date.month.value.toLowerCase();
season = 'unknown';
switch(month) {
case 'dec':
case 'december':
case '12':
case 'jan':
case 'january':
case '1':
case 'feb':
case 'february':
case '2':
season = 'winter';
break;
case 'mar':
case 'march':
case '3':
case 'apr':
case 'april':
case '4':
case 'may':
case '5':
season = 'spring';
break;
case 'jun':
case 'june':
case '6':
case 'jul':
case 'july':
case '7':
case 'aug':
case 'august':
case '8':
season = 'summer';
break;
case 'sep':
case 'september':
case '9':
case 'oct':
case 'october':
case '10':
case 'nov':
case 'november':
case '11':
season = 'fall';
break;
}
alert(season);
}
A little bit different approach could be to create variables for the seasons, use if/else statements (as the OP wants an example of) and find the 'index of' the month value in the one of the variables (note I added a ,
[comma] to the end of the month to disambiguate 1 from 12 and 1 from 0, etc...).
function getSeason() {
month = document.forms.date.month.value.toLowerCase()+",";
winter = 'dec,december,jan,january,feb,february,12,1,2,';
spring = 'mar,march,apr,april,may,3,4,5,';
summer = 'jun,june,jul,july,aug,august,6,7,8,';
fall = 'sep,september,oct,october,nov,november,9,10,11,';
season = 'unknown';
if (winter.indexOf(month) != -1) {
season = 'winter';
} else if (spring.indexOf(month) != -1) {
season = 'spring';
} else if (summer.indexOf(month) != -1) {
season = 'summer';
} else if (fall.indexOf(month) != -1) {
season = 'fall';
}
alert(season);
}