9

I have a rails-generated date, and a jQuery-generated date.

The rails date prints as such: 2002-10-27

and the jQuery date prints as such: Tue Aug 14 2001 00:00:00 GMT-0500 (CDT)

I want to check if the jQuery date is greater or less than the rails date. But no matter the dates, the jQuery date is always interpreted as larger than the rails date.

Why is that, and how can I successfully compare the two dates?

var year = 2001
var month = 9
month --
var day = 14
var date = new Date(year, month, day);
<% @date = Date.today - 18.years %>
if( date > <%= @date %> ) {
  //this code is always executed, no matter what dates I choose
}

UPDATE:

Actually I just figured out the problem is that it only allows dates before 1969. I intended the code to only allow dates over 18 years old. Does anyone know why the difference?

UPDATE 2:

I tested the time output of October 5th, 2000 in my js console and rails consoles, and they give the same first six digits, but the js console adds three zeros.

var year = 2000
var month = 10
month --
var day = 5
var date = new Date(year, month, day);
date.getTime();
=> 970722000000


Date.new(2000,10,5).to_time.to_i
=> 970722000 
Jeff Caros
  • 919
  • 5
  • 12
  • 32

5 Answers5

4

So it turns out the issue is that the js console prints times in milliseconds, which is why I was getting 973404000000, versus 973404000 in the rails console.

All I had to do was divide the js time by 1000, and comparing the js time to the rails time works perfectly.

var year = 2000
var month = 10
month --
var day = 5
var date = (new Date(year, month, day).getTime() / 1000);
date
=> 970722000


Date.new(2000,10,5).to_time.to_i
=> 970722000 
Jeff Caros
  • 919
  • 5
  • 12
  • 32
  • Another way: Javascript can provide date as `var date = new Date(); date.toLocaleDateString(); // "11/1/2015"` in this format and then you compare it server side easily. – vinayakj Oct 31 '15 at 19:58
3

User date Format patterns for Jquery Date, for changing the format of date according to ruby date , or second option is convert ruby date format according to jquery , using strftime.

Anil Yadav
  • 1,086
  • 7
  • 18
1

You might try converting them both to their unix timestamps and comparing those. If you don't care about the hours, and simply the dates, it should work.

var year = 2001
var month = 9
var day = 14
var date = new Date(year, month, day);
<% @date = Date.today - 18.years %>
if ( date.getTime() > <%= @date.to_time.to_i %>) {
  // do something
}
yez
  • 2,368
  • 9
  • 15
  • Hm, this is strange. The way I intended the code, it should prevent anyone under 18 from signing up. But apparently the problem is, it only allows anyone born in 1969 or before. Any idea why that would be? – Jeff Caros Oct 28 '15 at 00:24
  • Ah, you must have an issue with some date parsing. If ever you get the date 12-31-1969 when you parse a date, that resolves to Unix timestamp 0. I'd check that everything is parsing correctly in JS and in Ruby. – yez Oct 28 '15 at 00:44
  • For javascript you can check the parsing in the developer console (inspect element on chrome). In a console in ruby (rails c or irb), just do `@date.to_time.to_i` and compare the numbers. – yez Oct 28 '15 at 02:26
  • I ran October 5 2000. JQuery returns 973404000000, and rails returns 970704000 . Does that mean I have a parsing error? – Jeff Caros Oct 28 '15 at 02:45
  • Since they are two different numbers I'd double check have the exact same dates in js and in ruby. In ruby you should be getting: `Date.new(2000, 10, 5).to_time.to_i => 970729200` – yez Oct 28 '15 at 06:18
  • Weird. Now I'm getting `Date.new(2000, 10, 5).to_time.to_i => 970722000 `..what's that about? – Jeff Caros Oct 28 '15 at 06:19
  • You are probably in a different time zone than me, It shouldn't matter too much, since your goal is determining that someone is over a certain age. – yez Oct 28 '15 at 19:25
1

I'd use a library like Moment.JS to handle your date parsing needs on the client side. And then send the server something in a standard format like ISO8601 to ensure you don't have any problems in misintrepretation.

Epoch time will work as well, but as you've seen you have to carry the burden of ensuring that they're in the same units.

Rich Seviora
  • 1,789
  • 12
  • 16
0

To compare 2 dates for me u can use moment.js

With ur rails date create a moment date. Do the same with ur jquery date.

U can compare easily the 2 dates now.

If u need help see this post : moment-js-date-time-comparison

Community
  • 1
  • 1