-1

in javascript, I have a from date input and to date input on client page, I am implementing validation between the two inputs. to date should greater than from date, my solution is convert them to Julian date and then compare them, how to convert the javascript date to Julian date?

Jerry
  • 39
  • 2
  • 5
  • 1
    possible duplicate of [Calculating Jday(Julian Day) in javascript](http://stackoverflow.com/questions/11759992/calculating-jdayjulian-day-in-javascript) - a little investigation helps. But my guess is that you do NOT want to convert to Julian if you just want to compare - [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – mplungjan Dec 03 '13 at 08:33
  • What have you tried? Where are you stuck? The [rules are fairly straightforward](http://en.wikipedia.org/wiki/Gregorian_calendar#Difference_between_Gregorian_and_Julian_calendar_dates). – T.J. Crowder Dec 03 '13 at 08:34

3 Answers3

2

You do not need Julian Date to compare.

You need Date.parse() function or compare by comparison operators. It will return the milliseconds that have passed since 01/01/1970 00:00

Somehow like this:

if(Date.parse(fromDate) < Date.parse(toDate){
   //start is less than End
}else{
   //end is less than start
}

Here is a Fiddle

Igl3
  • 4,900
  • 5
  • 35
  • 69
  • 1
    This doesn't answer the full question. In particular: "how to convert the javascript date to Julian date?" – T.J. Crowder Dec 03 '13 at 08:35
  • Unless it is an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – mplungjan Dec 03 '13 at 08:36
  • Formula for julian date is on wikipedia, thats easy to look up for himself, but the more beautiful solution would be to use Date functions – Igl3 Dec 03 '13 at 08:37
0

You don’t need a Julian date for date comparison, just created two JavaScript Date objects and compare them directly.

You can also compare them without creating Date objects, comparing them as strings, if you take advantage of the fact that strings are compared character-by-character, if you “format” them in a sortable way, f.e. YYYY-MM-DD HH:MM:SS. 2013-12-03 10:00:05 is “greater” than 2013-12-03 09:47:23 when compared char-by-char.

CBroe
  • 91,630
  • 14
  • 92
  • 150
0

You don't need to convert to julian date!

in javascript:

function isInRange(startD , date, endD ){
    return startD <= date && date <= endD;
}

var startD = new Date('11/01/2013');
var endD = new Date('11/30/2013');
var date = new Date('11/25/2013');


alert(  isInRange(startD , date, endD ) );
actual_kangaroo
  • 5,971
  • 2
  • 31
  • 45