最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery validation to compare start and end date - Stack Overflow

programmeradmin0浏览0评论

I have two fields. One for person_start_date and another for 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

<%= 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

<%= 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>

I have two fields. One for person_start_date and another for 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

<%= 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

<%= 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>
Share Improve this question asked Nov 18, 2013 at 5:55 SamSam 5,25012 gold badges48 silver badges98 bronze badges 4
  • Show us what you tried? – Kiran Commented Nov 18, 2013 at 5:58
  • What sort of values (e.g. 01, 1, Jan, January)? Which select is which (e.g. is person_start_date_1i the day, month or year)? – RobG Commented Nov 18, 2013 at 6:02
  • see the above, below the erb code there is a html – Sam Commented Nov 18, 2013 at 6:30
  • Here I can add only jQuery validator add method to validate. I can add any alert. So what I am searching for names to validate the fro and to fields with all the hints – Sam Commented Nov 18, 2013 at 7:11
Add a comment  | 

5 Answers 5

Reset to default 5

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.

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

UPDATE:

This link describes exactly your question and gives a good solution for it

jQuery validate date field for seperate year, month, date

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

发布评论

评论列表(0)

  1. 暂无评论