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);
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

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.