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