Hi
I have FROM DATE and TO DATE textbox on page. I want to validate textbox where User is allow to enter only 7 days date range in both textboxes. For Ex. If date in FROM DATE textbox is 10/07/2023, then TO DATE must be 17/07/2023 and If TO DATE changes then accordingly FROM DATE should be -7 days back.
User should not allow to choose more than 7 days in both textbox, and should see data between only 7 days
Thanks

Amit MohantyRank #112 · 20K reputationPosted Jul 26, 2023, 3:49 AM EDT
Hey Sushant, try the below sample code.
ASPX Page:
FROM DATE: TO DATE: $(document).ready(function () { // Function to calculate the TO DATE when FROM DATE changes function updateToDate() { var fromDate = new Date($("#fromDate").val()); if (!isNaN(fromDate)) { var toDate = new Date(fromDate); toDate.setDate(toDate.getDate() + 7); $("#toDate").val(toDate.toISOString().slice(0, 10)); } } // Function to calculate the FROM DATE when TO DATE changes function updateFromDate() { var toDate = new Date($("#toDate").val()); if (!isNaN(toDate)) { var fromDate = new Date(toDate); fromDate.setDate(fromDate.getDate() - 7); $("#fromDate").val(fromDate.toISOString().slice(0, 10)); } } // Attach event listeners to the textboxes $("#fromDate").on("change", updateToDate); $("#toDate").on("change", updateFromDate); });Page_Load event:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DateTime currentDate = DateTime.Now.Date; fromDate.Text = currentDate.ToString("yyyy-MM-dd"); toDate.Text = currentDate.AddDays(7).ToString("yyyy-MM-dd"); fromDate.Attributes["min"] = currentDate.ToString("yyyy-MM-dd"); toDate.Attributes["min"] = currentDate.ToString("yyyy-MM-dd"); toDate.Attributes["max"] = currentDate.AddDays(7).ToString("yyyy-MM-dd"); } }Tahir AnsariRank #366 · 6.8K reputationPosted Jul 25, 2023, 10:41 PM EDT
Try
// Helper function to convert a date string to a Date object function parseDate(dateString) { const parts = dateString.split("/"); return new Date(parts[2], parts[1] - 1, parts[0]); } // Helper function to format a Date object to a date string function formatDate(date) { const day = date.getDate().toString().padStart(2, "0"); const month = (date.getMonth() + 1).toString().padStart(2, "0"); const year = date.getFullYear(); return `${day}/${month}/${year}`; } // Function to validate and update the date range function updateDateRange() { const fromDateInput = document.getElementById("fromDate"); const toDateInput = document.getElementById("toDate"); const fromDate = parseDate(fromDateInput.value); const toDate = parseDate(toDateInput.value); const sevenDaysAgo = new Date(); sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); if (fromDate.getTime() + 6 * 24 * 60 * 60 * 1000 !== toDate.getTime()) { toDate.setTime(fromDate.getTime() + 6 * 24 * 60 * 60 * 1000); toDateInput.value = formatDate(toDate); } if (fromDateVishal JoshiRank #377 · 6.7K reputationPosted Jul 26, 2023, 12:41 AM EDT
Hello Sushant,
try the below sample code to get difference between two dates in C#
ASPX Page code:
From Date: From Date: Difference: $(function () { $("#txtFDFromDate").datepicker({ dateFormat: "dd/mm/yy" }); $("#txtFDToDate").datepicker({ dateFormat: "dd/mm/yy", onSelect: function () { $('#btnDifference').trigger("click"); } }); });C# Code
protected void Difference(object sender, EventArgs e) { DateTime FDFromDate = DateTime.ParseExact(Request.Form["txtFDFromDate"], "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); DateTime FDToDate = DateTime.ParseExact(Request.Form["txtFDToDate"], "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); txtDifference.Text = (FDFromDate - FDToDate).TotalDays.ToString(); // you can write custom validation for date selection limit here. }Thanks
Subarta RayRank #336 · 7.4K reputationPosted Dec 26, 2023, 1:59 AM EST
To restrict users to a 7-day date range, use JavaScript. On TO DATE change, set FROM DATE to 7 days earlier, and vice versa. Example:
Anandu G NathRank #681 · 3.5K reputationPosted Dec 20, 2023, 2:09 AM EST
Try Below Code
// Attach event listeners to From Date and To Date textboxes document.getElementById('fromDate').addEventListener('change', validateDateRange); document.getElementById('toDate').addEventListener('change', validateDateRange); function validateDateRange() { var fromDate = new Date(document.getElementById('fromDate').value); var toDate = new Date(document.getElementById('toDate').value); var sevenDaysLater = new Date(fromDate); sevenDaysLater.setDate(sevenDaysLater.getDate() + 7); // Check if the date range is more than 7 days if (toDate - fromDate > 0 && toDate - fromDate sevenDaysLater) { fromDate = new Date(toDate); fromDate.setDate(fromDate.getDate() - 7); document.getElementById('fromDate').value = fromDate.toISOString().slice(0, 10); } } else { // Invalid date range, reset the To Date or show an error message alert('Please select a date range within 7 days.'); document.getElementById('toDate').value = ''; } } From Date: To Date:Anandu G NathRank #681 · 3.5K reputationPosted Dec 20, 2023, 2:09 AM EST
@Sushant Torankar
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 26, 2023, 4:26 AM EST
To validate the 7-day date range in C#, you can use the
DateTimeclass and some simple logic. Here's an example of how you can achieve this:// Get the values from the textboxes DateTime fromDate = DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); DateTime toDate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); // Calculate the difference in days TimeSpan difference = toDate - fromDate; // Check if the date range is within 7 days if (difference.Days == 7) { // Date range is valid // Perform your desired actions here } else { // Date range is not valid // Display an error message or take appropriate action }To automatically adjust the "FROM DATE" when the "TO DATE" changes, you can use an event handler for the "TO DATE" textbox's
TextChangedevent. Here's an example:private void txtToDate_TextChanged(object sender, EventArgs e) { // Get the new "TO DATE" value DateTime toDate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); // Calculate the new "FROM DATE" value DateTime fromDate = toDate.AddDays(-7); // Update the "FROM DATE" textbox txtFromDate.Text = fromDate.ToString("dd/MM/yyyy"); }By using these code snippets, you can ensure that the user can only enter a 7-day date range in both textboxes and automatically adjust the "FROM DATE" when the "TO DATE" changes.