Script for calculating how many days are left til X, which doesn't work 100%...

Share your favorite FastKeys commands
User avatar
Oblomov
Posts: 184
Joined: Mar 4th, ’19, 19:44

Post by Oblomov » Apr 14th, ’21, 21:17

Hi,

So this script:

Code: Select all

TargetDate := "2021" "04" "28"

UpdateCountdown:

SetTimer, UpdateCountdown, % 86400000 - 1000 * DateDiff( A_Year A_MM A_DD, A_Now, "s" )

Gui, Show, W250 H50, % DateDiff( A_Now, TargetDate, "d" ) " Days Left"

Return

Esc::Exitapp



DateDiff( startdate, enddate, units="d" ) {

	If units NOT IN s,sec,second,seconds,m,min,minute,minutes,h,hour,hours,d,day,days

		units := "d" ; days

	Else StringLeft, units, units, 1

	enddate -= startdate, %units%

	return enddate

}
Works, but after you press the Esc, all other commands stop working 😔
I tried experimenting with some returns here and there, but nothing so far...
This solution must be similar to viewtopic.php?f=6&t=1629, but I can't quite figure it out on my own 😔

Kind regards,
...
User avatar
Marko
Posts: 1719
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Apr 15th, ’21, 07:45

You should remove Esc::ExitApp line as it exits the script and replace it with

Code: Select all

Esc::
SetTimer, UpdateCountdown, off
return
In addition, as last time, you need to add Gui, Destroy line just before the Gui, Show line.
User avatar
Oblomov
Posts: 184
Joined: Mar 4th, ’19, 19:44

Post by Oblomov » Apr 15th, ’21, 09:41

Marko wrote: Apr 15th, ’21, 07:45 You should remove Esc::ExitApp line as it exits the script and replace it with

Code: Select all

Esc::
SetTimer, UpdateCountdown, off
return
In addition, as last time, you need to add Gui, Destroy line just before the Gui, Show line.
Thank you for indicating this!

Code: Select all

TargetDate := "2021" "04" "28"

UpdateCountdown:

SetTimer, UpdateCountdown, % 86400000 - 1000 * DateDiff( A_Year A_MM A_DD, A_Now, "s" )

Gui, Destroy
Gui, Show, W250 H50, % DateDiff( A_Now, TargetDate, "d" ) " Days Left"

Return

;Esc::Exitapp
Esc::
SetTimer, UpdateCountdown, off
return



DateDiff( startdate, enddate, units="d" ) {

	If units NOT IN s,sec,second,seconds,m,min,minute,minutes,h,hour,hours,d,day,days

		units := "d" ; days

	Else StringLeft, units, units, 1

	enddate -= startdate, %units%

	return enddate

}
The problem is now solved! The Esc button doesn't quite seem to work now however 🤔
User avatar
Oblomov
Posts: 184
Joined: Mar 4th, ’19, 19:44

Post by Oblomov » Apr 15th, ’21, 10:10

To add on the prior message, the Esc is now no longer working in other applications, so I'm guessing it's getting sort of locked still 😔
Last edited by Oblomov on Apr 15th, ’21, 11:44, edited 1 time in total.
User avatar
Marko
Posts: 1719
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Apr 15th, ’21, 10:17

Then change it to

Code: Select all

~Esc::
SetTimer, UpdateCountdown, off
return
Escape will keep it's native function.
User avatar
Oblomov
Posts: 184
Joined: Mar 4th, ’19, 19:44

Post by Oblomov » Apr 15th, ’21, 11:50

Marko wrote: Apr 15th, ’21, 10:17 Then change it to

Code: Select all

~Esc::
SetTimer, UpdateCountdown, off
return
Escape will keep it's native function.
Awesome! Thank you so much Marko! 🎕

The Esc is now working in other applications. Would of course be nice if one could also, still close the AHK window itself with the Esc; but Alt F4 will have to do I guess 🦝
User avatar
Marko
Posts: 1719
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Apr 15th, ’21, 21:12

Code: Select all

~Esc::
SetTimer, UpdateCountdown, off
Gui, Destroy
return
:D
User avatar
Oblomov
Posts: 184
Joined: Mar 4th, ’19, 19:44

Post by Oblomov » Apr 16th, ’21, 08:47

Marko wrote: Apr 15th, ’21, 21:12

Code: Select all

~Esc::
SetTimer, UpdateCountdown, off
Gui, Destroy
return
:D
Thanks so much! Hm... for some reason it's still not quite closing through Esc however 😔
I'm so sorry Marko 😢
User avatar
Marko
Posts: 1719
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Apr 16th, ’21, 16:43

I checked the script again. If your intention is just to check the number of days left, then the timer is not needed. I also renamed the gui, not to mix up with other guis you may have.

Code: Select all

TargetDate := "2021" "04" "28"

Gui, dd:Destroy
Gui, dd:Font, s12
Gui, dd:Add, Text,, % DateDiff( A_Now, TargetDate, "d" ) " Days Left"
Gui, dd:Show ;, W250 H50, % DateDiff( A_Now, TargetDate, "d" ) " Days Left"
Return

ddGuiClose:
ddGuiEscape:
Gui, dd:Destroy
return

DateDiff( startdate, enddate, units="d" ) {
	If units NOT IN s,sec,second,seconds,m,min,minute,minutes,h,hour,hours,d,day,days
		units := "d" ; days
	Else StringLeft, units, units, 1
	enddate -= startdate, %units%
	return enddate
}
User avatar
Oblomov
Posts: 184
Joined: Mar 4th, ’19, 19:44

Post by Oblomov » Apr 16th, ’21, 17:00

Marko wrote: Apr 16th, ’21, 16:43 I checked the script again. If your intention is just to check the number of days left, then the timer is not needed. I also renamed the gui, not to mix up with other guis you may have.

Code: Select all

TargetDate := "2021" "04" "28"

Gui, dd:Destroy
Gui, dd:Font, s12
Gui, dd:Add, Text,, % DateDiff( A_Now, TargetDate, "d" ) " Days Left"
Gui, dd:Show ;, W250 H50, % DateDiff( A_Now, TargetDate, "d" ) " Days Left"
Return

ddGuiClose:
ddGuiEscape:
Gui, dd:Destroy
return

DateDiff( startdate, enddate, units="d" ) {
	If units NOT IN s,sec,second,seconds,m,min,minute,minutes,h,hour,hours,d,day,days
		units := "d" ; days
	Else StringLeft, units, units, 1
	enddate -= startdate, %units%
	return enddate
}
Oh man, this is so awesome, I can't thank you enough! 🙏

Also didn't know that it was wise to rename the gui, as I thought that it's pretty much 'gone for good', after it's 'destroyed'. I'm assuming it's to not get a conflict with e.g., some currently opened gui 🤔
Post Reply