Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calendar extender prevent selection of previous date

Posted on 2013-01-10
9
Medium Priority
?
1,436 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

772 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