Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Week of Year - (Funktion Kalenderwoche)
#1
Wink 
If you need a Funktion for Week of the Year...

(Internationale ISO 8601 Kalender-Wochen-Nummerierung)

Siehe 
https://kalenderwoche-online.de
https://www.brepols.com/Data/Downloads/ISO8601_RULE_FOR_DEFINING_WEEKNUMBERS.pdf


The QM - built-in function: s.time( "%W" ) gives me an incorrect result for today: week 38 instead of 39 (= the same as C style parameter %U)

New: Correction of the FIRST and the LAST Days in the Year (Schaltjahre / Leap years wo der 1.1. auf einen Mittwoch fällt, sind jetzt auch noch implementiert - diese haben auch 53 KW - hiezu verwenden ich die Function / I Use: "GetDaysInMonth" from TheVig and rename it to Funktion: "Schaltjahr")

Bemerkung: Ist der Rückgabewert "0" so liegt der Tag noch in der letzen KW des Vorjahres - die letzte KW des Vorjahres wird in einem 2. Durchgang ermittelt...

Neu: Jetzt ist auch der Funktionsaufruf mit/ohne Datumsangabe und Rückgabewert 2-stellig oder im internationalen Format möglich...

If there's an even simpler solution in QM (NOT MS-EXCEL or MS-OL...), please post it... Smile

Alle benötigten QM-Macros samt einem Beispiel- (Test-) Macro und ein KW-Tray Programm sind im Anhang zum Download!
 
Code:
Copy      Help
function'str [iY] [iM] [iD]  ;;WeekOfYear

  Die ISO 8601-Wochennummerierung folgt diesen Regeln:

    Wochenstart: Jede Woche beginnt an einem Montag und endet an einem Sonntag.
    Erste Woche: Die erste Woche des Jahres ist diejenige, die den ersten Donnerstag des Jahres enthält.
    Eine 53. Woche tritt auf, wenn der erste und der letzte Tag des Jahres ein Donnerstag ist,
    oder ein Schaltjahr wo der 1.Tag ein Mittwoch ist...
    Wenn vor der Jahreszahl ein Minuszeichen angegeben wird, wird nur die KW (=2-stelliger String) ausgegeben!

    Beispiele/Examples:
    WeekOfYear() or WeekOfYear(0) or WeekOfYear(-1) ;;Gets KW-String or KW-Short String from Todays Date
    WeekOfYear( 2026 12 31 );;Gets KW or KW-String from 31.Dez.2026 = 2026-W53-4
    WeekOfYear( 2027 01 01 );;Gets KW or KW-String from 1.Jän.2027 = Last KW from Last Year = 2026-W53-5
    WeekOfYear( 2027 01 04 );;Gets KW or KW-String from 4.Jän.2027 = 1. Day in 1. KW = 2027-W01-1
    WeekOfYear( -2027 01 04 );;Gets only KW from 4.Jän.2027 = 1. Day in 1. KW = 01

DATE dD
str sY

if iY <= 0
    sY.time( "%Y" )
else
    dD=F"{iD}/{iM}/{iY}"
    sY.time( dD "%Y" )
if iY < -1
    int iNoString=1
    iY=iY*-1
    dD=F"{iD}/{iM}/{iY}"
    sY.time( dD "%Y" )

if iY=-1
    iNoString=1

int iGLY=0

iY=val(sY)

_GLY

;Jahr ermitteln
;sY=2027
;out F"01/01/{iY}"

;out F"iGLY={iGLY}"
if iGLY=1
    iY=iY-1
    ;out iY

;Wochentag ermitteln (nur 1. Durchlauf)
if iGLY=0
    str sDOW.time( dD "%w")
    if sDOW=0;sDOW=7
    ;mes sDOW

;Wochentag des 01.01. ermitteln
DATE dD0101=F"01/01/{iY}"
if iGLY=1
    dD0101=F"31/12/{iY}"

;out F"DATE:{dD0101}"

str sDN0101.time( dD0101 "%w")

if sDN0101="0"
    sDN0101="7"  ;;Sunday = 7. Day for Calc!
;out F"sDN0101={sDN0101}"

int iKorr0101=1
if val(sDN0101)>4
    iKorr0101=0

;;Bugfix!
if val(sDN0101)=1
    iKorr0101=0

;out iKorr0101

int iWOY

iWOY=val(sY.time(dD "%W"))+iKorr0101
if iGLY=1
    dD=F"31/12/{iY}"
    iWOY=val(sY.time(dD "%W"))+iKorr0101

;out iWOY

int iYC=0
if iWOY>52 ;;Korr KW 53
    iWOY=1
    iYC=1
    if val(sDN0101)=4
        iWOY=53
        iYC=0
    ;ermittle ob Schaltjahr
    if Schaltjahr(02 iY)=29  ;;I Use Function: "GetDaysInMonth" from TheVig and rename it to Funktion: "Schaltjahr")
        if val(sDN0101)=3
            ;mes "Schaltjahr!!!"
            iWOY=53
            iYC=0

;out iWOY

if iWOY=0
    ;mes "GLY=true"
    iGLY=1
    goto _GLY

;;internationales Format:

str sWOY=iWOY
if iWOY<10
    sWOY=F"0{iWOY}"

if iNoString=1
    ret sWOY
    
;out F"{iY+iYC}-W{sWOY}-{sDOW}"

ret F"{iY+iYC}-W{sWOY}-{sDOW}"

end

wfg. M. Schindlauer


Attached Files
.qml   Test_WeekOfYear_Func.qml (Size: 4 KB / Downloads: 7)
.qml   WeekOfYear.qml (Size: 5 KB / Downloads: 6)
.qml   Schaltjahr.qml (Size: 3 KB / Downloads: 6)
.zip   KW-Tray.zip (Size: 270.57 KB / Downloads: 14)


Forum Jump:


Users browsing this thread: 1 Guest(s)