Skip to main content
Manolo Garcia Carrasco
Participating Frequently
June 10, 2021
Answered

calculate week number of year

  • June 10, 2021
  • 2 replies
  • 4533 views

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

This topic has been closed for replies.
Correct answer try67

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...


After this line:

event.value = getWeekNumber(d);

Add this:

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

2 replies

bebarth
Community Expert
Community Expert
June 11, 2021

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).
@+

Manolo Garcia Carrasco
Participating Frequently
July 30, 2021

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

bebarth
Community Expert
Community Expert
July 31, 2021

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="";
}

 

try67
Community Expert
Community Expert
June 10, 2021

Google: "javascript get week number from date"