Page 1 of 1

Week number calculation

Posted: Mon Nov 03, 2025 1:28 am
by cdmmaui
Hello Everyone,

By any chance does anyone have a function that calculations the week number of the year based on day of the week starting on Sunday or Monday?

Thank you!

Re: Week number calculation

Posted: Mon Nov 03, 2025 2:46 am
by Willi Quintana
HI.. (from CHATGPT)

Code: Select all | Expand

FUNCTION WeekOfYear( dFecha )
   LOCAL nDiaAnio, nSemana
   // Calcula el día del año (1 a 365/366)
   nDiaAnio := DOY( dFecha )
   // Calcula la semana (asumiendo que la semana 1 comienza el 1 de enero)
   nSemana := INT( ( nDiaAnio - 1 ) / 7 ) + 1
 RETURN nSemana
🔹 Ejemplo de uso:
? WeekOfYear( CTOD("02/11/2025") )   // Devuelve 45

Re: Week number calculation

Posted: Mon Nov 03, 2025 2:48 am
by Willi Quintana
From CHATGPT (estándar ISO 8601, que es el usado por sistemas contables y ERP (donde la semana 1 es la que contiene el primer jueves del año, y las semanas comienzan en lunes)

Code: Select all | Expand

FUNCTION WeekISO( dFecha )
   LOCAL dJueves, dInicioAnio, nDiaAnio, nSemana
   // ISO dice que la semana se define por el jueves
   dJueves := dFecha - ( DOW( dFecha ) + 5 ) % 7 + 3
   // Primer jueves del año
   dInicioAnio := STOD( STR( YEAR( dFecha ), 4 ) + "0104" ) - ;
                  ( DOW( STOD( STR( YEAR( dFecha ), 4 ) + "0104" ) ) + 5 ) % 7 + 3
   // Número de semana
   nSemana := 1 + INT( ( dJueves - dInicioAnio ) / 7 )
 RETURN nSemana
🔹 Ejemplo de uso:
? WeekISO( CTOD("02/11/2025") )   // Devuelve 44 (semana ISO)

Re: Week number calculation

Posted: Mon Nov 03, 2025 2:58 am
by cdmmaui
Gracias Willi

Re: Week number calculation

Posted: Mon Nov 03, 2025 3:01 pm
by Enrico Maria Giordano

Code: Select all | Expand

#include "Fivewin.ch"


FUNCTION MAIN()

    ? WEEK()

    RETURN NIL

Re: Week number calculation

Posted: Tue Nov 04, 2025 2:39 pm
by Willi Quintana
Hola,, aqui mas preciso

Code: Select all | Expand

Function cISO8061Week( dDate )   
local dThu, nDay
#ifdef __XHARBOUR__   
 nDay        := dDate % 7      // Mon .. Sun as 0 .. 6
#else   
 nDay        := ( dDate - CToD( '' ) ) % 7
#endif   
dThu         := dDate - nDay + 3
return Str( Year( dThu ), 4 ) + '-W' + StrZero( Week( dDate ), 2 ) + '-' + Str(  nDay + 1, 1 )  

Re: Week number calculation

Posted: Wed Nov 05, 2025 1:52 pm
by jose_murugosa
This is a simple solution:
Esta es una solución simple:

In spanish:

Code: Select all | Expand

#include 'fivewin.ch'

FUNCTION Semana( wfecha )
         LOCAL wanio, wddsemana
         
         DEFAULT wfecha:=Date()
         wanio:=Year( wfecha )
         wddsemana:=Dow( wfecha )
         ?hb_Week( wfecha, @wanio, @wddsemana ) 
         
RETURN nil
In English:

Code: Select all | Expand

#include 'fivewin.ch'

FUNCTION WeekNumber( wDate )
         LOCAL wYear, wDayOfWeek
  
         DEFAULT wDate:=Date()
         wYear:=Year( wDate )
         wDayOfWeek:=Dow( wDate )
         ?hb_Week( wDate, @wYear, @wDayOfWeek ) 
         
RETURN nil
I hope it could help
Espero que ayude.