3

I want to check if date is not equal to 0000-00-00 00:00:00. How can I check it in javascript?

I checked with date !== null but it is not working.

Trupti
  • 843
  • 2
  • 11
  • 28

4 Answers4

3

You can check "0000-00-00 00:00:00" with

Date.parse(date) // NaN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Julia
  • 674
  • 1
  • 6
  • 18
2

Having 0000-00-00 00:00:00 suggest that you not deal with Date object in JS but string comparing it in if statement should do:

if (data !== '0000-00-00 00:00:00') { ....
bensiu
  • 24,660
  • 56
  • 77
  • 117
1

Wouldn't you use date !=== 0000-00-00 00:00:00?

samarmohan
  • 350
  • 3
  • 12
0

Here is an example using ternary operators:

(date === '0000-00-00 00:00:00' ? exprIfTrue : exprIfFalse );

Also, below is an example using indexOf. - There are definitely a few ways to do this but not exactly sure what you're looking for with your use-case.

const zeroDate = '0000-00-00 00:00:00';
const realDate = '2020-10-13 00:04:05';

//check to see if indexOf '0000-00-00' exists in zeroDate string
// if true then log 'yes', if false then log 'no'
// this will log 'yes' since '0000-00-00' exists
(zeroDate.indexOf('0000-00-00') == 0  ? console.log('yes') : console.log('no') );


//same as above but for realDate string
//since there is no occurance of '0000-00-00' it will log 'no'
(realDate.indexOf('0000-00-00') == 0  ? console.log('yes') : console.log('no') );
Aib Syed
  • 3,118
  • 2
  • 19
  • 30