JavaScript Problem: Determining the Earliest or Latest Date

preview_player
Показать описание
The last few tutorials I seem to be on a Date object kick. In this tutorial we are going to look at how you can take an array of dates and pick the earliest or latest.

Would you like to help keep this channel going?

For more resources on JavaScript:

#javascript #AllThingsJavaScriptLLC
Рекомендации по теме
Комментарии
Автор

We need great teachers like you, please please take care of yourself.

shabnamnaaz
Автор

You're the best. Wishing your family good health and recovery.

alanmartin
Автор

Amazing Steven. The best teacher ever.

polliluiz
Автор

//Math min and max is an fast way to solve it, as it was said valueof can be used or getTime.

//So there are two functions that can help. The first one is the sort helper function,
//that will mine milliseconds, in order to sort the array...

function byTime(r = false){
return (a, b) => {
[a, b] = [a.getTime(), b.valueOf()];
return a < b ? r ? 1 : -1 : a > b ? r ? -1 : 1 : 0;
};
}
//Here we use valueof or getTime or both....

//Then we shall have an extractor...
function getFirstLast(array){
let {[0] : first, [array.length - 1] : last} = [...array];
return {'earliest':first, 'latest':last, 'all':array};
}
//That here will return a object, with what we need and more if so needed...

//Example of use... This array have different way to create a Date object with Date Function..
let seriesOfDates = [
new Date(), //This moment
new Date(2026, 2, 7), //Date Attributes
new Date(2015, 5, 25, 14, 25), // With hours and minutes
new Date(60*60*24*1000 * (-365 * 2) + //millisecond
new Date("Mars 05 2077 06:32:57.545"), //String
new Date(2012, 6) //First day of month
];

//Let put it together..
let results =

//Output...
console.log(results.earliest, results.latest);

magnusfohlstrom
Автор

Your tutorials are easy to understand. Can you please make tutorials on how to read javascript api document? I get confused reading document as methods are overloaded with multiple implementations. Thanks

tanzeemahmed
Автор

Very helpful content.
Thank you so much for sharing.

عبدالقادرعبدالرحمنعبدالله
Автор

Hi Steven, can you tell us what do you think about Vue js, It will be great to hear your opinion, anyway amazing tutorial as always, thanks

relaxingwithoutinterruptio
Автор

I'm not getting able to apply correct condition like I'm getting incorrect results... I have msfe the dates in format with moment

kashmirtechtv
welcome to shbcf.ru