Calculating date xx days forward

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Lyzi
Posts: 3
Joined: 08 Feb 2023, 04:37

Calculating date xx days forward

Post by Lyzi » 08 Feb 2023, 04:46

Hello!

I've been searching for a script that helps me calculate the correct date 10 days forward from todays date.

Example: Today is the Wednesday February 08, the script will then automatically tell me the date 10 days forward.

I have tried to make a connection to the date on the computer but I have given up.

Is this even possible?

Regards

Lyzi
Posts: 3
Joined: 08 Feb 2023, 04:37

Re: Calculating date xx days forward

Post by Lyzi » 08 Feb 2023, 05:40

I did find this script though, is there any tips of how I could rewrite it to just enter the first date and then press ''calculate'' to predict the date 10 days forward, basically remove ''enter date two''.

Code: Select all


HowLongAutoExec:



Gui, SpanCalc:Add, Text, , Enter Date One:
Gui, SpanCalc:Add, DateTime, vDate1, LongDate
Gui, SpanCalc:Add, Text, , Enter Date Two:
Gui, SpanCalc:Add, DateTime, vDate2, LongDate
Gui, SpanCalc:Add, Button, , Calculate
Gui,SpanCalc:Show, , Calculate How Long

Return

SpanCalcButtonCalculate:
  Gui +OwnDialogs
  Gui, Submit, NoHide
  HowLong(Date1,Date2)
  MsgBox, , TimeSpan Calculation
       , Years %Years%`rMonths %Months%`rDays %Days%  %Past%
Return

HowLong(FromDay,ToDay)
  {
  Global Years,Months,Days,Past
  Past := ""

; Trim any time component from the input dates
    FromDay := SubStr(FromDay,1,8)
    ToDay := SubStr(ToDay,1,8)

;   For proper date order before calculation
/*
   If (ToDay <= FromDay)
   {
     Years := 0, Months := 0, Days := 0
     MsgBox, Start date after end date!
     Return
   }
*/

   If (ToDay = FromDay)
   {
     Years := 0, Months := 0, Days := 0
     Return
   }

   If (ToDay < FromDay)
   {
     Temp := Today
     ToDay := FromDay
     FromDay := Temp
     Past := "Ago"
   }

/*
The function no longer requires this Leap Year test since when needed
AutoHotkey now calculates the length of the previous month in the target
year. I've left this commented-out code in here for people looking for a different
approach to  identifying a Leap Year but it does not run in this script.

;   Test for Stop date Leap Year. Invalid date calc returns blank.

  Feb29 := SubStr(ToDay,1,4) . "0229"
  Feb29 += 1, days      ; Test for Leap Year

  If (Feb29 != "")
      Feb := 29
  Else
      Feb := 28

*/

;   Calculate years

    Years  := % SubStr(ToDay,5,4) - SubStr(FromDay,5,4) < 0
           ? SubStr(ToDay,1,4)-SubStr(FromDay,1,4)-1
            : SubStr(ToDay,1,4)-SubStr(FromDay,1,4)

;   Remove years from the calculation

     FromYears := Substr(FromDay,1,4)+years . SubStr(FromDay,5,4)


/*
Calculate the number of months between the Start date (Years removed)
and the Stop date. If the day of the month in the Start date is greater than
the day of the month in the Stop date, then add 11 or 12 months to the
calculation depending upon the comparison between month days.
*/

    If (Substr(FromYears,5,2) <= Substr(ToDay,5,2))  and (Substr(FromYears,7,2) <= Substr(ToDay,7,2))
       Months := Substr(ToDay,5,2) - Substr(FromYears,5,2)
    Else If (Substr(FromYears,5,2) < Substr(ToDay,5,2)) and (Substr(FromYears,7,2) > Substr(ToDay,7,2))
       Months := Substr(ToDay,5,2) - Substr(FromYears,5,2) - 1
    Else If (Substr(FromYears,5,2) > Substr(ToDay,5,2)) and (Substr(FromYears,7,2) <= Substr(ToDay,7,2))
       Months := Substr(ToDay,5,2) - Substr(FromYears,5,2) +12
    Else If (Substr(FromYears,5,2) >= Substr(ToDay,5,2)) and (Substr(FromYears,7,2) > Substr(ToDay,7,2))
       Months := Substr(ToDay,5,2) - Substr(FromYears,5,2) +11

;   If the start day of the month is less than the stop day of the month use the same month
;   Otherwise use the previous month, (If Jan "01" use Dec "12")

     If (Substr(FromYears,7,2) <= Substr(ToDay,7,2))
         FromMonth := Substr(ToDay,1,4) . SubStr(ToDay,5,2) . Substr(FromDay,7,2)
     Else If Substr(ToDay,5,2) = "01"
         FromMonth := Substr(ToDay,1,4)-1 . "12" . Substr(FromDay,7,2)
     Else
        FromMonth := Substr(ToDay,1,4) . Format("{:02}", SubStr(ToDay,5,2)-1) . Substr(FromDay,7,2)

;   FromMonth := Substr(ToDay,1,4) . Substr("0" . SubStr(ToDay,5,2)-1,-1) . Substr(FromDay,7,2)
;   "The Format("{:02}",  SubStr(ToDay,5,2)-1)" function replaces the original "Substr("0" . SubStr(ToDay,5,2)-1,-1)"
;   function found in the line of code above. Both serve the same purpose, although the original function
;   uses sleight of hand to pad single digit months with a zero (0).

;   Adjust for previous months with less days than target day

    Date1 := Substr(FromMonth,1,6) . "01"
    Date2 := Substr(ToDay,1,6) . "01"
    Date2 -= Date1, Days
    If (Date2 < Substr(FromDay,7,2)) and (Date2 != 0)
        FromMonth := Substr(FromMonth,1,6) . Date2

/*
;   Adjust for months with less than 31 days [Removed and replace by code above]

      If (Substr(FromMonth,5,2) = "02") and (Substr(FromDay,7,2) > "28")
              FromMonth := Substr(FromMonth,1,6) . Feb
       If (Substr(FromMonth,5,2) = "04") and (Substr(FromDay,7,2) > "30")
              FromMonth := Substr(FromMonth,1,6) . "30"
       If (Substr(FromMonth,5,2) = "06") and (Substr(FromDay,7,2) > "30")
              FromMonth := Substr(FromMonth,1,6) . "30"
       If (Substr(FromMonth,5,2) = "09") and (Substr(FromDay,7,2) > "30")
              FromMonth := Substr(FromMonth,1,6) . "30"
       If (Substr(FromMonth,5,2) = "11") and (Substr(FromDay,7,2) > "30")
              FromMonth := Substr(FromMonth,1,6) . "30"
 */

; Calculate remaining days. This operation (EnvSub) changes the value of the original
; ToDay variable, but, since this completes the function, we don't need to save ToDay
; in its original form.

     Days := ToDay

     Days -= %FromMonth% , d

/*
       ToDay -= %FromMonth% , d
       Days := ToDay
*/


  }

