EN
JavaScript - extract date object values from array
3 points
In this article, we would like to show you how to extract date object values from an array in JavaScript.
Quick solution:
xxxxxxxxxx
1
const resultArray = myArray.filter(element => element instanceof Date);
Since there is no 'date'
type in JavaScript and dates are 'object'
type, we check if each value
in the given array is an instance of Date
using instanceof
operator.
xxxxxxxxxx
1
const isDate = (value) => value instanceof Date;
2
3
4
// Usage example:
5
6
const array = [new Date(), '24 JAN 2022', '2022-01-24', '2022-01-24 17:30:30'];
7
8
console.log(array.filter(isDate));
9
10
// Output:
11
// [ 2022-01-24T17:43:37.893Z ]
Note:
The code returns only theDate
object created withDate()
constructor.