0

I have two dates like 2020-12-31 and 2020-12-01 in string format yyyy-mm-dd while check the month it should return true.

Suppose I compare 2020-11-31 and 2020-12-31 it should return false.

If there is a difference in only month and year, it should return true.

For example today Feb 18, 2020, Suppose I choose Feb 11, 2020, it should return true, If I choose Jan 11, 2020, then it should return false.

Date difference should not be considered

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
Maheswaran S
  • 525
  • 3
  • 12

3 Answers3

2

@Maheshwaran You can use something like this

var d1 = new Date("2020-12-31")
var d2 = new Date("2020-12-31")
return d1.getMonth() === d2.getMonth() && d1.getYear() === d2.getYear()
ashu saini
  • 86
  • 6
1

You can go with the following code:

const date1 = new Date('2020-12-01');
const date2 = new Date('2020-12-31');

// if you want to return true or false based on either date or month or year difference then use the following code.

const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
// this way if there is difference in either date or month or year then it will return true
if(diffDays) {
  return true;
} else {
  return false;
}

If you want to return either true or false on basis of either month or year only then you can use the following code.

const date1 = new Date('2020-12-01');
const date2 = new Date('2020-12-31');
// return basis on difference of either month OR year 
return date1.getMonth() === date2.getMonth() || date1.getYear() === date2.getYear()

// return basis on the difference of month AND year
return date1.getMonth() === date2.getMonth() && date1.getYear() === date2.getYear()
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • suppose different year but same month it will work? like 2019-12-31 and 2020-12-31 – Maheswaran S Feb 18 '20 at 06:41
  • @MaheswaranS What's will work in your case? Should it return true or false then? – Julian Feb 18 '20 at 06:44
  • 1
    @MaheswaranS you have not asked about year difference in your question. You just asked about 2020-11-31 and 2020-12-31 i.e. If month not the same then it should return false. – Deep Kakkar Feb 18 '20 at 06:45
  • @StackSlave I have updated the answer. by mistake, variables names were updated. :) – Deep Kakkar Feb 18 '20 at 06:49
  • @ Deep Kakkar i want to check year also. – Maheswaran S Feb 18 '20 at 06:49
  • @MaheswaranS i.e. so now, if there is a difference in both dates or months or years then it should return true? right? If yes then let me update your question and then I will update my answer as well. – Deep Kakkar Feb 18 '20 at 06:51
  • @ Deep there is difference in only month and year means it should return false. Date not an issue – Maheswaran S Feb 18 '20 at 06:55
  • For example today Feb 18, 2020, Suppose i choose Feb 11, 2020 it should return true, If i choose Jan 11, 2020 it should return false. I think you will get my question. – Maheswaran S Feb 18 '20 at 06:57
  • @MaheswaranS I have updated my answer. So now you can choose code snippet according to your requirement. – Deep Kakkar Feb 18 '20 at 07:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208024/discussion-between-deep-kakkar-and-maheswaran-s). – Deep Kakkar Feb 18 '20 at 07:06
0

Shorted, faster, and simpler solution is:

"2020-11-31".substr(5, 2) === "2020-12-31".substr(5, 2)