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.

Chintan_chiku
  • 425
  • 2
  • 12
  • 24
  • 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. – Felix Kling Feb 20 '14 at 05:03
  • in question i said "but some how if condition is not executing even if startdatetime is greater than enddatetime" – Chintan_chiku Feb 20 '14 at 05: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. – Felix Kling Feb 20 '14 at 05: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 – Chintan_chiku Feb 20 '14 at 05:15
  • So you alerted `startdate`, but not `s_date`. What does `alert(new Date(startdate))` show you? – Felix Kling Feb 20 '14 at 05:16
  • I will interpret your silence as acceptance to my hypothesis. – Felix Kling Feb 20 '14 at 05:26
  • @FelixKling I was trying the suggestions given in replies. bt non of them working for me :( – Chintan_chiku Feb 20 '14 at 05:37
  • I was (partly) wrong, the answer I said is correct, is not correct. I'm still right though that `s_date` and `e_date` are not the date objects you want. `new Date(startdate)` and `new Date(enddate)` both result in **invalid** date objects and `Date(startdate)`, `Date(enddate)` simply returns the **current** date time. You should have a look at [this question](http://stackoverflow.com/q/1576753/218196) to get an idea of how to properly parse custom date strings. – Felix Kling Feb 20 '14 at 05:43
  • Alternatively, just add a `T` between the date and time, so that your string looks like `YYYY-MM-DDThh:mm:ss` and you can pass it to `new Date`. – Felix Kling Feb 20 '14 at 05:45

5 Answers5

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"
Community
  • 1
  • 1
VJS
  • 1,017
  • 9
  • 21
  • That's the correct answer. FWIW, `dateString` can also be in the format `YYY-MM-DDTHH:mm:ss` – Felix Kling Feb 20 '14 at 05:19
  • Thanks @FelixKling for improving the answer. I am very bad at formatting the answer. It looks more readable this way :) – VJS Feb 20 '14 at 05:23
  • date object is converting string like Thu Feb 13 2014 00:00:00 GMT+0530 (India Standard Time) and still no popup – Chintan_chiku Feb 20 '14 at 05:36
  • Actually, it's not the correct answer. `Date('2014-02-20 00:00:00')` gives me the current date/time. – Felix Kling Feb 20 '14 at 05:39
  • You have to use commas, and pass it through Date.parse. It will then give the milliseconds. – Rutwick Gangurde Feb 20 '14 at 05:47
  • @Chintan_chiku: You shouldn't accepts an answer just for the sake of it. If others have the same problem, read your question and try this answer, will wonder why it doesn't work. – Felix Kling Feb 20 '14 at 05:47
  • @FelixKling http://jsfiddle.net/Th7Pa/ gives me NAN for new Date('2014-02-20 00:00:00'); – VJS Feb 20 '14 at 05:53
  • @FelixKling I Voted for that ans cuz that ans clarified constructor string format for date object. i was thinking its like "2014-02-20 00:00:00" but i was wrong – Chintan_chiku Feb 20 '14 at 06:01
  • @VJS: I was talking about `Date('2014-02-20 00:00:00')`, without `new` (we already clarified that such a format is not valid for `new Date`). I realize that you are just citing the other answer, but unfortunately it is now incorrect too. The answer is partly wrong as in that it says that a date string in that format cannot be passed to `new Date`, but it also contains the information that such a string would be valid for `Date`, which it isn't (anymore). – Felix Kling Feb 20 '14 at 06:01
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");
}
Phat H. VU
  • 2,350
  • 1
  • 21
  • 30
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');
}
Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
  • `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. http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.8 – Felix Kling Feb 20 '14 at 05:11
  • 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 http://jsfiddle.net/pcq6y/. Troubleshoot by breaking the problem down to smaller parts. – Kevin Le - Khnle Feb 20 '14 at 05:33
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.

Rutwick Gangurde
  • 4,772
  • 11
  • 53
  • 87
  • Why do you pass a `Date` object to `Date.parse`? The purpose of `Date.parse` is to convert **strings** to date objects. – Felix Kling Feb 20 '14 at 06:04
  • Thanks for correcting, but I got it to work using parse for converting to milliseconds. – Rutwick Gangurde Feb 20 '14 at 06:09
  • 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. – Felix Kling Feb 20 '14 at 06:11
-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

amit_183
  • 961
  • 3
  • 19
  • 36
  • 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`. – Felix Kling Feb 20 '14 at 05:04
  • 1
    i think there is problem while u r converting string to date ... have a look over here for converting the string to date http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js – amit_183 Feb 20 '14 at 05:14