Time until...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Time until...

16 May 2014, 10:09

Is there a way to display the time left, in hours/minutes/seconds, until a specific future time within the same day?

For example, "There are 6 hours, 32 minutes, and 53 seconds left before your work shift is over"

I keep getting confused when attempting to subtract the current & future minutes since the minute starts over after 60 seconds instead of 100 which would be easier.
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Time until...

16 May 2014, 11:15

I have the following, but I don't know how to put it in a "Hour:Minute:Second" until format.

Code: Select all

Now := A_Now
Future := "20140516163000"

EnvSub, Hours, %Future%, Hours
EnvSub, Minutes, %Future%, Minutes
EnvSub, Seconds, %Future%, Seconds

MsgBox, %Hours% Hours`n%Minutes% Minutes`n%Seconds% Seconds
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Time until...

16 May 2014, 14:36

Try this:

Code: Select all

FutureTime := "17:30:00" ; Each must be two digits and use 24 hour time 
FutureTime := A_YYYY A_MM A_DD RegExReplace(FutureTime, "\D")

EnvSub, Seconds, FutureTime, seconds
Seconds := - Seconds
H := Floor(Seconds / 3600), Seconds := Mod(Seconds, 3600)
M := Floor(Seconds / 60), Seconds :=  Mod(Seconds, 60)
S := Seconds

MsgBox % H (H=1 ? " hour, " :  " hours, ")  M (M=1 ? " minute, " :  " minutes, ") (S ? S : 0) (S=1 ? " second " :  " seconds ") "`n`nTill Party Time"

Esc::ExitApp
A dynamic Gui for amusement:

Code: Select all

FutureTime := "17:30:00" ; Each must be two digits and use 24 hour time 
FutureTime := A_YYYY A_MM A_DD RegExReplace(FutureTime, "\D")

Gui, Add, Text, w175 vTime 
Gui, Add, Text,, Till Party Time
Gui, Show, AutoSize

SetTimer, Update, 1000

Update:
	Seconds =
	EnvSub, Seconds, FutureTime, seconds
	Seconds := - Seconds
	H := Floor(Seconds / 3600), Seconds := Mod(Seconds, 3600)
	M := Floor(Seconds / 60), Seconds :=  Mod(Seconds, 60)
	S := Seconds
	GuiControl, Text, Time, % H (H=1 ? " hour, " :  " hours, ")  M (M=1 ? " minute, " :  " minutes, ") (S ? S : 0) (S=1 ? " second " :  " seconds ")
return

Esc::ExitApp
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Time until...

16 May 2014, 15:29

This seems to work:

Code: Select all

Future := 20140516163000

EnvSub, Hours, %Future%, Hours
EnvSub, Minutes, %Future%, Minutes
EnvSub, Seconds, %Future%, Seconds
Seconds -= Minutes * 60
Minutes -= Hours * 60
Hours *= -1, Minutes *= -1, Seconds *= -1

MsgBox, %Hours% Hours`n%Minutes% Minutes`n%Seconds% Seconds
(Similar to FG's)
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Time until...

16 May 2014, 16:50

function from FormatTime

Code: Select all

Future := 20140516163000
EnvSub, Future,, Seconds
MsgBox % FormatSeconds(Future)
return

FormatSeconds(NumberOfSeconds){  ; Convert the specified number of seconds to hh:mm:ss format.
	time := 19990101  ; *Midnight* of an arbitrary date.
	time += %NumberOfSeconds%, seconds
	FormatTime, mmss, %time%, mm:ss
	return NumberOfSeconds//3600 ":" mmss
}
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Time until...

16 May 2014, 21:59

thanks, it hit the spot!
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Time until...

18 May 2014, 04:25

Code: Select all

#NoEnv
SetBatchLines, -1
MsgBox, % TimeUntil(16, 30)
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
TimeUntil(Hour, Min := 0, Sec := 0) { ; Hour must be in 24 hour format
   TimeInterval := A_YYYY . A_MM . A_DD . SubStr("0" . Hour, -1) . SubStr("0" . Min, -1) . SubStr("0" . Sec, -1)
   TimeInterval -= %A_Now%, S
   If (TimeInterval > 0) {
      ; msdn.microsoft.com/en-us/library/bb759980(v=vs.85).aspx
      VarSetCapacity(Result, 128, 0)
      DllCall("Shlwapi.dll\StrFromTimeInterval", "Str", Result, "UInt", 64, "UInt", TimeInterval * 1000, "Int", 30)
      Return Result
   }
}
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Time until...

18 May 2014, 04:49

tested! yields positive numbers... and thanks for supporting my script habit! :lol:
TBat55

Re: Time until...

04 Aug 2014, 14:08

I assigned this code to the F4 function key ( added F4:: after #NoEnv ) and have a shortcut in the Windows Startup folder.

It works fine, but only once. Closing the message box and pressing F4 again does nothing.
If my browser is open it works, but pressing F4 a second time drops down the browsing history (the normal F4 function).

How do I make F4 run the script every time?
Guest

Re: Time until...

04 Aug 2014, 14:13

That is because of the ExitApp, replace it with Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Hugh Jars, oktavimark, peter_ahk, Pianist and 346 guests