Home >Web Front-end >JS Tutorial >Detailed explanation of the steps to obtain the specific days of the month using JS
This time I will bring you a detailed explanation of the steps to get the specific number of days in a month with JS. What are the precautions for getting the specific number of days in a month with JS. The following is a practical case, let’s take a look.
JavascriptThe new Date("xxxx/xx/xx") in the construction method of this date has a wonderful thing, when you pass in "xxxx /xx/0" (number 0), the obtained date is the last day of the month before month "xx" (the maximum value of month "xx" is 69, off topic), if "1999/13/" is passed in 0", you will get "1998/12/31". And the biggest advantage is that when you pass in "xxxx/3/0", you will get the last day of February in xxxx year. It will automatically determine whether the current year is a leap year and return 28 or 29. You don't have to judge by yourself. It's so convenient! ! Therefore, if we want to select how many days there are in a certain year and month, we only need
var d=new Date("选择年/选择月+1/0"); alert(d.getDate());
The following iswritten using Javascriptto get the number of days in a certain month of a certain yeargetDaysInOneMonth(year, month)
Method:
function getDaysInOneMonth(year, month){ month = parseInt(month,10)+1; var d= new Date(year+"/"+month+"/0"); return d.getDate(); }
Note: After my test, I found that Chrome browser (Google browser) does not support this feature, and the result obtained is NAN. Compatibility issue!
You can try other methods to get the total number of days in a certain month.
Modify it as follows:
function getDaysInOneMonth(year, month){ month = parseInt(month, 10); var d= new Date(year, month, 0); return d.getDate(); }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use webpack3.0 to configure webpack-dev-server
How to configure the WeChat applet Develop verification code password input box function
The above is the detailed content of Detailed explanation of the steps to obtain the specific days of the month using JS. For more information, please follow other related articles on the PHP Chinese website!