-4

I want to compare following date in Javascript. Please help me.

$fromdate=2014-12-08
$todate=2014-12-12

I want to compare both the dates with current date. Please tell me how to code in if loop.

11mb
  • 1,339
  • 2
  • 16
  • 33
Devendra
  • 29
  • 1
  • 1
  • 5
  • 2
    show us what you have tried – Pinoniq Jan 09 '15 at 12:04
  • A quick google search return already several duplicates on SO: https://www.google.ch/search?q=compare%2Bjavascript%2Bdates&oq=compare%2Bjavascript%2Bdates&aqs=chrome..69i57j0l5.483j0j4&sourceid=chrome&es_sm=93&ie=UTF-8 – RononDex Jan 09 '15 at 12:07
  • What are you expecting as a result from your comparison? – Xotic750 Jan 09 '15 at 12:31
  • By comparing the date coming from my input field, i will compare it with current date and use it to display it in datepicker – Devendra Jan 09 '15 at 12:33
  • But what would the result of your comparison be? If it is today's date then `true` otherwise `false`, or perhaps the difference from today's date to the compared date in days, what? – Xotic750 Jan 09 '15 at 12:36
  • For e.g if $Fromdate=2015-01-06 and $todate=2015-01-15 I will get todays date i.e 2015-01-09 and will enable the datepicker only from 2015-01-09 to 2015-01-15 I will disable the dates before todays date – Devendra Jan 09 '15 at 12:41
  • So you are saying that want to know if today's date falls between a give date range, if it does then true otherwise false? – Xotic750 Jan 09 '15 at 12:42
  • yup exactly....and it should also help me set the datepicker only for future days and current day of that given date range – Devendra Jan 09 '15 at 12:45
  • 1
    Why didn't you explain that more clearly in the question? And what have you tried, show us your code? No code, not tried, have you read [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) and even had any thoughts about how you may be able to do this? – Xotic750 Jan 09 '15 at 12:50

2 Answers2

1

You can construct a Date object and then compare them however you want:

// Date months are 0-based
var fromDate = new Date(2014, 11, 8);
var toDate = new Date(); // Today

// Then you can calculate the difference between them
var seconds = (toDate.getTime() - fromDate.getTime())/1000;
var minutes = ~~ (seconds/60);
var hours = ~~ (minutes/60);
var days = ~~ (hours/24);

Then you can use the diff to calculate how many seconds, hours, days etc. there are between them.

Eric
  • 18,532
  • 2
  • 34
  • 39
0

A date can be represented numerically as a count of milliseconds from an epoch (01 January, 1970 UTC in javascript).

Create a Date object for today, start and end dates.

Get the numerical representation of these dates by using the method getTime

Now compare today's representation with the start and end representations.

IF the numerical value of today is greater than or equal to the start numerical value, AND is less than or equal to the end numerical value, THEN today falls within the supplied range, ELSE it does not.

Xotic750
  • 22,914
  • 8
  • 57
  • 79