简体   繁体   English

jQuery验证来比较开始和结束日期

[英]jQuery validation to compare start and end date

I have two fields. 我有两个领域。 One for person_start_date and another for person_end_date. 一个用于person_start_date,另一个用于person_end_date。 Both has three separate fields for year, month, date. 两者都有三个单独的字段,分别为年,月,日。 I would like to validate like Person end date > Person start date. 我想验证像人员结束日期>人员开始日期。 Thanks in advance. 提前致谢。

html.erb html.erb

<%= datetime_select :person_start_date %>


   <select id="person_start_date_1i" name="person[start_date(1i)]"> ... </select>
   <select id="person_start_date_2i" name="person[start_date(2i)]"> ... </select>
   <select id="person_start_date_3i" name="person[start_date(3i)]"> ... </select>

html.erb html.erb

<%= datetime_select :person_end_date %>


  <select id="person_end_date_1i" name="person[end_date(1i)]"> ... </select>
  <select id="person_end_date_2i" name="person[end_date(2i)]"> ... </select>
  <select id="person_end_date_3i" name="person[end_date(3i)]"> ... </select>

Here is a suggestion : 这是一个建议:

var startDate = new Date(
    $('#person_start_date_1i').val(),
    $('#person_start_date_2i').val() - 1,
    $('#person_start_date_3i').val()
);
var endDate = new Date(
    $('#person_end_date_1i').val(),
    $('#person_end_date_2i').val() - 1,
    $('#person_end_date_3i').val()
);
if (endDate > startDate) {
    // do something
}

If you have year, month and day values as numbers (they can be Type number or string), you can create a Date like: 如果您将年,月和日值作为数字(可以是类型编号或字符串),则可以创建日期,如:

var dStart = new Date(year, --month, day);

Note that for calendar month 1 the javascript month number is 0 (zero) as months are zero indexed, hence --month . 请注意,对于日历月1,javascript月号为0(零),因为月份为零索引,因此 - --month

and you can compare two dates directly: 你可以直接比较两个日期:

if (dEnd < dStart) {
  // end is before start
}

I think either of these links should answer your question in general: 我认为这些链接中的任何一个都应该回答你的问题:

validate end date equal to greater than start date 验证结束日期等于大于开始日期

Validate that end date is greater than start date with jQuery 使用jQuery验证结束日期是否大于开始日期

UPDATE: 更新:

This link describes exactly your question and gives a good solution for it 此链接准确描述了您的问题并为其提供了一个很好的解决方案

jQuery validate date field for seperate year, month, date jQuery验证单独的年,月,日的日期字段

http://jsfiddle.net/GGsMx/ http://jsfiddle.net/GGsMx/

cant get simpler than this.. 不能比这更简单..

var date1 = Date.parse("2012-11-18");
var date2 = Date.parse("2011-11-18");
if (date1 > date2) {
    alert ("Error!");
}
else{
    alert("correct")
}

The important aspect is that such comparison fails because of date format used. 重要的一点是,由于使用了日期格式,这种比较失败了。 Mostly it works with mm-dd-yyyy format because PC have same regional settings and it looks like correct format, however thats not the case. 它主要使用mm-dd-yyyy格式,因为PC具有相同的区域设置,看起来像是正确的格式,但事实并非如此。 For compatibility various languages/platforms keep dates in year-month-date format. 为了兼容性,各种语言/平台以年 - 月 - 日期格式保留日期。 Hence, it is best to explicitly convert date to yyyy-mm-dd format and do the conversion, specially when you are using dd-mm-yyy (Aus or European format). 因此,最好将日期显式转换为yyyy-mm-dd格式并进行转换,特别是在使用dd-mm-yyy(Aus或欧洲格式)时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM