laspalmos
laspalmos

Reputation: 149

Javascript checking date

I am trying to use JavaScript to validate that the date selected is not earlier than today, but when I select today's date it's showing the alert box.

JavaScript:

function checkDueDate(sender, args) {
    var td = new Date(); 
    td.setMinutes(59);
    td.setSeconds(59);
    td.setHours(23);
    //to move back one day
    td.setDate(td.getDate() - 1);

    if (sender._selectedDate < td) {
        alert("You can't select day from the past! " + td + "");
        sender._selectedDate = new Date();
        // set the date back to the current date
        sender._textbox.set_Value(sender._selectedDate.format(sender._format))
    }

ASP.NET:

        <asp:TextBox ID="txtDueDate" runat="server"></asp:TextBox>
        <asp:CalendarExtender ID="txtDueDate_CalendarExtender" runat="server" 
            TargetControlID="txtDueDate" OnClientDateSelectionChanged="checkDueDate">
        </asp:CalendarExtender>

Image

Upvotes: 1

Views: 1859

Answers (3)

Ram Singh
Ram Singh

Reputation: 6938

if you want to do select only future dates then you can try this code also....this is working with ajax calendar:

 function checkDate(sender, args) {
        if (sender._selectedDate < new Date()) {
            alert("You can select only future day!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }

Here is the HTML code:

 <asp:TextBox ID="txtDOB" Width="180px" MaxLength="50" runat="server"></asp:TextBox>
                            <ajaxctrl:calendarextender onclientdateselectionchanged="checkDate" id="cale_txtDOB"
                                runat="server" targetcontrolid="txtDOB" format="MM/dd/yyyy" cssclass="cal_Theme1">
                            </ajaxctrl:calendarextender>

This code works only if you select past dates it will show a pop up " that you can not select past dates" whatever be it. UPDATED CODE: Here is code work if you dont want to include today's date also, you just want future dates only:

function checkDate(sender, args) {
        if (sender._selectedDate <= new Date()) {
            alert("You can select only future day!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            //sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }

hope this will help you..

Upvotes: 0

elclanrs
elclanrs

Reputation: 94141

I think maybe you're complicating things too much. I would just subtract a day in miliseconds and it should work:

function isPast( date ) {
  return date.getTime() < (new Date().getTime() - 864e5); 
}

Demo: http://jsbin.com/igeyov/1/edit

Upvotes: 2

Chris O&#39;Kelly
Chris O&#39;Kelly

Reputation: 1893

the logic you have here seems to do exactly what you want - you have set the td variable which you evaluate against to the last possible second of todays date and you are checking if the selected date is before or equal to that. Todays date IS "before or equal to" 23:59:59 today...

Also, you have tagged this with c# , although it is all javascript and ASP.net as far as I can tell.

Upvotes: 0

Related Questions