; Enable mousewheel in AutoHotkey GUIs

#If MouseIsOver("ahk_class AutoHotkeyGUI")
   WheelUp::Send {Up}
   WheelDown::Send {Down}
#If

MouseIsOver(WinTitle)
{
   MouseGetPos,,, Win
   Return WinExist(WinTitle . " ahk_id " . Win)
}

^!z::
  GuiControl, SpanCalc: , SysDateTimePick321, 20000101
Return

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: Calculating date xx days forward

Post by RussF » 08 Feb 2023, 07:02

This works in AHK 1.1. I'm still getting acquainted with v 2.0, so perhaps someone else could help there.

Code: Select all

FutureDate := A_Now
FutureDate += 10, Days
FormatTime, FuturDateFormatted, %FutureDate%, Longdate
MsgBox, % "The date in 10 days will be: `n`n" . FuturDateFormatted
Russ

Rohwedder
Posts: 7625
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculating date xx days forward

Post by Rohwedder » 08 Feb 2023, 08:36

Hallo,
try:

Code: Select all

#Requires AutoHotkey v2.0-
FutureDate := A_Now
FutureDate := DateAdd(FutureDate, 10, "Days")
FuturDateFormatted := FormatTime(FutureDate, "Longdate")
MsgBox "The date in 10 days will be: `n`n" FuturDateFormatted
or:

Code: Select all

#Requires AutoHotkey v2.0-
MsgBox "The date in 10 days will be: `n`n"
. FormatTime(DateAdd(A_Now, 10, "Days"), "Longdate")

Lyzi
Posts: 3
Joined: 08 Feb 2023, 04:37

Re: Calculating date xx days forward

Post by Lyzi » 09 Feb 2023, 04:17

Rohwedder wrote:
08 Feb 2023, 08:36
Hallo,
try:

Code: Select all

#Requires AutoHotkey v2.0-
FutureDate := A_Now
FutureDate := DateAdd(FutureDate, 10, "Days")
FuturDateFormatted := FormatTime(FutureDate, "Longdate")
MsgBox "The date in 10 days will be: `n`n" FuturDateFormatted
or:

Code: Select all

#Requires AutoHotkey v2.0-
MsgBox "The date in 10 days will be: `n`n"
. FormatTime(DateAdd(A_Now, 10, "Days"), "Longdate")

I have came up with something like this:

Code: Select all

Gui, Add, Text, vInfo, Ange dagens datum:
Gui, Add, DateTime, vDate,
Gui, Add, Button, x66 w100 , Beräkna
Gui, Show
Return

ButtonBeräkna:

Gui, Submit

var1 = %Date%
var2 = %Date%
var3 = %Date%
var1 += 11, days
FormatTime, out1, %var1%, dddd MMMM dd yyyy
IfInString, out1, Saturday
{	
	Var2 += 29, days
	FormatTime, out2, %var2%, dddd MMMM dd yyyy
	MsgBox, %out2%.  ; 
	ExitApp
}
Else IfInString, out1, Sunday
{
	Var3 += 28, days
	FormatTime, out3, %var3%, dddd MMMM dd yyyy
	MsgBox, %out3%.  ; 
	ExitApp

}
Else
{
	MsgBox, %out1%.  ; 
	ExitApp
}
Is there any simple way to make the script jump forward to monday if the ''last day'' is on saturday or sunday? I have problem to get it work in a good way.

Post Reply

Return to “Ask for Help (v1)”