dates: date to week number

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

dates: date to week number

11 Mar 2018, 22:32

The question of date to week number, was prompted by this thread:
Task log - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=45410

AutoHotkey has one way to do date to week number using YWeek:
FormatTime
https://autohotkey.com/docs/commands/FormatTime.htm

YWeek is a good system, although one disadvantage is that it may state that the 1 Jan this year, is part of the last week of last year.

For me the most intuitive system would be: for a year that starts on a Friday.
Week 1 of year: Fri Sat Sun. [Fri 1 Jan]
Week 2 of year: MTWTFSS.
Week 3 of year: MTWTFSS. Etc.

I might make use of both systems for record-keeping. But I would use the more 'intuitive' system, if someone simply asked me, what week is it.

I've tried to write a function to do this, where you can specify any day you like as the 'first day of the week', it assumes Monday. I've also written a function that tries to do date to week number, that only allows Monday as the first day of the week, and which is based on YWeek. (One reason for two functions, is to use the simpler function to check that the more complex function is working correctly.)

Do notify of any issues, or any better alternative ideas (which there may well be).
Btw you might find this quite(/very) fiddly to get your head around, if you try to write a function from scratch, without looking at my attempt.

Code: Select all

q:: ;list dates with week numbers and weekdays
vDate := 20000101
vOutput := ""
Loop, % 366*20
{
	vOutput .= SubStr(vDate, 1, 8) " " JEE_DateGetWeek(vDate) " " FormatTime(vDate, "ddd") "`r`n"
	vDate := DateAdd(vDate, 1, "D")
}
Clipboard := vOutput
MsgBox, % "done"
return

;w:: ;test the functions
vDate := 2000
Loop, % 366*20
{
	vWeek1 := JEE_DateGetWeek(vDate)
	vWeek2 := JEE_DateGetWeekAlt(vDate)
	if !(vWeek1 = vWeek2)
		MsgBox, % SubStr(vDate, 1, 8) " " vWeek1 " " vWeek2 " " FormatTime(vDate, "ddd") "`r`n"
	vDate := DateAdd(vDate, 1, "D")
}
MsgBox, % "done"
return

e:: ;test the functions
oXl := ComObjCreate("Excel.Application")

vDate := 2000
vDateXl := 36526 ;2000-01-01
Loop, % 366*20
{
	;vWeek1 := JEE_DateGetWeek(vDate) ;week starts on Mon
	;vWeek2 := Round(oXl.WeekNum(vDateXl, 2)) ;week starts on Mon
	vWeek1 := JEE_DateGetWeek(vDate, 1) ;week starts on Sun
	vWeek2 := Round(oXl.WeekNum(vDateXl)) ;week starts on Sun
	;MsgBox, % vWeek1 " " vWeek2
	if !(vWeek1 = vWeek2)
		MsgBox, % SubStr(vDate, 1, 8) " " vWeek1 " " vWeek2 " " FormatTime(vDate, "ddd") "`r`n"
	vDate := DateAdd(vDate, 1, "D")
	vDateXl++
}
MsgBox, % "done"

oXl.Quit()
oXl := ""
return

'==================================================

;e.g. vWeek := JEE_DateGetWeek(A_Now)

;e.g. if the first day of the week is defined as Monday,
;'week 1' is from 1 Jan to the first Sunday of the year

;1 Jan is always in 'week 1'
;vWeekStart: 1 (Sun), 7 (Sat)
;SMTWTFS
;1234567

;e.g. vWeek := JEE_DateGetWeek(A_Now)

;e.g. if the first day of the week is defined as Monday,
;'week 1' is from 1 Jan to the first Sunday of the year

;1 Jan is always in 'week 1'
;vWeekStart: 1 (Sun), 7 (Sat)
;SMTWTFS
;1234567

