how to calculate the week number of a date

121
3
09-30-2022 04:37 AM
Occasional Contributor

I would like to know if it is possible to calculate the number of the week from a date using today() in the calculation field.
I have tried the format-date with the %W operator, but it doesn't work.....

thanks to all of you who participate.

Regards Javier

Javier C. Martinez Prieto
Tags (3)
1 Solution

Accepted Solutions
Esri Notable Contributor

If using the mobile app, go with format-date as shown below:

Unfortunately format-date %W is not supported in the web app. If you want this to work in the web app you will need to use a custom JS function. For example:

function ISO8601_week_no(unix_timestamp)
{
var dt = new Date(unix_timestamp);
var tdt = new Date(dt.valueOf());
var dayn = (dt.getDay() + 6) % 7;
tdt.setDate(tdt.getDate() - dayn + 3);
var firstThursday = tdt.valueOf();
tdt.setMonth(0, 1);
if (tdt.getDay() !== 4)
{
tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);
}
return 1 + Math.ceil((firstThursday - tdt) / 604800000);
}

If you are not familiar with custom JS functions. Check this blog.

Attaching samples

3 Replies
MVP Regular Contributor

Date Functions | ArcGIS Arcade | ArcGIS Developers

Week(Today())

Have a great day!
Johannes
Esri Notable Contributor

If using the mobile app, go with format-date as shown below:

Unfortunately format-date %W is not supported in the web app. If you want this to work in the web app you will need to use a custom JS function. For example:

function ISO8601_week_no(unix_timestamp)
{
var dt = new Date(unix_timestamp);
var tdt = new Date(dt.valueOf());
var dayn = (dt.getDay() + 6) % 7;
tdt.setDate(tdt.getDate() - dayn + 3);
var firstThursday = tdt.valueOf();
tdt.setMonth(0, 1);
if (tdt.getDay() !== 4)
{
tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);
}
return 1 + Math.ceil((firstThursday - tdt) / 604800000);
}

If you are not familiar with custom JS functions. Check this blog.

Attaching samples

Occasional Contributor

thank you for answering both of them.

Best regards Javier

Javier C. Martinez Prieto