-3

I have 3 dateTimes in the same format. These 3 dateTimes have the same date at different times.

var orgstartdatetime = 'Mon Jan 01 1900 10:00:00 GMT+0519 (India Standard Time)';
var orgenddatetime = 'Mon Jan 01 1900 12:00:00 GMT+0519 (India Standard Time)';
var newdatetime = 'Mon Jan 01 1900 10:30:00 GMT+0519 (India Standard Time)';

I want to compare newdatetime is in between the orgstartdatetime and orgenddatetime. I tried it like these 3 ways. But those are not working.

                if (orgstartdatetime.getTime() <= newdatetime.getTime() < orgenddatetime.getTime() ) {
                         console.log('conflit');
                 }
                 if (orgstartdatetime.valueOf() <= newdatetime.valueOf() < orgenddatetime.valueOf() ) {
                         console.log('conflit');
                 }
                 if (+orgstartdatetime <= +newdatetime < +orgenddatetime ) {
                         console.log('conflit');
                 }

Please help me to fulfill this if condition. Thank you

Prasanga
  • 9
  • 1
  • You have strings, not `Date` instances so you cannot use `getTime()`. Converting them to numbers isn't going to work either. Nor will any sort of string comparison because the month names are not in alphabetical order. Your only real option is to parse them into `Date` instances – Phil Nov 19 '20 at 05:35
  • Did you check the error on console or output? – thanhdx Nov 19 '20 at 05:36
  • You can't chain comparative operators like that. If you want to do `a <= b < c` you have to do each comparison separately as `a <= b && b < c`. Since the timestamps conform to the format specified for [*Date.prototype.toString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString), they can be parsed by the built–in parser and compared directly. – RobG Nov 19 '20 at 10:10

1 Answers1

0

You first need to create a Date object to access getTime() method, try this:

var orgstartdatetime = new Date('Mon Jan 01 1900 10:00:00 GMT+0519 (India Standard Time)');
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35