arasatasaygin/is.js

is.date() returns true when instance of date is Invalid date

Closed this issue · 2 comments

In project I have some helpers for preparing user data, which i've get from server. Server does respond with some information about user: firstName, lastName, date, etc.

I've try to format date string with new Date() and check it by is.date(instanceOfDate).

Sometimes server does respond with undefined and is.date(new Date(undefined)) returns true, but new Date (undefined) returns Invalid Date.

I know that i can check initial value for undefined, but in this case It's logical that function would return false instead of true. When i got date: null — its all ok.

Hi @Forshortmrmeth!

is.date does not return true for is.date(undefined). The is.date method is a value kind checker. An invalid date is still a date value just as NaN and Infinity are number values.

jt3k commented

@Forshortmrmeth try to use

const isCorrectDate = new Date(undefined).toString() !== "Invalid Date";