Get all date in month javascript
is there a way to have all the days of a month or of a year? I am looking for this in order to disable some specific days in a datepicker, i would have a page in the back-office to select these days to disable. So i would need to show all the days in a month, and add a "activate or deactive" button below each day. Is there a way to find these days with the Date object? I found this link for example : Displaying all the days of a month but i don't really understand it, plus it is Java, i am trying to find a solution in javascript. Thank you for your help
Joshua Wade 4,1152 gold badges23 silver badges40 bronze badges asked Oct 30, 2012 at 19:37
1 To get a list of all days in a month, you can start with a
UTC VersionIn response to some comments, I've created a version that uses UTC methods in case you want to call UTC methods instead of the standard methods that return the localized time zone. I suspect this is the
culprit of the comments saying this didn't work. You typically want to make sure you call
Editing This AnswerIf you think there's a problem with this script, please feel free to:
Unit Tests
answered Oct 30, 2012 at 20:07
Ruan MendesRuan Mendes 87.2k30 gold badges146 silver badges208 bronze badges 7 One liner to get all days as Date object in a month
answered Jul 8, 2019 at 11:35
chickenschickens 16.7k4 gold badges54 silver badges51 bronze badges 1 I'm not sure from your description if the standard disable dates datepicker will work with you, so I'll answer you question directly. You can construct an array of days for a month fairly easily by doing this:
Using that array of days you can pretty much do whatever you want with it and know the day of the week as well. answered Oct 30, 2012 at 20:00
0 Using ES6 Array InitializerYou can get the number of days in a specified month, and then create a new array with that length and each of the days as items.
Demo in Stack Snippets
naveen 51.9k46 gold badges159 silver badges243 bronze badges
answered Sep 6, 2021 at 18:14
KyleMit♦KyleMit 34.6k61 gold badges427 silver badges618 bronze badges 3 Here's a loopp that runs through each month to determine the last day of that month. Javascript Date object indexes months starting at zero and if you set the day to zero it reverts back to last day of prior month. Handy for determining leap year last day for February
and the all important one to figure out is February with
DEMO: http://jsfiddle.net/5k8sn/1/ answered Oct 30, 2012 at 20:11
charlietflcharlietfl 170k13 gold badges115 silver badges146 bronze badges 0 I have implemented the functionality you requested using a jQuery datepicker. First, add all the dates selected in the back office to be disabled into an array
Second, specify the datepicker with two functions
Here is the definition of the required functions
Convert date to string for comparison with dates in array
Once a date has been selected process it
Here is a fiddle http://jsfiddle.net/KYzaR/7/ Just thought it would be worth mentioning that Array.indexOf is a recent addition to ECMA-262 standard and so in the case of IE7 and IE8 it is not supported. The following MDN page provides code that implements Array.indexOf for these browsers https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf answered Oct 30, 2012 at 23:32
BrunoBruno 5,6921 gold badge25 silver badges43 bronze badges 1 I have a solution. Happy coding ❤️ !! Note: 0=January, 1=February etc. example w3schools
answered Mar 19, 2021 at 11:30
3 Why don't you get all days in a month in array like this.
then count them
And then divide the total using Math.ceil 7 to get the number of rows for the number of weeks
answered Oct 18, 2021 at 22:05
This part "new Date(year, month , 0)" will get last day of month, so you need only iterate from this days creating all days fron 1 until last day.
answered Jan 26 at 23:40
|