How to check date is not zero in javascript?

Viewed 1074

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.

4 Answers

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') { ....

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

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') );

Related