Solved

Calendar extender prevent selection of previous date

Posted on 2013-01-10
9
1,343 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now