JQuery Datepicker and implementing selected dates to disable dynamically

I have a classic ASP page that pulls dates from the database. These dates are dates that my jquery calendar must disable.

I need a page that takes information from the database and puts it into an array which is then put into the javascript.

In order to disable specific dates, we need to create a custom function. Our function will compare all the dates with an array of disabled dates.

So if a date is in disabled dates array our function will simply return false.  Here is the code snippet. The code is heavily commented and will help you in understanding.


Here is the Javascript for Jquery Datepicker

 <script>
 
 /** Days to be disabled as an array */
var disableddates = ["12-3-2019", "12-11-2019", "12-25-2019", "12-20-2019"];
 
function DisableSpecificDates(date) {
 
 var m = date.getMonth();
 var d = date.getDate();
 var y = date.getFullYear();
 
 // First convert the date in to the mm-dd-yyyy format 
 // Take note that we will increment the month count by 1 
 var currentdate = (m + 1) + '-' + d + '-' + y ;
 
  // We will now check if the date belongs to disableddates array 
 for (var i = 0; i < disableddates.length; i++) {
 
 // Now check if the current date is in disabled dates array. 
 if ($.inArray(currentdate, disableddates) != -1 ) {
 return [false];
 }
 }
 
}
 
 
 $(function() {
 $( "#datepicker" ).datepicker({
 beforeShowDay: DisableSpecificDates
 });
 });
 </script>

Open in new window

The inArray() method returns -1 if  it cannot find a match.


SQL Database of Dates
tbl_calendardates

/****** Object:  Table [dbo].[tbl_calendardates]    Script Date: 5/15/2019 11:30:35 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tbl_calendardates](
	[autoid] [int] IDENTITY(1,1) NOT NULL,
	[daterequired] [datetime] NULL
) ON [PRIMARY]
GO

Open in new window



Thank you in advance
LVL 1
souldjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
	<script type="text/javascript">
		 /** Days to be disabled as an array */
		var disableddates = [<%
			Dim objConn
			Set objConn = Server.CreateObject("ADODB.Connection")
			objConn.ConnectionString = "Provider=SQLOLEDB; Data Source=(local); Database=[DATABASE]; User ID=[USER ID]; Password=[PASSWORD]"
			objConn.Open	
			Dim SQL
			SQL = "select * from tbl_calendardates"

			Set rs = objConn.Execute(SQL)
			If conn.Errors.Count == 0 Then
				While Not rs.EOF
					For Each objField in rs.Fields
						Response.Write "'" & rs(objField.daterequired) & "',"
					Next
					rs.MoveNext
				Wend
				rs.Close
			End If

		%>];

		function DisableSpecificDates(date) {
			  // We will now check if the date belongs to disableddates array 
			 for (var i = 0; i < disableddates.length; i++) {
			 // Now check if the current date is in disabled dates array. 
				 if ($.inArray(currentdate, disableddates) != -1 ) {
				 	return [false];
				 }
			 }
		}

		$(function() {
			$( "#datepicker" ).datepicker({
				beforeShowDay: DisableSpecificDates
			 });
		});
	</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
classic asp

From novice to tech pro — start learning today.