1

I am new to js. Here, I have two durations . one is start and second is end duration . Now, What I want to do is that if start duration is greater than end then it should give a message and vice versa.

My logic is like -

var stDate = new Date(start_duration_month);
var enDate = new Date(end_duration_month);
var final_Date = enDate - stDate;
if(final_Date > 0) {var duration_message = true;}

Now here stDate is -> Sat May 01 1999 00:00:00 GMT+0530 (IST)
enDate is --> Tue May 01 2018 00:00:00 GMT+0530 (IST)

Now Due to this, I am able to get to know that there is something wrong as it will give -value . But Now I want to know which one is not proper whether it is start or end So, How will I come to know this ?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
ganesh kaspate
  • 1
  • 9
  • 41
  • 88
  • What is the value of the `start_duration_month` and `end_duration_month`, right now you can use `final_date` which will give you positive or negative value, will that not help you ? – sanjeev May 16 '18 at 06:18
  • Yes If it gives positive value that means end duration is greater than start duration. But what if start is greater then end duration it gives only -value but does not know which one is wrong – ganesh kaspate May 16 '18 at 06:20
  • _I want to know which one is not proper_ what do you mean by **proper** here – Muhammad Usman May 16 '18 at 06:21
  • https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript You can check this answer. – Sagar Gangwal May 16 '18 at 06:21
  • I mean if I get a - value in the final date then whether it was start duration or it was the end duration which caused the -value that means start duration was greater than end or end duration was smaller than start. – ganesh kaspate May 16 '18 at 06:22

1 Answers1

1

Wouldnt it be better for you purpose if you just compare the dates instead of subtracting and checking the value.

var stDate = new Date(start_duration_month);
var enDate = new Date(end_duration_month);
if(enDate > stDate) {
var duration_message = true;
}else{
var duration_message = false;
}

In this case there wont be a negative value. Just true or false.

Codex
  • 155
  • 6