3

I have a asp:textbox , how can i set its minimum date to today using javascript:

With C# I am doing it like this and it works fine..but I have to do it using Js/Jquery

DateTime date = DateTime.Today.Date;
            String today = date.ToString("yyyy-MM-dd");

            tourStartDate.Attributes["min"] =today;
<asp:TextBox Width="95%" ID="tourStartDate" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox></td>
  • possible duplicate of [Minimum and maximum date](http://stackoverflow.com/questions/11526504/minimum-and-maximum-date) – Shirish Jun 29 '15 at 06:01
  • Can you show the SetDate() method ? – sudhansu63 Jul 01 '15 at 07:39
  • function SetDate() { if (document.getElementById('<%=tourEndDate.ClientID%>').value <= document.getElementById('<%=tourStartDate.ClientID%>').value) { $('#<%=tourEndDate.ClientID %>').val(document.getElementById('<%=tourStartDate.ClientID%>').value); } – Syed Muhammad Yasir Jul 01 '15 at 08:43
  • @SyedMuhammadYasir Please add the content of your comment to your question. After that delete your comment. – Reporter Feb 15 '17 at 13:30

3 Answers3

3

On Code Behind C#:

tourStartDate.Attributes["max"] = DateTime.Now.ToString("yyyy-MM-dd");

VB.net:

tourStartDate.Attributes("max") = Now.ToString("yyyy-MM-dd")
Seb33300
  • 7,464
  • 2
  • 40
  • 57
lrturbo
  • 31
  • 3
0

you need to put the ClientIdMode = static in the server control to get the static Id.

<asp:TextBox Width="95%" ID="tourStartDate" ClientIdMode = "static" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox>

Jquery: Set today in attribute.

$("#tourStartDate").attr("min", (new Date()));

EDIT:

Can you try <input type="date" min="2015-07-01" max="2015-10-20">

JQuery:

  $("#tourStartDate").attr("min", (new Date()).toISOString().substring(0,10));

Make sure your doc type is html ( for HTML 5 controls)

EDIT2 :

JavaScript :

 document.getElementById('tourStartDate').setAttribute('min', (new Date()).toISOString().substring(0,10));
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
0

My design code is :

<asp:TextBox ID="txtDate" runat="server" MaxLength="30" CssClass="form-control" autocomplete="off"></asp:TextBox>

Javascript code and jquery code

$(function () {
  $("#ContentPlaceHolder1_txtDate").datepicker({
    minDate: new Date(new Date().getTime() - 2 * 24 * 60 * 60 * 1000),
    maxDate: new Date()
  });
});
InSync
  • 4,851
  • 4
  • 8
  • 30
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '23 at 17:27