In many modern web applications, especially admin dashboards, filtering data by date is critical to improving both usability and performance. Rather than loading all records at once, allowing users to select a specific date range helps the system run faster, cleaner, and more efficiently.
In this article, we will walk through building a Date Range Picker that users can interact with to filter data based on the selected date range. We’ll implement this using jQuery, Moment.js, and Bootstrap — a solution commonly used in ASP.NET Core MVC applications.
Objective
The main goal of this guide is to implement a Date Range Picker in an ASP.NET Core MVC application, allowing users to filter data (such as order lists or transaction history) between specific start and end dates.
The Date Range Picker will provide:
A user-friendly UI
Controlled date selection
Backend-ready formatted values
Reduced user input errors
Key Features of the Date Range Picker
User-Friendly UI: A calendar icon combined with a text input field
Auto Date Formatting: Uses Moment.js to format dates
Predefined Ranges: "Last 30 Days", "Month to Date", "Quarter to Date", etc.
Backend-Friendly Format: Stores dates in YYYY-MM-DD format for database queries
Reset Option: Quickly reset to the last 30 days
No Manual Date Entry: Prevents format-related user errors
Step-by-Step Guide to Building the Date Range Picker
1. HTML Structure
We begin by setting up the HTML structure. This includes:
A visible input field to display the selected date range
Two hidden fields to store backend-friendly start and end dates
<div class="daterange-wrapper">
<i class="fa fa-calendar"></i>
<input type="text"
id="daterange"
class="form-control daterange-input"
readonly />
</div>
<input type="hidden" id="StartDate" />
<input type="hidden" id="EndDate" />
Explanation:
#daterangedisplays the formatted date range for users.#StartDateand#EndDatestore dates inYYYY-MM-DDformat for API or database queries.
2. Initialize Date Range Picker Using jQuery and Moment.js
Next, initialize the Date Range Picker using Moment.js and the jQuery daterangepicker plugin.
$(function () {
var today = moment();
var start = moment().subtract(29, 'days');
var end = today.clone();
$('#daterange').daterangepicker({
startDate: start,
endDate: end,
minDate: moment('1900-01-01'),
maxDate: today,
autoUpdateInput: true,
showDropdowns: true,
alwaysShowCalendars: true,
opens: 'right',
locale: {
format: 'DD MMM YYYY',
cancelLabel: 'Cancel'
},
ranges: {
'Last 30 Days': [moment().subtract(29, 'days'), today],
'Last 2 Months': [moment().subtract(2, 'months'), today],
'Last 3 Months': [moment().subtract(3, 'months'), today],
'Last 12 Months': [moment().subtract(12, 'months'), today],
'Month to Date': [moment().startOf('month'), today],
'Quarter to Date': [moment().startOf('quarter'), today]
}
});
});

Join the conversation! Your thoughts help the community grow.