0

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.

9
  • If the alert isn't shown, it means that s_date is not larger than e_date. Make sure the date objects are created correctly. Commented Feb 20, 2014 at 5:03
  • in question i said "but some how if condition is not executing even if startdatetime is greater than enddatetime" Commented Feb 20, 2014 at 5:09
  • Yes, but > is such a simple operation, that the only reason why the the condition would be false is that the date objects are not the ones you intend to have, and s_date is in fact not greater than e_date. How do you know that s_date is truly larger? Did you do console.log(s_date.toString(), e_date.toString())? Unless you provide a running example where s_date is later than e_date and s_date > e_date is false, I will stick to my opinion. Commented Feb 20, 2014 at 5:12
  • i have used alert to check startdate and enddate. for both date is same bt time varies still no popup. startdate : 2014-02-20 00:30:00 enddate : 2014-02-20 00:00:00 Commented Feb 20, 2014 at 5:15
  • So you alerted startdate, but not s_date. What does alert(new Date(startdate)) show you? Commented Feb 20, 2014 at 5:16

5 Answers 5

4

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"
Sign up to request clarification or add additional context in comments.

9 Comments

That's the correct answer. FWIW, dateString can also be in the format YYY-MM-DDTHH:mm:ss
Thanks @FelixKling for improving the answer. I am very bad at formatting the answer. It looks more readable this way :)
date object is converting string like Thu Feb 13 2014 00:00:00 GMT+0530 (India Standard Time) and still no popup
Actually, it's not the correct answer. Date('2014-02-20 00:00:00') gives me the current date/time.
You have to use commas, and pass it through Date.parse. It will then give the milliseconds.
|
1

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");
}

2 Comments

bt no time with date ? what if dates are same bt time varies ?
You can use : var date1 = new Date(2013, 2, 20, 7,0,0) var date2 = new Date(2013, 2, 20, 7,0,1) if(date2>date1) {}
0

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');
}

2 Comments

getTime returns the same value as valueOf, which is automatically called when you use the > operator. So if it doesn't work correctly without getTime, it won't work with it. ecma-international.org/ecma-262/5.1/#sec-15.9.5.8
Not working because you don't show what your start_date[] and end_date[] are and there's now way for me or anyone to know. So when you call the Date function constructor, what you passed in might not be valid. Try this fiddle jsfiddle.net/pcq6y. Troubleshoot by breaking the problem down to smaller parts.
0

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.

3 Comments

Why do you pass a Date object to Date.parse? The purpose of Date.parse is to convert strings to date objects.
Thanks for correcting, but I got it to work using parse for converting to milliseconds.
Oh, you are right, Date.parse does indeed return milliseconds. However, when you compare date objects with >, they are automatically converted to milliseconds. Just try sd > ed and see for yourself. Date.parse is unnecessary here and rather confusing than helping.
-2
    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

2 Comments

Do you also have an explanation? Code alone is not very useful most of the time. I would be especially interested why you use (edate-sdate)<0, instead of the much easier expression sdate > edate.
i think there is problem while u r converting string to date ... have a look over here for converting the string to date stackoverflow.com/questions/5619202/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.