JS gets the last days of the month, the maximum days and the number of days of the week _javascript skills

Source: Internet
Author: User
Tags getdate

The example of this article is about how to get the last days of month, the maximum days and the number of days. Share to everyone for your reference, specific as follows:


JS gets one months maximum days

JS inside of the new date ("Xxxx/xx/xx") the date of the construction method has a beauty,
When you pass in the "xxxx/xx/0" (number No. 0), the date is the "XX" month the first one months of the last day ("XX" month's maximum value is 69, digression),
When you pass in the "XXXX/XX/1" (number 1th), the date is "XX" month after the first day of one months (oneself understand)
If you pass in "1999/13/0", you will get "1998/12/31". And the biggest advantage is that when you pass in "xxxx/3/0", will get XXXX year February of the last day, it will automatically determine whether it is a leap year to return 28 or 29, do not judge yourself,
So, we want to choose how many days the choice of year, only need

var temp=new Date ("Select year/Select Month +1/0");
return temp.getdate ()//Maximum days

Check, you can also use this method.

The following is a getdaysinmonth (year, Month) method written using JS to obtain the number of days of a certain month:

function Getdaysinmonth (year,month) {
   month = parseint (month,10) +1;
   var temp = new Date (year+ "/" +month+ "/0");
   return Temp.getdate ();

JS to get the day of the year is the first few weeks

/** * To determine whether the year is run year * * @param {Number} years/function Isleapyear {return (400 = 0) | |
(Year% 4 = 0 && Year% 100!= 0); /** * Get a January number of days in a year * * @param {numbers} years * @param {numbers} month/function getmonthdays (yearly, month) {RET urn [to, null, 31][month, M, M, M, M] | |
(Isleapyear (year) 29:28); /** * Get the day of the year is the first weeks * @param {number} y * @param {number} m * @param {number} D * @returns {Number} * */function gets WeekNumber (Y, M, d) {var now = new Date (y, m-1, D), year = Now.getfullyear (), month = Now.getmonth (), Day
  s = now.getdate ();
  That day is the number of days of that year for (var i = 0; i < month i++) {day = Getmonthdays (year, i); The first day of the year is the week. var yearfirstday = new Date (years, 0, 1). Getday () | |
  var week = null;
  if (Yearfirstday = = 1) {week = Math.ceil (days/yearfirstday);
    else {days = (7-yearfirstday + 1);
  Week = Math.ceil (DAYS/7) + 1;
Return week;


I hope this article will help you with your JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.