How to use the isDate function from util

Find comprehensive JavaScript util.isDate code examples handpicked from public code repositorys.

The util.isDate method checks whether a value is a Date object or not.

167
168
169
170
171
172
173
174
175
176
  // false
```


<div id="isDate" class="anchor"></div>
## util.isDate(object)

> 稳定度:0 - 已废弃

如果给定的 'object' 是 `Date`,返回 `true`。否则,返回 `false`。
fork icon21
star icon99
watch icon2

317
318
319
320
321
322
323
324
325
326
// Returns: false
util.isBuffer(Buffer.from('hello world'));
// Returns: true
```

### util.isDate(object)
<!-- YAML
added: v0.6.0
deprecated: v4.0.0
-->
fork icon0
star icon6
watch icon1

How does util.isDate work?

The util.isDate() function is a built-in Node.js utility function that takes a single argument and returns true if the argument is a Date object, otherwise returns false. The function checks if the argument has the typeof value "object" and that the Object.prototype.toString() method returns the string representation "[object Date]".

1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
} else if (isBuffer(actual) && isBuffer(expected)) {
  return compare(actual, expected) === 0;

// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (util.isDate(actual) && util.isDate(expected)) {
  return actual.getTime() === expected.getTime();

// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source and
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
const util = require("util");

console.log(util.isDate(new Date())); // true
console.log(util.isDate(Date.now())); // false
console.log(util.isDate("2022-05-12")); // false

In this example, we import the util module and use the isDate method to check whether a value is a Date object. We pass three values to isDate: a Date object, a timestamp, and a string. The first call returns true because we pass a Date object. The second call returns false because we pass a timestamp, and the third call also returns false because we pass a string.