How to Select Min/Max Dates in an Array Using JavaScript? Last Updated : 31 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Here are the different ways to select min/max dates in an array using JavaScript1. Using Math.max and Math.minUse Math.max and Math.min function to get the maximum and minimum dates respectively. JavaScript let dates = [ new Date("2019/06/25"), new Date("2019/06/26"), new Date("2019/06/27"), new Date("2019/06/28") ]; function fun() { const maxDate = new Date(Math.max(...dates)); const minDate = new Date(Math.min(...dates)); console.log("Max date is - " + maxDate); console.log("Min date is - " + minDate); } fun(); OutputMax date is - Fri Jun 28 2019 00:00:00 GMT+0000 (Coordinated Universal Time) Min date is - Tue Jun 25 2019 00:00:00 GMT+0000 (Coordinated Universal Time) In this example: The code finds the maximum (latest) and minimum (earliest) dates from an array of Date objects. It uses Math.max() and Math.min() with the spread operator (...) to get the highest and lowest timestamps, and then converts them back to Date objects. The result is printed using console.log().2. Using reduce() methodUse reduce() method in an array of dates and define the respective function for the maximum and minimum dates. JavaScript let dates = [ new Date("2019/06/25"), new Date("2019/06/26"), new Date("2019/06/27"), new Date("2019/06/28") ]; function fun() { const minDate = dates.reduce((a, b) => (a < b ? a : b)); const maxDate = dates.reduce((a, b) => (a > b ? a : b)); console.log("Max date is - " + maxDate); console.log("Min date is - " + minDate); } fun(); OutputMax date is - Fri Jun 28 2019 00:00:00 GMT+0000 (Coordinated Universal Time) Min date is - Tue Jun 25 2019 00:00:00 GMT+0000 (Coordinated Universal Time) In this example: The code uses reduce() to iterate through the dates array, comparing two dates (a and b) at a time. It finds:Minimum date (minDate): By keeping the smaller of a and b.Maximum date (maxDate): By keeping the larger of a and b.3. Using Spread OperatorTo select the minimum and maximum dates in an array, use the spread operator to clone the array. JavaScript let dates = [new Date('2022-01-01'), new Date('2022-03-15'), new Date('2022-02-10')]; let minDate = new Date(Math.min(...dates)); let maxDate = new Date(Math.max(...dates)); console.log(minDate); console.log(maxDate); Output2022-01-01T00:00:00.000Z 2022-03-15T00:00:00.000Z In this example: The code finds the minimum and maximum dates in an array of Date objects using Math.min() and Math.max().Math.min(...dates): Finds the earliest date by spreading the dates array and comparing their timestamps.Math.max(...dates): Finds the latest date similarly.Converts the timestamps back to Date objects with new Date(). 4. Using Array.prototype.sort with a custom comparatorUsing Array.prototype.sort() with a custom comparator allows you to sort an array based on specific criteria by defining a comparison function. JavaScript let dates = [ new Date("2024-05-15"), new Date("2024-05-17"), new Date("2024-05-13"), new Date("2024-05-20"), ]; dates.sort((a, b) => a - b); let minDate = dates[0]; let maxDate = dates[dates.length - 1]; console.log("Min date:", minDate.toDateString()); console.log("Max date:", maxDate.toDateString()); OutputMin date: Mon May 13 2024 Max date: Mon May 20 2024 In this exampledates[0]: Gives the earliest (minimum) date.dates[dates.length - 1]: Gives the latest (maximum) date.The results are printed in a human-readable format using toDateString(). Comment More infoAdvertise with us P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like