Input of type date time disabling dates until current date

Viewed 605

I have this calendar and I want to disable all dates until current date:

In my html

<div>
    <input asp-for="SelectedDate" id="SelectedDate"/>
</div>

In my js file

$(document).ready(function () {
    var date = new Date();
    var currentMonth = date.getMonth();
    var currentDate = date.getDate();
    var currentYear = date.getFullYear();
    
    var minDate = new Date(currentMonth, currentDate, currentYear);
    
    $("#SelectedDate").attr("min", minDate);
});

Backend:

[BindProperty]
public DateTime SelectedDate { get; set; }

I have to mention that I am using Razor Pages .Net Core.

The issue is that the min attribute is not working. I tried adding it in my html for a test like this but had no effect:

<input asp-for="SelectedDate" id="SelectedDate" min="05/11/2021"/>

Is there any workaround so that I can disable all dates untill the current date?

EDIT

enter image description here

1 Answers

In this article you can see if you want to set min and max time, you need follows the format yyyy-MM-ddThh:mm,and must have both min and max.

So you can change your code like below.

<input asp-for="SelectedDate" min="@DateTime.Now.ToString("yyyy-MM-ddThh:mm")"  max="2050-06-01T00:00" />
Related