-2

Here my below code:

var date = new Date().toLocaleString();
console.log(date); // output: 2018-01-15 16:39:00

var schedule = 2018-01-15 16:39:00 (which is coming from my html form, i putting this date in one input box and getting into variable called schedule).
console.log(schedule); // output: 2018-01-15 16:39:00


if(date === schedule) {
console.log('date is matched');
} else {
 console.log('error');
}

This will show correctly as output: // 'date is matched' (I am typing this date in input box without any date picker).

My problem is if i do this same code using date picker instead of writing date in input box.

My code:

var date = new Date().toLocaleString();
console.log(date); // output: 2018-01-15 16:39:00

    var schedule = 2018-01-15 16:39:00 (which is coming from my html form,using date picker).
    console.log(schedule); // output: 2018-01-15 16:39:00


    if(date === schedule) {
    console.log('date is matched');
    } else {
     console.log('error');
    }

Now i am getting error in console. Date picker which i am using https://www.npmjs.com/package/angularjs-datetime-picker

Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51
  • well, for me `new Date().toLocaleString();` prints out `"15/01/2018, 12:39:45"` so there's no way they can be equal due to `/` and `-` difference – anteAdamovic Jan 15 '18 at 11:40
  • i am getting both values as - , but if i put without date picker its work, otherwise no. – Mohamed Sameer Jan 15 '18 at 11:41
  • @AnteJablanAdamović `toLocaleString()` does just that: it converts the date into a string that's formatted according to the user's locale. – Schlaus Jan 15 '18 at 11:46
  • The output of [*toLocaleString*](http://ecma-international.org/ecma-262/8.0/#sec-date.prototype.tolocalestring) is entirely implementation dependent and gives different results in different implementations. What you're really trying to do is generate a string to compare to the value from the date picker. – RobG Jan 15 '18 at 12:15

2 Answers2

0

How about doing it the other way around: construct a Date object from your datepicker's output and compare the timestamps.

var date = new Date('2018-01-15 16:39:00');

var schedule = new Date(dateFromYourForm); //From Date picker
   
if(date.getTime() === schedule.getTime()) {
  console.log('date is matched');
} else {
  console.log('error');
}
Schlaus
  • 18,144
  • 10
  • 36
  • 64
  • new Date can be dynamically change for every second/ – Mohamed Sameer Jan 15 '18 at 11:55
  • @MohamedSameer It returns a numeric representation of the date. This way you won't have to worry about string formatting. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime – Schlaus Jan 15 '18 at 11:56
  • I dont want put static hard code inside new Date() how to do dynamically? – Mohamed Sameer Jan 15 '18 at 11:57
  • @MohamedSameer Just pass the date from your html input – Schlaus Jan 15 '18 at 11:58
  • The result of `new Date('2018-01-15 16:39:00')` is an invalid Date in at least one common browser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jan 15 '18 at 12:20
-1

The date inside schedule is not a string, is a Date, while the date in date is a string because you have used toLocaleString() on the first line. The compare is not working because you are also using the strict equality operator (===) that checks also the type of your variables instead of checking only the value.

SyncroIT
  • 1,510
  • 1
  • 14
  • 26