问题
I have a quick one that I'm struggling with after a long day!
I have a form with a hidden field with the name of 'grant_cycle'.
If the form is submitted after January 15, it should be give the value of 'Spring, [year]', or after July 15 should be 'Fall, [year]'.
Could you kind people send me in the right direction, please? :-)
Thanks!
EDIT: Added year bits.
回答1:
One option would be:
- retrieve the year of submission first
- create the date of January 15th in that year
- create the date of July 15th in that year
- check if the value is between January 15th and July 15th. If it is, then use Spring, else use Fall.
Code
var submitDate = new Date();
var currentYear = submitDate.getFullYear();
var jan15 = new Date('Jan 15 ' + currentYear);
var jul15 = new Date('Jul 15 ' + currentYear);
if (jul15.getTime() <= submitDate.getTime()) {
// Set hidden value to "Fall, " + currentYear
}
else if (jan15.getTime() <= submitDate.getTime()) {
// Set hidden value to "Spring, " + currentYear
}
else { // This is the case for January 1st to 14th of the submitDate year
// Set hidden value to "Fall, " + (currentYear - 1)
}
来源:https://stackoverflow.com/questions/17350586/javascript-set-hidden-field-if-date-after-jan-15