1console.log("hello-world);
console.log("hello-world);
.container {
width: 80%;
}
<pre><code class="language-css">
.container {
width: 80%;
}
</code></pre>
1console.log("hello-world);
console.log("hello-world);
.container {
width: 80%;
}
<pre><code class="language-css">
.container {
width: 80%;
}
</code></pre>
Buckle up Javascript Chrononauts 🚀⏲, in this blogpost we will explore how to find maximum and minimum dates in an array of Javascript objects.
The Date
object in JavaScript encapsulates date and time functionality.
getFullYear()
: Retrieves the year.
getMonth()
: Retrieves the month (0-11).
getDate()
: Retrieves the day of the month.
getHours()
, getMinutes()
, getSeconds()
: Retrieves the respective time components.
setFullYear()
, setMonth()
, setDate()
: Sets the corresponding date components.
let currentDate = new Date();
console.log("Current Date and Time:", currentDate);
let specificDate = new Date('2022-12-31T23:59:59');
console.log("Specific Date:", specificDate);
let customDate = new Date(2022, 11, 31); // Month is zero-based
console.log("Custom Date:", customDate);
let customDateTime = new Date(2022, 11, 31, 23, 59, 59);
console.log("Custom Date and Time:", customDateTime);
let epochTimeDate = new Date(1640995200000); // January 1, 2022
console.log("Epoch Time Date:", epochTimeDate);
let originalDate = new Date('2022-01-01');
let copiedDate = new Date(originalDate);
console.log("Copied Date:", copiedDate);
let dates = [
{ date: new Date('2022-01-01') },
{ date: new Date('2022-02-15') },
{ date: new Date('2022-03-10') }
];
let maxDate = new Date(Math.max(...dates.map(obj => obj.date.getTime())));
let minDate = new Date(Math.min(...dates.map(obj => obj.date.getTime())));
console.log("Max Date:", maxDate);
console.log("Min Date:", minDate);
Explanation:
dates
array contains objects with a date property, each holding a JavaScript Date object.dates.map(obj => obj.date.getTime())
extracts the time in milliseconds for each date using the map
function. This results in an array of milliseconds.Math.max(…millisecondsArray)
and Math.min(…millisecondsArray)
find the maximum and minimum values in the array of milliseconds, respectively.new Date(maximumMilliseconds)
and new Date(minimumMilliseconds)
create Date
objects using the maximum and minimum milliseconds found.console.log
statements display the maximum and minimum dates.In the spirit of Test Driven Development ( 😁), lets test our understanding by solving a problem.
Find the sum of milliseconds between the maximum and minimum dates in the given Dates array.
// Temporal Challenge Question:
let dates = [
{ date: new Date('2022-01-01') },
{ date: new Date('2022-02-15') },
{ date: new Date('2022-03-10') }
];
/**
* Find the sum of milliseconds between
* the maximum and minimum dates in the array.
*/
function calculateTimeDifference(datesArray) {
// > > > 👉 Write code here 👈 < < <
}
// User fills in the method
let timeBenderResult = calculateTimeDifference(dates);
console.log("Time Bender Result:", timeBenderResult);
Please attempt before seeing the Answer:
// Temporal Challenge Solution:
function calculateTimeDifference(datesArray) {
let maxDate = new Date(Math.max(...datesArray.map(obj => obj.date.getTime())));
let minDate = new Date(Math.min(...datesArray.map(obj => obj.date.getTime())));
let timeDifference = maxDate.getTime() - minDate.getTime();
return timeDifference;
}
Explanation:
calculateTimeDifference
function takes an array of date objects as a parameter, finds the maximum and minimum dates using the map
, Math.max
, and Math.min
techniques, calculates the time difference, and returns the result.BTW, do you know why the JavaScript date refuse to go to the party? It heard they needed a timestamp, not a time-stampede. 😉
Happy coding 🚀!
Feel free to reach out!