• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

calculate week number of year

Community Beginner ,
Jun 10, 2021 Jun 10, 2021

Copy link to clipboard

Copied

Hello, I need to calculate the number of weeks in a year from a date entered in a field.

TOPICS
JavaScript , PDF forms

Views

2.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Aug 03, 2021 Aug 03, 2021

You can use this code, based on the function you provided above, as the custom Calculation script of the WEEK field:

 

var s = this.getField("MYDATE").valueAsString;
if (s=="") event.value = "";
else {
	var d = util.scand("yymmdd", s);
	event.value = getWeekNumber(d);
}

function getWeekNumber(date) {
	date.setHours(0, 0, 0, 0);
	// Thursday in current week decides the year.
	date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
	// January 4 is always in week 1.
	var week1 = new Date(date
...

Votes

Translate

Translate
Community Expert , Sep 16, 2021 Sep 16, 2021

After this line:

event.value = getWeekNumber(d);

Add this:

if (event.value.length==1) event.value = "0"+event.value;

Votes

Translate

Translate
Community Expert ,
Jun 10, 2021 Jun 10, 2021

Copy link to clipboard

Copied

Google: "javascript get week number from date"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Here is the adaptation to the US date format of a file shared on the French forum a few years ago.
The next year with 53 weeks will be in 2026 (the previous one was in 2020).
@+

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2021 Jul 30, 2021

Copy link to clipboard

Copied

What I really need is that from a date field (DATE) I get the result in another field (WEEK).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2021 Jul 30, 2021

Copy link to clipboard

Copied

Find a function that does that and we'll help you integrate it into the form.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: https://weeknumber.com/how-to/javascript

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7);
}

// Returns the four-digit year corresponding to the ISO week of the date.
Date.prototype.getWeekYear = function() {
var date = new Date(this.getTime());
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
return date.getFullYear();
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

What I really need is that from a date field (MYDATE) get the week number of the year where the result appears another field (WEEK).
For example:
MYDATE (yymmdd): 210803
WEEK: 31

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 31, 2021 Jul 31, 2021

Copy link to clipboard

Copied

As you only indicate one date field, I guess the calculation must be done with the actual date.

Here is a validation script:

if (event.value!="") {
	var theDay=new Date(util.printd("mm/dd/yyyy", new Date(event.value))); 
	var timeDifference=new Date().getTime()-theDay.getTime();
	if (timeDifference>=0) {
		this.getField("theWeeks").value=Math.round(timeDifference/1000/3600/24/7);
	} else {
		app.alert("The indicated date must be earlier than today ",3);
		event.rc=false;
	}
} else {
	this.getField("theWeeks").value="";
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

What I really need is that from a date field (MYDATE) get the week number of the year where the result appears another field (WEEK).
For example:
MYDATE (yymmdd): 210803
WEEK: 31

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

You can use this code, based on the function you provided above, as the custom Calculation script of the WEEK field:

 

var s = this.getField("MYDATE").valueAsString;
if (s=="") event.value = "";
else {
	var d = util.scand("yymmdd", s);
	event.value = getWeekNumber(d);
}

function getWeekNumber(date) {
	date.setHours(0, 0, 0, 0);
	// Thursday in current week decides the year.
	date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
	// January 4 is always in week 1.
	var week1 = new Date(date.getFullYear(), 0, 4);
	// Adjust to Thursday in week 1 and count number of weeks from date to week1.
	return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
		- 3 + (week1.getDay() + 6) % 7) / 7);
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

Thank you very much for your time

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

It works very well but I need the result to be in double digits.
For example:
If I type 210916 the result is 37, but if I type 220103 the result is 1, this result is correct but I am interested that it is 01.

JANUARY > 01
FEBRUARY > 02
MARCH > 03
APRIL > 04
MAY > 05...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

After this line:

event.value = getWeekNumber(d);

Add this:

if (event.value.length==1) event.value = "0"+event.value;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

LATEST

Perfect, thank you very much for your professionalism.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines