Solved

Calendar extender prevent selection of previous date

Posted on 2013-01-10
9
1,366 Views
Last Modified: 2013-01-25
I am trying to prevent people from selecting an earlier date than today.

I found this function that works great
   function checkDate(sender, args) {
        alert(sender._selectedDate);
        alert(new Date());
        if (sender._selectedDate < new Date()) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }
  <asp:CalendarExtender   OnClientDateSelectionChanged="checkDate"   Format="yyyy/MM/dd" ID="Tbx_targetDate_CE" runat="server" Enabled="True"
                                TargetControlID="Tbx_targetDate">

But the sender._selectedDate shows a day ealier so if i select the current date on the calendar I will get the alert "You cannot select a day earlier than today!");"
I need to be able to select the current day
0
Comment
Question by:arnololo123
  • 4
  • 4
9 Comments
 
LVL 21

Expert Comment

by:masterpass
ID: 38765223
Have a look at this approach. The logic is inside the extension.js file.

http://www.aspsnippets.com/Articles/AJAX-CalendarExtender-Disable-Past-Date-Selection.aspx

Hope this helps :)
0
 

Author Comment

by:arnololo123
ID: 38765341
I have seen this method, the one I am using is working for many other and quiet easy to implement, I just don't understand why the selected date is a day earlier.
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38765406
Try something like this :

function checkDate(sender, args) {
	var selectedDate = new Date(sender._selectedDate);
	var dateToday = new Date();
	dateToday.setHours(0,0,0,0);
        if (selectedDate < dateToday) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }

Open in new window


The reason is that datepicker has time associated with it.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:arnololo123
ID: 38765606
IT still shows a day earlier which still create the alert when I select the day of today.

Regards
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38765657
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38766318
I feel Format="yyyy/MM/dd" could be causing the issue.

Can you change it to Format="MM/dd/yyyy" and check.
0
 

Author Comment

by:arnololo123
ID: 38767099
it does not make a difference when I change the format.
    function checkDate(sender, args) {
        var selectedDate = new Date(sender._selectedDate);
        var dateToday = new Date();
        dateToday.setHours(0, 0, 0, 0);
        selectedDate.setHours(0, 0, 0, 0);
        alert(selectedDate);alert(dateToday);
        if (selectedDate < dateToday) {

            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }

I still have the probleme where The two alert show eaxctly one day difference, same time 00:00:00
0
 

Author Comment

by:arnololo123
ID: 38776409
Sorry to insist but  does anybody has any idea on how to fix my problem?
0
 
LVL 21

Accepted Solution

by:
masterpass earned 500 total points
ID: 38776439
modify it to something like this:

if (selectedDate < (dateToday.getTime() - 864e5)) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }

Hope this helps.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question