Javascript编码:输入具体日期,输出季节 [英] Javascript coding: Input a specific date, Output the season
问题描述
< body>
< h1>到季节的日期< / h1>
< p align =left>输入日期:
< input id =seasonmaxlength =4size =24/>
< / p>
< input type =buttonvalue =Submitonclick =Submit()/>< br />
< textarea cols =40rows =3id =output>< / textarea>< / body>
< script type =text / javascript>
function Submit(){
var date = document.getElementById('season')。value;
var seasons =
if(
请注意,有这样做的更为简单的方法(例如,使用例如日期对象),这可能更有用/更灵活,特别是如果要根据实际的季节开始/停止日期(例如3月28日),这只是为了展示出发点。
这是一个非常简单的例子,使用swtich()根据一个数字月份:
< form name =date>
< input type =text name =month/>
< input type =buttonvalue =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);
}
这里有一个更复杂的例子,显示短/长的月份加上数字月。
http://jsfiddle.net/ RtC58 / 1 /
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':
'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);
}
有一点不同的方法可能是为季节创建变量,使用if / else语句(如OP想要的一个例子),并在其中一个变量中找到index的月份值(注意我添加了一个,
[逗号]到月底,消除1从12和1从0,等...)。
function getSeason(){
month = document.forms.date.month.value.toLowerCase()+,;
winter ='dec,十二月,jan,january,feb,二月,12,1,2'
spring ='mar,march,apr,april,may,3,4,5,';
summer ='jun,june,jul,july,aug,august,6,7,8,';
fall ='sep,9月,oct,10月,nov,11月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);
}
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);
}
这篇关于Javascript编码:输入具体日期,输出季节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!