Reputation: 425
i have startdate and enddate string and i m converting string to date object
var startdate = start_date[3]+'-'+month_value_start+'-'+start_date[2]+' '+start_time[0]+':'+start_time[1]+':00';
var enddate = end_date[3]+'-'+month_value_end+'-'+end_date[2]+' '+end_time[0]+':'+end_time[1]+':00';
var s_date = new Date(startdate);
var e_date = new Date(enddate);
startdate and enddate format is like 2014-02-20 00:00:00
i want to compare this date object if s_date is greater than e_date popup will be shown to user
if(s_date > e_date)
{
alert('Start Date Cannot Be Greater Than End Date');
}
but some how if condition is not executing even if startdatetime is greater than enddatetime.
how to solve this issue any suggestions ?
Solved
program which i m using, automatically changes enddate in runtime if startdate is greater than end date. but no update on frontend ie datetimepicker textbox so on frontend enddate remains less than startdate but in background code has modified the enddate variable and that variable i was using to compare dates....sorry for troubling u guys....and thanks for helping me.
Upvotes: 0
Views: 4745
Reputation: 4922
Try this:
var sd = new Date('12, 12, 2012');
var ed = new Date('12, 12, 2014');
//This condition means the end date is bigger even if the comparison says otherwise
//Can work without Parse
if(Date.parse(sd) > Date.parse(ed)){
console.log('Start date is bigger');
} else {
console.log('End date is bigger');
}
In my example the end date will be bigger because the number of milliseconds for the earlier date will always be smaller than the later date. So you have to check if the start date milliseconds are greater than end date milliseconds in order to make sure that the start date is lesser than the end date.
Upvotes: 0
Reputation: 1017
All the answers are correct in determining the difference.
But the problem you are facing is the incorrect way of calling
new Date(dateString);
Copied answer from here Difference between Date(dateString) and new Date(dateString)
Date()
With this you call a function called Date(). It accepts date in format "yyyy-mm-dd hh:mm:ss"
new Date()
With this you're creating a new instance of Date.
You can use only the following constructors:
new Date() // current date and time new Date(milliseconds) //milliseconds since 1970/01/01 new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)
So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.
EDIT: new Date(dateString) uses one of these formats:
"October 13, 1975 11:13:00"
"October 13, 1975 11:13"
"October 13, 1975"
Upvotes: 4
Reputation: 2370
Following code snippet demonstrates how data comparison is done using JavaScript.
var startDate= new Date();
startDate.setFullYear(2020, 1, 20);
var today= new Date();
if (startDate> today) {
alert("Today is before 20th Feb 2020");
} else {
alert("Today is after 20th Feb 2020");
}
Upvotes: 1
Reputation: 10887
You can use the getTime() method of Date object. That method returns the number of milliseconds since 1970/01/01. So the the comparison becomes:
if(s_date.getTime() > e_date.getTime())
{
alert('Start Date Cannot Be Greater Than End Date');
}
Upvotes: 0
Reputation: 981
edate = Date.parse(e_date);
sdate = Date.parse(s_date);
if((edate-sdate)<0)
{
alert("End date should be greater then start date.");
return;
}
Date.parse("date")->this function parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970.
using this function u can get the timestamp of both the dates and by subtraction u can find out whether start date is greater than end date
Upvotes: -2