Reputation: 11
I have store date in dd-mm-yy like(21-05-2010) and I am getting system date by
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();.
So I have to compare this two values and have to find out which one is greater. I need to compare in Javascript function .so is their any function which compair this two values.
Upvotes: 1
Views: 199
Reputation: 305
date11 = "2018-10-12";
date22 = "2018-12-02";
date1 = new Date(date11);
date2 = new Date(date22);
if(date1>date2){
alert("date1>date2");
}
if(date1<date2){
alert("date1<date2");
}
if(date2==date1){
alert("date2==date1");
}
Upvotes: 1
Reputation: 1671
its simple if you get the date as an integer value of both the dates you compare and this can be done using "getTime()" method. This method returns the milliseconds from 1970 jan 1 to the date you give. Consider the following
var fromDate = new Date();//current date
var fromDateTime = fromDate.getTime();
var toDate = new Date();
toDate .setFullYear(2099,0,14);//changing for comparision
var toDateTime = toDate.getTime();
//MAIN LOGIC
if(fromDateTime> toDateTime){
alert('from time cannot be greater than to date');
return false;
}
Upvotes: 0
Reputation: 1073968
The JavaScript Date
object can be used directly in relations, like <
, >
, <=
, ==
, etc., and also certain math operations (-
, for instance). What happens under the covers is that the Date
object's underlying primitive "time" value, which is milliseconds since The Epoch (the same value that you get from the getTime
function), is used in the expression.
So:
var d1 = new Date(2010, 0, 1); // January 1st, 2010
var d2 = new Date(2010, 1, 1); // February 1st, 2010
display(d1 < d2); // alerts "true"
display(d1 > d2); // alerts "false"
display(d2 - d1); // alerts "2678400000", the difference in milliseconds
The above lines are functionally identical to:
var d1 = new Date(2010, 0, 1); // January 1st, 2010
var d2 = new Date(2010, 1, 1); // February 1st, 2010
display(d1.getTime() < d2.getTime()); // alerts "true"
display(d1.getTime() > d2.getTime()); // alerts "false"
display(d2.getTime() - d1.getTime()); // alerts "2678400000", the difference in milliseconds
This information is in the spec, but you really have to cross-reference sections to find it. We start with 11.8.5 ("The Abstract Relational Comparison Algorithm") that tells us the values will be compared using the [[ToPrimitive]] operation with the "hint" Number
. Head over to section 9.1 ("ToPrimitive") and it tells us that for Objects, [[ToPrimitive]] just passes through to [[DefaultValue]], passing on the hint. Head over to 8.12.8 ("DefaultValue (hint)") and it tells us if the hint is Number
and the object has a valueOf
, it'll use valueOf
. So check out Date
's valueOf
(15.9.5.8 ) and it says it returns "this time value" which is the spec's way of saying the number of milliseconds since The Epoch (read the introduction to the Date
object [15.9.1, 15.9.1.1] to verify that). Conveniently, the very next section (15.9.5.9) is getTime
, which says the same thing.
Whew!
(No, I didn't do all of that just to answer this question; I'd done it a year or so back when I realized I didn't quite know what was going on when I did this, and I got curious.)
Upvotes: 3
Reputation: 2720
var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();
if (myDate>today)
{
alert("Today is before 14th January 2010");
}
else
{
alert("Today is after 14th January 2010");
}
As seen here
http://wap.w3schools.com/js/js_obj_date.asp
Upvotes: 0