how to calculate the week number of a date

1132
4
Jump to solution
09-30-2022 04:37 AM
JavierCMartínezPrieto
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)
0 Kudos
1 Solution

Accepted Solutions
IsmaelChivite
Esri Notable Contributor

Hi @JavierCMartínezPrieto 

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

IsmaelChivite_0-1664582956934.png

ISOWek.gif

 

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

View solution in original post

4 Replies
JohannesLindner
MVP Frequent Contributor

Date Functions | ArcGIS Arcade | ArcGIS Developers

Week(Today())

Have a great day!
Johannes
0 Kudos
mikAMD
by
Occasional Contributor II

this is misleading since arcade functions do not work in survey123

0 Kudos
IsmaelChivite
Esri Notable Contributor

Hi @JavierCMartínezPrieto 

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

IsmaelChivite_0-1664582956934.png

ISOWek.gif

 

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

JavierCMartínezPrieto
Occasional Contributor

thank you for answering both of them.

Best regards Javier

Javier C. Martinez Prieto
0 Kudos