# Date.isDate(date) method

**URL:** <https://es.discourse.group/t/date-isdate-date-method/1844>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 7, 2023, 9:55am UTC](https://es.discourse.group/t/date-isdate-date-method/1844 "2023-11-07T09:55:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 7, 2023, 9:55am UTC](https://es.discourse.group/t/date-isdate-date-method/1844/1 "2023-11-07T09:55:35Z")

</div>

Today, the only way to check if a Date is a valid Date is through the `!isNaN(new Date(someDate).getTime())` which is both not readable and also takes a while to understand (especially for new devs) what is it really doing.

My proposal is to follow on the `Array.isArray` method and add a `Date.isDate`. The implementation of this function could follow the signature:

```js
function isDate (date) => boolean
function isDate (string) => boolean
function isDate (number) => boolean

```

Accepting a `Date` constructor, a number (timestamp) or a string (ISOString) would then pass it to the actual date constructor, this is a (VERY SIMPLE DRAFT) implementation:

```js
function isDate (maybeDate) {
 switch (typeof maybeDate) {
  case 'string':
  case 'number': 
    const dateValue = new Date(maybeDate)
    return !isNaN(dateValue.getTime())
  case 'object':
    if ('getTime' in maybeDate) return !isNaN(maybeDate.getTime())
    return false
  default:
    return false
  }
}

```

So the usage could be simplified and made available under the `Date` constructor as:

```js
if (Date.isDate(new Date())) // true

```

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [November 7, 2023, 6:50pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/2 "2023-11-07T18:50:15Z")

</div>

> [@khaosdoctor](#):
>
> `if ('getTime' in maybeDate) return !isNaN(maybeDate.getTime())`

Just an FYI that a more robust check would be something that checks for the Date internal slot:

```javascript
try {
  Date.prototype.getTime.call(object);
  return true;
} catch {
  return false;
}

```

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [November 7, 2023, 6:54pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/3 "2023-11-07T18:54:31Z")

</div>

If you've not yet checked it out, soon there will be the `Temporal` API in JavaScript which will hopefully be able to replace all use cases for Date. [Temporal documentation](https://tc39.es/proposal-temporal/docs/zoneddatetime.html#from)

---

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 8, 2023, 10:12am UTC](https://es.discourse.group/t/date-isdate-date-method/1844/4 "2023-11-08T10:12:38Z")

</div>

Yeah, this was more of a proof of concept, the actual implementation would be much better than this, since instanceof can be tampered with, I think we could check like we do in the Date Mocks API from Node ([https://github.com/nodejs/node/pull/48638/](https://github.com/nodejs/node/pull/48638/))

---

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 8, 2023, 10:14am UTC](https://es.discourse.group/t/date-isdate-date-method/1844/5 "2023-11-08T10:14:29Z")

</div>

On the Temporal one, I'm super excited for it but it's a proposal that's been around for 5 years or more already... I get your point, we should have it in one API, but I think to wait until Temporal is ready may take years, and since this is a "simple" change, which is compliant to other APIs like `Array.isArray`, I wanted to vouch and defend the idea of this coming to the current API, at least until we have Temporal in place.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [November 8, 2023, 6:31pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/6 "2023-11-08T18:31:56Z")

</div>

Dates can have invalid values. This isn't a check for "is a date", but "is a _valid_ date, so a name like `Date.isDate` makes little sense.

Also, a date is valid if and only if its source can be parsed as a date value. So, logically speaking, `Number.isNaN(Date.parse(someDate))` is currently the most direct way to check whether a given string is a valid date string. (It also has effectively zero memory overhead.)

For numbers, I don't see the point other than maybe exposing `Date.MAX_TIME`/`Date.MIN_TIME`. Testing is as easy as testing `Math.abs(value) <= Date.MAX_TIME`, which naturally fails for NaNs.

---

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 8, 2023, 7:55pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/7 "2023-11-08T19:55:40Z")

</div>

I see the logic here, thanks for the clarification. But I think that, from the developer/user pov it makes little to no difference what type we are testing, in this case, the point is to verify if the passed value would be a valid `Date` object in JavaScript, in this case we are both testing for valid values and, by definition, if it's a valid date, otherwise the `Date` object would also not accept it.

The naming of the function is just a mimic to the present `Array.isArray` from pior art, I'm not against changing it if it makes more sense. But, in the same spirit as the array, it tests if this would be a valid object in the end but abstracting from the deeper logic of what is a date and what is an internal date value.

Besides, I think writing:

```js
if (Date.isDate(someValue)) { ... }

```

Is much more readable and easy to understand in a glimpse than:

```js
if (!isNaN(new Date(someValue).getTime())) { ... }

```

So it makes the code easier to maintain and friendlier for other people as well.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 8, 2023, 8:37pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/8 "2023-11-08T20:37:35Z")

</div>

“is date” is an operation that must return true for any Date object, even if it’s time value is NaN.

---

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 9, 2023, 10:11pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/9 "2023-11-09T22:11:42Z")

</div>

Should we consider renaming it to `isValidDate`?

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 10, 2023, 2:21am UTC](https://es.discourse.group/t/date-isdate-date-method/1844/10 "2023-11-10T02:21:24Z")

</div>

I think a name like that would make more sense.

---

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 10, 2023, 5:00pm UTC](https://es.discourse.group/t/date-isdate-date-method/1844/11 "2023-11-10T17:00:22Z")

</div>

I can't update the topic anymore 😅, but I'm not opposed to have as this name as well
