JavaScript calculator: JavaScript calculates the current date is the year and the first weeks of the month

Source: Internet
Author: User
Tags date getdate return

Calculates the number of weeks of the current date in this year
Date.prototype.getWeekOfYear = function (Weekstart) {//Weekstart: Weekly start in week: Sunday: 0, Monday: 1, Tuesday: 2 ..., default to Sunday
Weekstart = (weekstart 0)-0;
if (isNaN (Weekstart) weekstart > 6)
Weekstart = 0;
var year = This.getfullyear ();
var firstday = new Date (year, 0, 1);
var firstweekdays = 7-firstday.getday () + Weekstart;
var dayofyear = ((New Date (year, This.getmonth (), This.getdate ())-FirstDay)/(24 * 3600 * 1000)) + 1;
Return Math.ceil ((dayofyear-firstweekdays)/7) + 1;
}
Calculates the number of weeks of the current date in this month
Date.prototype.getWeekOfMonth = function (Weekstart) {
Weekstart = (weekstart 0)-0;
if (isNaN (Weekstart) weekstart > 6)
Weekstart = 0;
var DayOfWeek = This.getday ();
var day = This.getdate ();
Return Math.ceil ((day-dayofweek-1)/7) + ((DayOfWeek >= weekstart)? 1:0);
}
Use
var date = new Date (2011, 11, 31); Note: The value range of JS in the month is 0~11
var weekofyear = Date.getweekofyear (); The current date is the first weeks of the year
var weekofmonth = Date.getweekofmonth (); The current date is the first few weeks of this month
2011 is a couple of weeks.
(New Date (a)). Getweekofyear ();
2011 A few weeks in January
(New Date (0)). Getweekofmonth ();
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130101/35445.html

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.