Articles in this section
Category / Section

How to Sort Datasource Based on Date Time in JavaScript Timeline?

6 mins read

This article explains how to sort a datasource based on dateTime in JavaScript Timeline.

The following steps are used to sort the data source based on date and time in the JavaScript Timeline component.
Step 1: Implement the Timeline component using our Getting Started documentation.

Step 2: To achieve this requirement, send the Timeline component data source as a parameter to the sortDatasByDateTime method.

Step 3: In the sortDatasByDateTime function, sort the data source based on date and time using the sort function.

Step 4: Define a helper function, which is 'parseDateTime,' to convert the datetime strings in the data source into JavaScript Date objects. This will allow you to sort the items chronologically.

 

[index.js]

// Function to parse datetime strings

function parseDateTime(datetime) {

  return new Date(datetime);

}

 

// Function to sort data based on datetime values

function sortDatasByDateTime(data) {

  return data.sort((a, b) => {

    const dateA = parseDateTime(a.datetime);

    const dateB = parseDateTime(b.datetime);

    return dateA - dateB;

  });

}

 

// Sort datas array

datas = sortDatasByDateTime(datas);

 

var defaultTimeline = new ej.layouts.Timeline({

  items: datas,

  orientation: 'Horizontal',

});

defaultTimeline.appendTo('#default');

Refer to the working sample for additional details and implementation: Timeline-SortedDatasource

 

See Also : 

 

Conclusion

We hope you enjoyed learning how to sort datasource based on date time in JavaScript Timeline

You can refer to our JavaScript Timeline feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Timeline example to understand how to create and manipulate data.For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsBoldDesk Support, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied