Solved

Calendar extender prevent selection of previous date

Posted on 2013-01-10
9
1,403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
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

617 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