Answered by:
Count Date Except Sunday Javascript

Question
-
User2033107836 posted
Hello
This is my javascript code
<script type="text/javascript"> function checkDate(sender, args) { var td = new Date(); td.setMinutes(59); td.setSeconds(59); td.setHours(23); //to move back one day td.setDate(td.getDate() - 3); //Check if the date selected is less than todays date if (sender._selectedDate < td || sender._selectedDate > new Date() ) { //show the alert message alert("Please Select Todays Date Or 5 Days Before"); //set the selected date to todays date in calendar extender control sender._selectedDate = new Date(); // set the date back to the current date sender._textbox.set_Value(sender._selectedDate.format(sender._format)) } } </script>
and i have binded this java script to ajax calendar extender
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd-MMM-yyyy" PopupButtonID="Tdoi" OnClientDateSelectionChanged="checkDate" TargetControlID="Tdoi">
When i select calendar it count including Sunday but i want my java script to show except Sunday white counting 5 days?
Need Query
Thank You
Wednesday, October 26, 2016 6:27 PM
Answers
-
User283571144 posted
Hi asp.ambur,
Count Date Except Sunday JavascriptAccording to your description, I suggest you could use "sender._selectedDate.getDay()" method to adjust the selected date is Sunday.
If this value is 0, it means this day is Sunday.
More details, you could refer to follow codes:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script src="../Scripts/jquery-1.8.1.min.js"></script> <title></title> <script> function checkDate(sender, args) { //today var td = new Date($.now()); td.setDate(td.getDate() - 7); if (sender._selectedDate < td || sender._selectedDate > new Date() || sender._selectedDate.getDay() == 0) { alert("Please Select Todays Date Or 5 Days Before or Not Sunday"); var now = new Date($.now()); sender._textbox.set_Value(now.format(sender._format)); } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:TextBox ID="Tdoi" runat="server"></asp:TextBox> <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd-MMM-yyyy" PopupButtonID="Tdoi" OnClientDateSelectionChanged="checkDate" TargetControlID="Tdoi"/> </div> </form> </body> </html>
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 27, 2016 11:56 AM
All replies
-
User-691209617 posted
You can achieve in below mentioned way.
sender._selectedDate.getDay() // gives you sunday
Hope it helps
Wednesday, October 26, 2016 6:47 PM -
User2033107836 posted
You can achieve in below mentioned way.
sender._selectedDate.getDay() // gives you sunday
Hope it helps
Will You Please Give me the full code.. I'm a new to javascript so that i can copy and paste the code.
Thank You
Wednesday, October 26, 2016 6:51 PM -
User-691209617 posted
Here is the calculation.
function checkDate(sender, args) { var ms = new Date().getTime() + 6 * 86400000; var weekFar = new Date(ms); if (sender._selectedDate < weekFar) { //getTime() gives milliseconds //and 86400000 is the number of //milliseconds in a day. alert("You can't select a day earlier than a week!"); // set the date back to a week from today var ms1 = new Date().getTime() + 7 * 86400000; var weekFar1 = new Date(ms1); sender._selectedDate = weekFar1; sender._textbox.set_Value(sender. _selectedDate.format(sender._format)) } }
Wednesday, October 26, 2016 7:13 PM -
User2033107836 posted
Let me tell you clear
Today is 27-Oct-2016 in calender extender
scenario 1 : if user select 28-Oct-2016 alert message will show coz cant allow to select next day
scenario 2 : allow user to select past 6 days that is 21-Oct-2016 friday one day, 22-Oct saturday second day, 23-Oct Sunday leave this day, 24-Oct Monday third day, 25-Oct tuesday fourth day, 26-Oct wednesday fifth day, 27-Oct Thursday sixth day.
Hope you got need full code
Thanks in advance
Wednesday, October 26, 2016 7:25 PM -
User-691209617 posted
Here you go to count working days. I tried to handle all the condition add logic for weekend saturday and sunday. you can restrict to sunday or any day.
function checkDate(sender, args) { var days = workingDaysBetweenDates(new Date(),sender._selectedDate); alert(days); }
function workingDaysBetweenDates(startDate, endDate) { // Validate input if (endDate < startDate) return 0; // Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDate.setHours(0, 0, 0, 1); // Start just after midnight endDate.setHours(23, 59, 59, 999); // End just before midnight var diff = endDate - startDate; // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); days = days - (weeks * 2); // Handle special cases var startDay = startDate.getDay(); var endDay = endDate.getDay(); // Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay == 0 && endDay != 6) { days = days - 1; } // Remove end day if span ends on Saturday but starts after Sunday if (endDay == 6 && startDay != 0) { days = days - 1; } return days; }
Hope it helps.
Wednesday, October 26, 2016 8:01 PM -
User2033107836 posted
Tried Your Code Not Working..
If you have any doubt on my requirement
Let me tell you clear
Today is 27-Oct-2016 in calender extender
scenario 1 : if user select 28-Oct-2016 alert message will show coz cant allow to select next day
scenario 2 : allow user to select past 6 days that is
21-Oct-2016 is Friday = one day,
22-Oct is Saturday = second day,
23-Oct Sunday leave this day coz sunday,
24-Oct is Monday = third day,
25-Oct is Tuesday = fourth day,
26-Oct is Wednesday = fifth day,
27-Oct is Thursday = sixth day.
If user select 28-Oct (or) before 21 oct then we should bind 27-Oct Today's Date only
Hope you got it my requirement and need full code
Thanks in advance
Thursday, October 27, 2016 6:18 AM -
User283571144 posted
Hi asp.ambur,
Count Date Except Sunday JavascriptAccording to your description, I suggest you could use "sender._selectedDate.getDay()" method to adjust the selected date is Sunday.
If this value is 0, it means this day is Sunday.
More details, you could refer to follow codes:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script src="../Scripts/jquery-1.8.1.min.js"></script> <title></title> <script> function checkDate(sender, args) { //today var td = new Date($.now()); td.setDate(td.getDate() - 7); if (sender._selectedDate < td || sender._selectedDate > new Date() || sender._selectedDate.getDay() == 0) { alert("Please Select Todays Date Or 5 Days Before or Not Sunday"); var now = new Date($.now()); sender._textbox.set_Value(now.format(sender._format)); } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:TextBox ID="Tdoi" runat="server"></asp:TextBox> <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd-MMM-yyyy" PopupButtonID="Tdoi" OnClientDateSelectionChanged="checkDate" TargetControlID="Tdoi"/> </div> </form> </body> </html>
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 27, 2016 11:56 AM