Week number calculation
- cdmmaui
- Posts: 708
- Joined: Fri Oct 28, 2005 9:53 am
- Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong
- Contact:
Week number calculation
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!
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!
- Willi Quintana
- Posts: 1070
- Joined: Sun Oct 09, 2005 10:41 pm
- Location: Cusco - Perú
- Contact:
Re: Week number calculation
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
- Willi Quintana
- Posts: 1070
- Joined: Sun Oct 09, 2005 10:41 pm
- Location: Cusco - Perú
- Contact:
Re: Week number calculation
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)
- cdmmaui
- Posts: 708
- Joined: Fri Oct 28, 2005 9:53 am
- Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong
- Contact:
- Enrico Maria Giordano
- Posts: 9054
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Week number calculation
Code: Select all | Expand
#include "Fivewin.ch"
FUNCTION MAIN()
? WEEK()
RETURN NIL- Willi Quintana
- Posts: 1070
- Joined: Sun Oct 09, 2005 10:41 pm
- Location: Cusco - Perú
- Contact:
Re: Week number calculation
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 )
- jose_murugosa
- Posts: 1279
- Joined: Mon Feb 06, 2006 4:28 pm
- Location: Uruguay
- Contact:
Re: Week number calculation
This is a simple solution:
Esta es una solución simple:
In spanish:
In English:
I hope it could help
Espero que ayude.
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
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
Espero que ayude.
Saludos/Regards,
José Murugosa
"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"
José Murugosa
"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"