FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Week number calculation
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Week number calculation
Posted: Mon Nov 03, 2025 01:28 AM

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!

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 1054
Joined: Sun Oct 09, 2005 10:41 PM
Re: Week number calculation
Posted: Mon Nov 03, 2025 02:46 AM
HI.. (from CHATGPT)
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
Posts: 1054
Joined: Sun Oct 09, 2005 10:41 PM
Re: Week number calculation
Posted: Mon Nov 03, 2025 02:48 AM
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)
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)
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: Week number calculation
Posted: Mon Nov 03, 2025 02:58 AM

Gracias Willi

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Week number calculation
Posted: Mon Nov 03, 2025 03:01 PM
#include "Fivewin.ch"


FUNCTION MAIN()

    ? WEEK()

    RETURN NIL
Posts: 1054
Joined: Sun Oct 09, 2005 10:41 PM
Re: Week number calculation
Posted: Tue Nov 04, 2025 02:39 PM
Hola,, aqui mas preciso
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 )
Posts: 1279
Joined: Mon Feb 06, 2006 04:28 PM
Re: Week number calculation
Posted: Wed Nov 05, 2025 01:52 PM
This is a simple solution:
Esta es una solución simple:

In spanish:
#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:
#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.
Saludos/Regards,

José Murugosa

"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"

Continue the discussion