Is there a way to get the week number out of a date?

Hi,

The ‘format-date’ function cannot give return it. Any idea how to compute
it?

Thanks

Guillaume,

Do you mean the Day of The Week (1-7) or “the current week of the year”
(1-52)?

-Clayton

··· On Wed, Mar 25, 2015 at 2:24 PM, Guillaume Deflaux < deflaux.guillaume@gmail.com> wrote:

Hi,

The ‘format-date’ function cannot give return it. Any idea how to compute
it?

Thanks


You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

I mean the week number of the year (1-52).

··· On Wednesday, March 25, 2015 at 8:18:26 PM UTC, Clayton Sims wrote: > > Guillaume, > > Do you mean the Day of The Week (1-7) or "the current week of the year" > (1-52)? > > -Clayton > > On Wed, Mar 25, 2015 at 2:24 PM, Guillaume Deflaux <deflaux....@gmail.com > wrote: > >> Hi, >> >> The 'format-date' function cannot give return it. Any idea how to compute >> it? >> >> Thanks >> >> -- >> You received this message because you are subscribed to the Google Groups >> "commcare-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to commcare-user...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > >

Guillaume,

I don’t think we support a super easy way to get that, but this calculation
seems like it does the right thing to me.

int((now() - date(concat(format-date(now(),’%Y’),’-01-01’))) div 7)

-Clayton

··· On Thu, Mar 26, 2015 at 5:33 AM, Guillaume Deflaux < deflaux.guillaume@gmail.com> wrote:

I mean the week number of the year (1-52).

On Wednesday, March 25, 2015 at 8:18:26 PM UTC, Clayton Sims wrote:

Guillaume,

Do you mean the Day of The Week (1-7) or “the current week of the year”
(1-52)?

-Clayton

On Wed, Mar 25, 2015 at 2:24 PM, Guillaume Deflaux <deflaux....@gmail.com wrote:

Hi,

The ‘format-date’ function cannot give return it. Any idea how to
compute it?

Thanks


You received this message because you are subscribed to the Google
Groups “commcare-users” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to commcare-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yes good idea. Thanks.

··· On Thursday, March 26, 2015 at 2:21:52 PM UTC, Clayton Sims wrote: > > Guillaume, > > I don't think we support a super easy way to get that, but this > calculation seems like it does the right thing to me. > > int((now() - date(concat(format-date(now(),'%Y'),'-01-01'))) div 7) > > -Clayton > > On Thu, Mar 26, 2015 at 5:33 AM, Guillaume Deflaux <deflaux....@gmail.com > wrote: > >> I mean the week number of the year (1-52). >> >> On Wednesday, March 25, 2015 at 8:18:26 PM UTC, Clayton Sims wrote: >>> >>> Guillaume, >>> >>> Do you mean the Day of The Week (1-7) or "the current week of the year" >>> (1-52)? >>> >>> -Clayton >>> >>> On Wed, Mar 25, 2015 at 2:24 PM, Guillaume Deflaux < deflaux....@gmail.com> wrote: >>> >>>> Hi, >>>> >>>> The 'format-date' function cannot give return it. Any idea how to >>>> compute it? >>>> >>>> Thanks >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "commcare-users" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to commcare-user...@googlegroups.com. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "commcare-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to commcare-user...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > >

Dear Clayton,

thank you for this idea, can you give example .
because I don’t know how and where I use this validation.

best,

Clayton,

What if, you have a question where you ask the date, and would like to use the answer to calculate the week number:

Using date_of_choice (the date of answer) in place of the first now() does the work; but, only the week start day is Tuesday.
: int((#form/date_of_choice - date(concat(format-date(now(), ‘%Y’), ‘-01-01’))) div 7)

Since the calculation listed doesn’t depend on any time components (just date components), you can replace “now()” in that expression with “date(#form/path/to_your_question”) and it should provide the same output.

-Clayton

Hi everyone,

I’ve been working on it, and wanted to stick to the ISO rules of counting weeks numbers. Unfortunately, int((now() - date(concat(format-date(now(),’%Y’),’-01-01’))) div 7) doesn’t work very well for some cases such as week 53 or new year’s eve in the middle of a week…

After some trials, it seem that this solution works fine and give the folowing format : 2019W02.

Form Builder clip	version 1
id	type	labelItext:en-default	calculateAttr	defaultValue
/calcul_week_nb/entrez_une_date	Date	Entrez une date	null	date('2019-12-31')
/calcul_week_nb/date	DataBindOnly	null	coalesce(#form/calcul_week_nb/entrez_une_date, today())	null
/calcul_week_nb/calcul1	DataBindOnly	null	date(concat(format-date(date(#form/calcul_week_nb/date - ((#form/calcul_week_nb/date - 4) mod 7) + 3), '%Y'), '-01-01'))	null
/calcul_week_nb/week_nb	DataBindOnly	null	floor((#form/calcul_week_nb/date - #form/calcul_week_nb/calcul1 + (#form/calcul_week_nb/calcul1 mod 7) + 3) div 7)	null
/calcul_week_nb/week_nb_year	DataBindOnly	null	if(format-date(#form/calcul_week_nb/calcul1, '%M') < format-date(#form/calcul_week_nb/date, '%M'), format-date(#form/calcul_week_nb/calcul1, '%Y') + 1, format-date(#form/calcul_week_nb/calcul1, '%Y'))	null
/calcul_week_nb/dhis2_real_date	DataBindOnly	null	concat(#form/calcul_week_nb/week_nb_year, 'W', if(#form/calcul_week_nb/week_nb < 10, '0', ''), #form/calcul_week_nb/week_nb)	null

Basically what it does is to look for the Thursday of the current week and check wether or not the current date belong to the year before this Thursday or to the year after.

There may be some way to achieve the same results more elegantly…

Please use those formula after you’ve tested it several times !
Enjoy,

-Michel

Thank you for sharing that solution, Michel, I’m sure others will find that useful.

I haven’t yet reviewed it, but it looks very impressive!