javascript:如果日期在1月15日之后,则设置隐藏字段 [英] javascript: set hidden field if date after Jan 15

查看:110
本文介绍了javascript:如果日期在1月15日之后,则设置隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个隐藏字段名为'grant_cycle'的表单。如果表格是在1月15日之后提交的,那么应该给出'春季,[年]'的值,或者7月15日之后应该是'秋季,[年] ]'。



拜托你能请人正确指导我吗? : - )



谢谢!

编辑:新增年份。
<一个选项是:


  1. 首先检索提交年份 / li>
  2. 创建当年1月15日的日期

  3. 创建当年7月15日的日期

  4. 检查值是在1月15日到7月15日之间。如果是,则使用Spring,否则使用Fall。

代码 b

  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()){
//设置隐藏值为Fall,+ currentYear
}
else if(jan15 .getTime()< = submitDate.getTime()){
//将隐藏值设置为Spring,+ currentYear
}
else {// 1月1日到submitDate年的第14天
//将隐藏值设置为Fall,+(currentYear - 1)
}


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.

解决方案

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)
}

这篇关于javascript:如果日期在1月15日之后,则设置隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