Home > Blockchain >  How to validate whether a string-value is a parsable date-format?
How to validate whether a string-value is a parsable date-format?

Time:01-14

I am running the code below, checking if the string is a date. One of my input values is 'text' which returns the NaN value but my if clause does not work as expected.

function isDate(myDate) {
  console.log('myDate = '   myDate);
  return Date.parse(new Date(myDate));
}

// var date1 = '1/5/22'; // good date
var date1 = 'test'; // bad date

var whatDate = isDate(date1);

console.log('date = '   whatDate);

if (whatDate == 'NaN') {
  console.log('bad date');
} else {
  console.log('good date');
}

CodePudding user response:

You're almost there:

if(whatDate == 'NaN'){
    log.debug('bad date');
}else{
    log.debug('good date');
}

Instead of comparing whatDate to 'NaN' use the function isNaN():

if(isNaN(whatDate)){
    log.debug('bad date');
}else{
    log.debug('good date');
}

Alternatively, if you really want to compare to 'NaN' you first have to convert whatDate to a string:

if((whatDate   "") == 'NaN'){
    log.debug('bad date');
}else{
    log.debug('good date');
}

Is one possibility. Another way would be

if(whatDate.toString() == 'NaN'){
    log.debug('bad date');
}else{
    log.debug('good date');
}

CodePudding user response:

in your if statement you are checking if whatDate is equal to string "NaN". what you want to check whether whatDate is a number or not.

you can try

if (isNaN(whatDate))

CodePudding user response:

The following answer, in addition to what already was explained about how to detect a NaN value, wants to reach further.

A check or test function especially if it also carries the prefix is in its name should always return exclusively a boolean value.

  • Thus the OP firstly would implement such a function in a way a user would expect it to work.
  • Secondly the OP had do deal with the NaN issue exactly once within the test function's implementation.

... and/but ... according to the ECMAScript's spec, following the steps of how a Date instantiation takes place ... 21.4.2.1 Date ( ...values ) ... whenever it internally comes to handling the number type NaN the internal return value is 'Invalid Date'. For e.g. new Date('') the return value is a Date object with an internal string value of 'Invalid Date'. Thus a validation / test implementation could be something as straightforward as ...

function isParsableDateRepresentative(...args) {
  return String(new Date(...args)) !== 'Invalid Date';
}

console.log('new Date("1/5/22") ...', new Date("1/5/22"));
console.log(
  'isParsableDateRepresentative("1/5/22") ...',
  isParsableDateRepresentative("1/5/22")
);
console.log('\nnew Date("test") ...', new Date("test"));
console.log(
  'isParsableDateRepresentative("test") ...',
  isParsableDateRepresentative("test")
);
console.log('\nnew Date(new Date("test")) ...', new Date(new Date("test")));
console.log(
  'isParsableDateRepresentative(new Date("test")) ...',
  isParsableDateRepresentative(new Date("test"))
);

console.log('\nnew Date() ...', new Date());
console.log(
  'isParsableDateRepresentative() ...',
  isParsableDateRepresentative()
);
console.log('\nnew Date(null) ...', new Date(null));
console.log(
  'isParsableDateRepresentative(null) ...',
  isParsableDateRepresentative(null)
);
console.log('\nnew Date(undefined) ...', new Date(undefined));
console.log(
  'isParsableDateRepresentative(undefined) ...',
  isParsableDateRepresentative(undefined)
);
console.log('\nnew Date("") ...', new Date(""));
console.log(
  'isParsableDateRepresentative("") ...',
  isParsableDateRepresentative("")
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

  •  Tags:  
  • Related