JEE_DateGetWeek(vDate:="", vWeekStart:=2)
{
	if (vDate = "")
		vDate := A_Now
	vWDay := FormatTime(SubStr(vDate, 1, 4), "WDay")
	vDays := DateDiff(vDate, SubStr(vDate, 1, 4), "D") + Mod(7+vWDay-vWeekStart, 7)
	;find the Monday in the period 26 Dec to 1 Jan,
	;count the weeks between the requested date and that Monday
	return Format("{:02}", 1 + vDays//7)
}

'==================================================

JEE_DateGetWeekAlt2(vDate:="", vWeekStart:=2)
{
	vWeekEnd := Mod(vWeekStart+7-1, 7)
	if (vDate = "")
		vDate := A_Now
	vDays := DateDiff(vDate, SubStr(vDate, 1, 4), "D")
	vWeeks := vDays//7
	vDaysX := Mod(vDays, 7)
	vWDay := FormatTime(vDate, "WDay")

	;where Monday is the first day of the week,
	;within the first 7 days of the year,
	;if the day is after a Sunday, it's in the second week of the year
	if (vDaysX = 0)
		return Format("{:02}", 1 + vWeeks)
	vRange1 := vWDay-vDaysX
	vRange2 := vWDay-1
	if ((vWeekEnd >= vRange1) && (vWeekEnd <= vRange2))
	|| ((vWeekEnd-7 >= vRange1) && (vWeekEnd-7 <= vRange2))
		return Format("{:02}", 2 + vWeeks)
	else
		return Format("{:02}", 1 + vWeeks)
}

;==================================================

;e.g. vWeek := JEE_DateGetWeekAlt(A_Now)

;uses YWeek where Monday is the first day of week

JEE_DateGetWeekAlt(vDate:="")
{
	if (vDate = "")
		vDate := A_Now
	vYWeek := FormatTime(vDate, "YWeek")
	if !(SubStr(vDate, 1, 4) = SubStr(vYWeek, 1, 4))
		if (SubStr(vDate, 5, 2) = 12)
			return Format("{:02}", %A_ThisFunc%(DateAdd(vDate, -7, "D"))+1)
		else
			return Format("{:02}", 1)
	vYWeekX := FormatTime(SubStr(vDate, 1, 4), "YWeek")
	vWeek := SubStr(vYWeek, 5)
	if (SubStr(vDate, 1, 4) = SubStr(vYWeekX, 1, 4))
		return Format("{:02}", vWeek)
	else
		return Format("{:02}", vWeek+1)
}

;==================================================

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

DateAdd(DateTime, Time, TimeUnits)
{
    EnvAdd DateTime, %Time%, %TimeUnits%
    return DateTime
}
DateDiff(DateTime1, DateTime2, TimeUnits)
{
    EnvSub DateTime1, %DateTime2%, %TimeUnits%
    return DateTime1
}
FormatTime(YYYYMMDDHH24MISS:="", Format:="")
{
    local OutputVar
    FormatTime OutputVar, %YYYYMMDDHH24MISS%, %Format%
    return OutputVar
}

;==================================================
[EDIT:] Added example scripts. Corrected JEE_DateGetWeekAlt.
[EDIT:] Added Excel tests. Functions now always return two-digit numbers. Replaced JEE_DateGetWeek with a far simpler algorithm, original function is now called JEE_DateGetWeekAlt2.
Last edited by jeeswg on 12 Mar 2018, 12:37, edited 4 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: dates: date to week number

12 Mar 2018, 00:25

AFAIK the way 'yweek' works is based on the ISO-standard. Here's the thread that brought yweek to the light of day: https://autohotkey.com/board/topic/935- ... r-a-yweek/

I was a bit shocked that I was kinda involved in the conversation about it :mrgreen:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: dates: date to week number

12 Mar 2018, 10:04

- Wow BoBo, you're the one responsible for this A_ variable, back in 2004. You were an early adopter of AutoHotkey!
- Your link again, plus two other links mentioned in the thread:
Current week of the year (A_YWeek) - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/935- ... r-a-yweek/
International standard date and time notation
https://www.cl.cam.ac.uk/~mgk25/iso-time.html
Week Numbers In Excel
http://www.cpearson.com/excel/WeekNumbers.aspx
- Unfortunately, AFAICS, in the 'Week Numbers In Excel' link, they avoided writing a function for what I think is the most natural definition of the week, the one I used in my functions above.
- Btw prompted by the AHK link, I've changed my functions to always output a two-digit number, which is usually more convenient, and which can be forced to be one/two-digit by using +0.
- Btw it turns out that by my definition of the week, you get 54 'weeks' in 2012, because it's a leap year that starts on a Sunday.
20111231 53 Sat
20120101 01 Sun <
20120102 02 Mon
20120108 02 Sun
20121224 53 Mon
20121230 53 Sun
20121231 54 Mon <
20130101 01 Tue
- A 365-day year begins and ends on the same weekday: 365 = 350+14+1 = (52*7)+1.
Last edited by jeeswg on 12 Mar 2018, 12:36, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: dates: date to week number

12 Mar 2018, 10:11

@jeeswg
I've already met Chris Mallett at the AutoIT-forum at the time he started to discuss if hotkeys should be implemented in AutoIT! So, yep I'm an AHK-fossil :mrgreen: :shh:
* I've used MacroScheduler (a Shareware) before the legacy AutoIT and ported a billing project completely to AutoHotkey for the reason that it was/is free of charge ;)

PS: Interesting statistics that I've found a minute ago ...
http://www.shouldiremoveit.com/AutoHotk ... ogram.aspx :o :thumbup:
http://www.shouldiremoveit.com/AutoHotk ... ogram.aspx
http://www.shouldiremoveit.com/AutoHotk ... ogram.aspx
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: dates: date to week number

12 Mar 2018, 10:29

http://www.shouldiremoveit.com/AutoHotk ... ogram.aspx
MOST USED OS: 99 % Windows 8
Which countries install it? 100 % USA
Interesting: yes
Makes sense: no :D :shifty:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: dates: date to week number

12 Mar 2018, 12:49

- By going through the 'Week Numbers In Excel' link, one example prompted me to think that I could find the Monday in the period 26 Dec to 1 Jan, and treat that as the first day of the year, and then simply count the number of weeks between a date and that 'first day of the year'. I now have a far simpler algorithm in the function in the original post.
- Excel's WEEKNUM function appears to be based on the same principles as my JEE_DateGetWeek function, and gives the same results when you specify Sunday/Monday as the first day of the week in both of them.
- Excel now has an ISOWEEKNUM function, although it's in newer versions, so I couldn't test it. It probably gives the same results as YWeek.
- Some further links:
ISO week date - Wikipedia
https://en.wikipedia.org/wiki/ISO_week_date
Excel Isoweeknum Function
http://www.excelfunctions.net/excel-iso ... ction.html
Excel WEEKNUM Function
http://www.excelfunctions.net/Weeknum-Function.html

- @Bobo: Interesting, when did you first discover AutoIt, and did many others join you in moving to AutoHotkey, when AutoHotkey began? Also, how did AutoIt work without hotkeys!? (I love a bit of AutoHistory.) Great stats cheers.
- @gregster: Yes, that must be the Region 1 version of AutoHotkey (AmeriHotkey or Amerikey).
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: dates: date to week number

12 Mar 2018, 14:49

@jeeswg
AHK went quite popular right after it has split from AutoIT - well, it was 100% AU + hotkeys, so no big deal to convert to it - but from my opinon mainly for the reason that it kept its beginner friendly legacy syntax, and the newly introduced AHK forum has had a much more friendly athmosphere. I found it really interesting to be one of its early adopters, and at that time have requested the first "foreign language"-section AKA the German Sektion 8-) Germany's main computer magazine (c't) has discovered AHK and published several articles about it (+ created ac'tiveAid a popular tool. If remember it correctly our fellow forum member Tekl was its developer).

https://www.heise.de/download/product/activaid-24593
https://www.heise.de/download/specials/ ... te-3149034

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AHK_user and 233 guests