javascript: set hidden field if date after Jan 15

依然范特西╮ 提交于 2019-12-11 14:56:29

问题


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:

  1. retrieve the year of submission first
  2. create the date of January 15th in that year
  3. create the date of July 15th in that year
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!