ordering the date and time in a reverse sequence using javascript

I am working with dynamically generated date and time data and I am looking to sort them in descending order using JavaScript.

Here is an example of the data format I am dealing with:

var array= ["25-Jul-2017 12:46:39 pm","25-Jul-2017 12:52:23 pm","25-Jul-2017 12:47:18 pm"];

Any assistance on how to achieve this would be greatly appreciated.

Answer №1

Check out this efficient code snippet:

let dates = ["15-Oct-2021 11:30:45 am", "15-Oct-2021 11:35:22 am", "15-Oct-2021 11:32:18 am", "15-Oct-2021 11:45:18 am"];

dates.sort((first, second) => new Date(second).getTime() - new Date(first).getTime())

console.log(dates)

Answer №2

const dates = ["12-Aug-2019 10:30:45 am", "12-Aug-2019 09:15:22 am", "12-Aug-2019 11:20:17 am"];

dates.sort(function(a, b) {
  return new Date(b) - new Date(a);
});

console.log(dates);

Answer №3

DEMO

var array= ["25-Jul-2017 12:46:39 pm","25-Jul-2017 12:52:23 pm","25-Jul-2017 12:47:18 pm"];
var dateAr = [];

for(var i=0;i<array.length;i++){
 dateAr.push(new Date(array[i].replace(/-/g,'/')).getTime()); //convert to milliseconds
}


var sortAr = dateAr.sort(function (a, b) {  return a - b;  }); //sort the array

var resArr = [];
for(var i=0;i<array.length;i++){
 resArr.push(new Date(sortAr[i])); //create date object
}
console.log(resArr);

Convert date to milliseconds, then sort it and create date object from milliseconds. Use replace(/-/g,'/') to replace date strings, it is compatible with all browsers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for Saving JSON Response from Fetch API into a JavaScript Object

I am facing an issue trying to store a Fetch API JSON as a JavaScript object in order to use it elsewhere. The console.log test is successful, however I am unable to access the data. The Following Works: It displays console entries with three to-do items: ...

What is the best way to arrange an array to display country data based on Covid cases in descending order rather than alphabetical order?

This particular code snippet retrieves Covid19 statistics data using an API. Currently, it displays the data for covid cases of all countries in Alphabetical order. However, I am looking to present the data in descending order, meaning that the country wit ...

Troubleshooting problems with dropdown binding in Knockout ObservableArray

I am encountering an issue with fetching data from a restful wcf service during page load and binding it to a dropdown, which is not functioning as expected. function CreateItem(name, value) { var self = this; self.itemNam ...

What is the method for incorporating text into the Forge Viewer using three.js?

Currently, I am attempting to incorporate TextGeometry into the viewer using Three.js. I am curious about the feasibility of this task and the steps to achieve it. After exploring the documentation, I encountered challenges as the Forge viewer operates on ...

Leverage the Power of JSON Data Manipulation in WordPress with Angular JS and the WordPress JSON

After testing code in this particular post and making some adjustments, I encountered an issue with obtaining a JSON object from the API of my blog created using the WordPress JSON plugin. URL of API from BLOG (NOT FUNCTIONING): URL from W3C example (WO ...

Validate if the user is actively scrolling; if not, automatically adjust the scroll position to the bottom

I am currently developing a chat site where the chat box updates every 200 milliseconds. I have managed to reposition the scrollbar to the bottom when a new message is loaded. However, I encountered a problem - whenever a user tries to scroll to the top, t ...

Keep things in line with async functions in Node JS

Hello, I am just starting out with NodeJs and I have a question. I am trying to push elements into an array called files based on the order of the urls provided, but it seems like I'm getting a random order instead. Below is the code I've been wo ...

I desire to alter the description of an item within a separate div, specifically in a bootstrap

I used the map method to render all the images. However, I need a way to track the current image index so that I can change the item description located in another div. Alternatively, if you have any other solutions, please let me know. App.js : import Ca ...

Assigning a class to an li element once the page has finished loading

I am facing an issue with my navigation bar where the links are dynamically loaded from a database using a foreach loop. Although the nav bar is static, I want to apply an 'Active' class to the link when it is currently active. Despite trying to ...

The 'this' variable is malfunctioning when trying to assign a JSONP array to an AngularJS controller

I'm working with a REST service in my angular controller and using JSONP to make the call. I need to store the array that is returned from the service into a variable. This is what I currently have: this.testTheRest = function() { $http.jsonp(&a ...

Perpetual duplication of data occurs upon inserting an item into the Array state of a React component

Whenever a new item is added to the React JS Array state, data duplication occurs. console.log(newData) The above code outputs a single object, but when you print the actual data with console.log(data) You will notice continuous duplicates of the same da ...

React and Redux encounter an issue where selecting a Select option only works on the second attempt, not the first

I am currently working on a React/Redux CRUD form that can be found here. ISSUE RESOLVED: I have encountered an issue where the state should be updated by Redux after making an API call instead of using this.setState. The concept is simple: when a user s ...

Passing data from the controller to the $resource object in Angular with the MEAN stack

After several attempts to troubleshoot my issue with passing parameters to the Express server, it turns out the problem lies on the Angular side. When I manually inserted the id in the flConstruct.js function, the query worked correctly. It appears that ...

Error: The Jquery plugin Object #<HTMLDocument> does not have a function called 'observe'

Hello, I recently tried to install the Jquery plugin known as chosen that allows customization of my <select> tag across different browsers. If you're interested, you can find more information about it here. However, after integrating this plugi ...

Display additional images with "Show More" view

I'm looking to create a view similar to the one shown in this image: https://i.stack.imgur.com/AZf61.png Within my FlatList, all the data is being populated including these thumbnails. These thumbnails are stored in an array of objects where I retri ...

Having trouble sending data through a jQuery AJAX request?

When I make a post request using jQuery AJAX to an ActionResult method, I encounter the following issue: $("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) { if ((e.keyCode == 120){ var GetReportLast5SellAndBuyURL="/Trd/Se ...

Failed to establish Modbus TCP connection

Currently, I am utilizing the "Panasonic FP7" master PLC along with their official software "FPWIN GR7" to monitor data flow on my PC. However, since the software lacks certain functions, I have decided to develop a solution using nodeJS. Below is the code ...

What is the best way to include a substantial amount of HTML in a Vue.js template?

As a newcomer to Vue.js, I have a question regarding the rendering of a large amount of HTML in a Vue.js template. When I include around 500 lines of plain HTML code in my template and run npm run dev the compiling process becomes extremely slow or d ...

What is the best way to apply various styles using the ng-style directive in different scenarios?

When working in Angular, I am attempting to apply different style attributes based on varying conditions. However, the typical conditional expression method is limited to just two conditions and is not providing the desired results. <div ng-style=" ...

Using JQuery, retain only the initial N elements in a list and discard the rest

I'm looking to streamline a list of items in the DOM by keeping only the first N items, ideally through the use of slice and remove methods. Could someone provide me with the JQuery syntax for removing items that come after the first N in the list? ...