Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Time() - Count Days, hours, minutes, seconds between dates


  • Please log in to reply
3 replies to this topic
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Using Time() you can count time.

There are surely some bugs that I did not find yet, please report if you find one.

to and from can be in following format (from will be A_Now if empty):
- 20090101...
- 20090101000000
- Apr-04-2009 or Apr-04 for current year
- 01-01-2009 or 01-01 for current year
Units (output units) can be d=days, h=hours, m=minutes, s=seconds
params can be W,D,H,M,S
- W = WeeksDay to include, e.g. W2-6 (mon-fri) or W1.7 (exclude weekends)
- D = Day of Month to include, e.g. e D1-5 (to exclude D1.2.3)
- B = Bank holiday to exclude, e.g. B0101.3112
- H = Hours to include, e.g. H8-17 (to exclude use H0.1.2...)
- M = Minutes to include, e.g. M0-30 (to exclude use M0.1.2...)
- S = Seconds to include, e.g. S0-30 (to exclude use S0.1.2...)

See examples in code. Hope you enjoy ;)

#NoEnv
SetBatchLines,-1
MsgBox % "Count days from 12 May until 15 May`n"
		. Time("May-15","12.May","d")

MsgBox % "Working hours from 01.05.2009 00:00:00   to   10.05.09 09:30:00`n"
		. "Count only working hours from 09 to 17 and consider weekends and 01.May bank holiday`n"
		. Time("20090504093000","20090501000000","h","W1.7 H9-17 B0105")

MsgBox % "Days to work till end of 2009`n" . Time("01.01.2010","","d","W1.7")

MsgBox % "Hours to work till end of 2009 from 09 - 17`n" . Time("01.01.2010","","h","W1.7 H9-17")

ExitApp
Time(to,from="",units="d",params=""){
	static _:="0000000000",s:=1,m:=60,h:=3600,d:=86400
				,Jan:="01",Feb:="02",Mar:="03",Apr:="04",May:="05",Jun:="06",Jul:="07",Aug:="08",Sep:="09",Okt:=10,Nov:=11,Dec:=12
	r:=0
	units:=units ? %units% : 8640
	If (InStr(to,"/") or InStr(to,"-") or InStr(to,".")){
		Loop,Parse,to,/-.,%A_Space%
			_%A_Index%:=RegExMatch(A_LoopField,"\d+") ? A_LoopField : %A_LoopField%
			,_%A_Index%:=(StrLen(_%A_Index%)=1 ? "0" : "") . _%A_Index%
		to:=SubStr(A_Now,1,8-StrLen(_1 . _2 . _3)) . _3 . (RegExMatch(SubStr(to,1,1),"\d") ? (_2 . _1) : (_1 . _2))
		_1:="",_2:="",_3:=""
	}
	If (from and InStr(from,"/") or InStr(from,"-") or InStr(from,".")){
		Loop,Parse,from,/-.,%A_Space%
			_%A_Index%:=RegExMatch(A_LoopField,"\d+") ? A_LoopField : %A_LoopField%
			,_%A_Index%:=(StrLen(_%A_Index%)=1 ? "0" : "") . _%A_Index%
		from:=SubStr(A_Now,1,8-StrLen(_1 . _2 . _3)) . _3 . (RegExMatch(SubStr(from,1,1),"\d") ? (_2 . _1) : (_1 . _2))
	}
   count:=StrLen(to)<9 ? "days" : StrLen(to)<11 ? "hours" : StrLen(to)<13 ? "minutes" : "seconds"
	to.=SubStr(_,1,14-StrLen(to)),(from ? from.=SubStr(_,1,14-StrLen(from)))
	Loop,Parse,params,%A_Space%
		If (unit:=SubStr(A_LoopField,1,1))
			 %unit%1:=InStr(A_LoopField,"-") ? SubStr(A_LoopField,2,InStr(A_LoopField,"-")-2) : ""
			,%unit%2:=SubStr(A_LoopField,InStr(A_LoopField,"-") ? (InStr(A_LoopField,"-")+1) : 2)
	count:=!params ? count : "seconds"
	add:=!params ? 1 : (S2="" ? (M2="" ? (H2="" ? ((D2="" and B2="" and W="") ? d : h) : m) : s) : s)
	While % (from<to){
		FormatTime,year,%from%,YYYY
		FormatTime,month,%from%,MM
		FormatTime,day,%from%,dd
		FormatTime,hour,%from%,H
		FormatTime,minute,%from%,m
		FormatTime,second,%from%,s
		FormatTime,WDay,%from%,WDay
		EnvAdd,from,%add%,%count%
		If (W1 or W2){
			If (W1=""){
				If (W2=WDay or InStr(W2,"." . WDay) or InStr(W2,WDay . ".")){
					Continue=1
				}
			} else If WDay not Between %W1% and %W2%
				Continue=1
			;else if (Wday=W2)
			;	Continue=1
			If (Continue){
				tempvar:=SubStr(from,1,8)
				EnvAdd,tempvar,1,days
				EnvSub,tempvar,%from%,seconds
				EnvAdd,from,%tempvar% ,seconds
				Continue=
				continue
			}
		}
		If (D1 or D2 or B2){
			If (D1=""){
				If (D2=day or B2=(day . month) or InStr(B2,"." . day . month) or InStr(B2,day . month . ".") or InStr(D2,"." . day) or InStr(D2,day . ".")){
					Continue=1
				}
			} else If day not Between %D1% and %D2%
				Continue=1
			;else if (day=D2)
			;	Continue=1
			If (Continue){
				tempvar:=SubStr(from,1,8)
				EnvAdd,tempvar,1,days
				EnvSub,tempvar,%from%,seconds
				EnvAdd,from,%tempvar% ,seconds
				Continue=
				continue
			}
		}
		If (H1 or H2){
			If (H1=""){
				If (H2=hour or InStr(H2,hour . ".") or InStr(H2,"." hour)){
					Continue=1
				}
			} else If hour not Between %H1% and %H2%
				continue=1
			;else if (hour=H2)
			;	continue=1
			If (continue){
				tempvar:=SubStr(from,1,10)
				EnvAdd,tempvar,1,hours
				EnvSub,tempvar,%from%,seconds
				EnvAdd,from,%tempvar% ,seconds
				continue=
				continue
			}
		}
		If (M1 or M2){
			If (M1=""){
				If (M2=minute or InStr(M2,minute . ".") or InStr(M2,"." minute)){
					Continue=1
				}
			} else If minute not Between %M1% and %M2%
				continue=1
			;else if (minute=M2)
			;	continue=1
			If (continue){
				tempvar:=SubStr(from,1,12)
				EnvAdd,tempvar,1,minutes
				EnvSub,tempvar,%from%,seconds
				EnvAdd,from,%tempvar% ,seconds
				continue=
				continue
			}
		}
		If (S1 or S2){
			If (S1=""){
				If (S2=second or InStr(S2,second . ".") or InStr(S2,"." second)){
					Continue
				}
			} else if (second!=S2)
				If second not Between %S1% and %S2%
					continue
		}
		r+=add
	}
	tempvar:=SubStr(count,1,1)
	tempvar:=%tempvar%
	Return (r*tempvar)/units
}


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
Great function thanks for posting it
Never lose.
WIN or LEARN.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Here some more examples.

MsgBox % Time(SubStr(A_Now,1,4)) ;days left until next year
MsgBox % Time(SubStr(A_Now,1,4) "11","","h","H8-16") ;hours to work until November (working hours 08:00-17:00)

t:=A_Now
t+=1,days
MsgBox % SubStr(t,1,8) "`n" Time(SubStr(t,1,8) "0000","","h") ;hours left till midnight

MsgBox % "Days left till 2011:`n" Time("2011000000") "`n`nSame but consider 01 Nov, 25 + 26 + 31 Dez as bank holidays:`n" Time("2011000000","","d","B0111.2512.2612.3112")
EDIT:
Thanks God happy.png
I changed it not to use ErrorLevel variable for calculation, no idea why I used it :roll:

Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

Here some more examples.
 

MsgBox % Time(SubStr(A_Now,1,4)) ;days left until next year
MsgBox % Time(SubStr(A_Now,1,4) "11","","h","H8-16") ;hours to work until November (working hours 08:00-17:00)

t:=A_Now
t+=1,days
MsgBox % SubStr(t,1,8) "`n" Time(SubStr(t,1,8) "0000","","h")) ;hours left till midnight

MsgBox % "Days left till 2011:`n" Time("2011000000") "`n`nSame but consider 01 Nov, 25 + 26 + 31 Dez as bank holidays:`n" Time("2011000000","","d","B0111.2512.2612.3112") 
EDIT:
Thanks God happy.png
I changed it not to use ErrorLevel variable for calculation, no idea why I used it :roll:

An extra parenthesis was removed: MsgBox % SubStr(t,1,8) "`n" Time(SubStr(t,1,8) "0000","","h") ; hours left  happy.png

; http://www.autohotkey.com/board/topic/42668-time-count-days-hours-minutes-seconds-between-dates/

#include C:\...\Time.ahk

MsgBox % Time(SubStr(A_Now,1,4)) ; days left until next year
MsgBox % Time(SubStr(A_Now,1,4) "11","","h","H8-16") ; hours to work until November (working hours 08:00-17:00)

t:=A_Now
t+=1,days

MsgBox % SubStr(t,1,8) "`n" Time(SubStr(t,1,8) "0000","","h") ; hours left till midnight

MsgBox % "Days left till 2011:`n" Time("2011000000") "`n`nSame but consider 01 Nov, 25 + 26 + 31 Days as bank holidays:`n" Time("2011000000","","d","B0111.2512.2612.3112")