add 1 year to the current date

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

add 1 year to the current date

09 May 2021, 15:57

Dear members of the forum.

I have a scenario where I have confirmed today's date over the Internet.
I want to get through today's date 1 year with the help of Msjbox.
I want the year-round to work stably. How can it be coded?

Code: Select all

WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttp.Open("GET", "http://worldtimeapi.org/api/ip", false) ; Get date from the internet
try
{
	WinHttp.Send() ; try to execute
}
catch ; if there is an error, this block will execute
{
	;~ msgbox, Could not connect to the internet, the program will exit.
	;~ ExitApp
}
data := WinHttp.ResponseText ; get the info
;~ MsgBox, % data
Pos := InStr(data, "datetime")
Pos += 11

CurrentDate := StrReplace(SubStr(data, Pos, 10),"-", "") ; extract the date
FormatTime, CurrentDate,, dd.MM.yyyy
MsgBox, % CurrentDate

MsgBox, Next 1 year Date = ? ( for example current date = 09.05.2021  newdate result : 09.05.2022  )
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: add 1 year to the current date

09 May 2021, 16:11

Code: Select all

Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Working
WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttp.Open("GET", "http://worldtimeapi.org/api/ip", false) ; Get date from the internet
Try WinHttp.Send()
Catch
{
	Gui, Destroy
	MsgBox, 48, Error, An error occurred while connecting to the Internet. Aborting.
	ExitApp
}
RegExMatch(WinHttp.ResponseText, """datetime"":""(.+?)(-.+?)T", datePart)
Gui, Destroy
MsgBox, 64, Next year, % nextYear := datePart1 + 1 datePart2
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: add 1 year to the current date

09 May 2021, 16:27

mikeyww wrote:
09 May 2021, 16:11

Code: Select all

Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Working
WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttp.Open("GET", "http://worldtimeapi.org/api/ip", false) ; Get date from the internet
Try WinHttp.Send()
Catch
{
	Gui, Destroy
	MsgBox, 48, Error, An error occurred while connecting to the Internet. Aborting.
	ExitApp
}
RegExMatch(WinHttp.ResponseText, """datetime"":""(.+?)(-.+?)T", datePart)
Gui, Destroy
MsgBox, 64, Next year, % nextYear := datePart1 + 1 datePart2

I want the format this way, (10.05.2022) dd.MM.yyyy
how can I do?
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: add 1 year to the current date

09 May 2021, 16:32

Code: Select all

RegExMatch(WinHttp.ResponseText, """datetime"":""(.+?)-(.+?)-(.+?)T", datePart)
Gui, Destroy
MsgBox, 64, Next year, % nextYear := datePart3 "." datePart2 "." datePart1 + 1
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: add 1 year to the current date

09 May 2021, 18:01

What if it is 2/29/20?
14.3 & 1.3.7
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: add 1 year to the current date

09 May 2021, 18:08

lol. Ok then, what if it is 02/29/24? It will return 02/29/25 ... which is not a valid date.
14.3 & 1.3.7
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: add 1 year to the current date

09 May 2021, 18:10

That will be up to OP to define. 365 days....?

:)
Last edited by mikeyww on 09 May 2021, 18:11, edited 1 time in total.
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: add 1 year to the current date

09 May 2021, 18:11

Nope. 2025 is not a leap year. ;)
14.3 & 1.3.7
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: add 1 year to the current date

09 May 2021, 18:15

I would add something like:

Code: Select all

dt := (datePart1 + 1) . datePart2 . datePart3
if dt is time
	MsgBox, 64, Next year, % nextYear := datePart3 "." datePart2 "." datePart1 + 1
else
	MsgBox, 64, Next year, % nextYear := "01.03." datePart1 + 1
Last edited by flyingDman on 09 May 2021, 18:18, edited 2 times in total.
14.3 & 1.3.7
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: add 1 year to the current date

09 May 2021, 18:16

Code: Select all

RegExMatch(WinHttp.ResponseText, """datetime"":""\K.+?(?=T)", date)
Gui, Destroy
nextYear := StrReplace(date, "-")
nextYear += 365, Days
FormatTime, nextYear, %nextYear%, dd.MM.yyyy
MsgBox, 64, Next year, %nextYear%
But I prefer your method!
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: add 1 year to the current date

09 May 2021, 18:20

Whether to add 365 days or 1 year will give different results. That's for the OP to decide...
14.3 & 1.3.7
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: add 1 year to the current date

09 May 2021, 18:30

Or could just use seconds instead....
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: add 1 year to the current date

10 May 2021, 04:35

mikeyww wrote:
09 May 2021, 16:32

Code: Select all

RegExMatch(WinHttp.ResponseText, """datetime"":""(.+?)-(.+?)-(.+?)T", datePart)
Gui, Destroy
MsgBox, 64, Next year, % nextYear := datePart3 "." datePart2 "." datePart1 + 1

centuryago := 10.10.1900 

I will have one more question. I have a definition of "centuryago". and the equivalent of 10.10.1900. How can we determine the greater or lesser relationship with the definition of "centruyago" with the date detected in your scenario? In other words, the value of 10101900 is higher than 10052021 due to the reason resulting from the date format. normally the history for the past should be small. How do we determine the lower or greater than date relationship?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: add 1 year to the current date

10 May 2021, 05:11

chngrcn wrote:
09 May 2021, 15:57
add 1 year to the current date
Cannot be done with normal maths.
However, you can add 12 months to current date.

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force

NextYear := AddMonths(A_Now, 12)
Msgbox % NextYear


AddMonths(date, months) {
Local
  date += 0, s
  Loop %Months%
  {
   ; find last day of current month
     ldom := Substr(date,1,6) . "28" . Substr(date,9,6)
     ldom += 4, Days
     ldom := Substr(ldom,1,6) . "01" . Substr(ldom,9,6)
     ldom += -1, Day

   ; extract DD from date
     AddDays := SubStr(ldom,7,2)
     date += AddDays, Days
  }
Return date
}
My Scripts and Functions: V1  V2
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: add 1 year to the current date

10 May 2021, 05:26

chngrcn wrote:
10 May 2021, 04:35
... I will have one more question. ...
That's why the YYYYMMDDHH24MISS format is used with date-time calculations.
iPhilip
Posts: 795
Joined: 02 Oct 2013, 12:21

Re: add 1 year to the current date

10 May 2021, 14:26

For adding years, this is faster (~9 times):

Code: Select all

NextYear := AddYears(A_Now, 1)
Msgbox % NextYear

AddYears(Date, Years) {
   Date += 0, Days
   RegExMatch(Date, "O)(\d{4})(\d{2})(\d{2})(\d{6})", Match)
   YYYYMM := (Match[1] + Years) Match[2]
   if (Match[2] Match[3] != "0229")
      return YYYYMM Match[3] Match[4]
   Loop {
      NewDate := YYYYMM Format("{:02u}", Match[3] - A_Index + 1) Match[4]
      NewDate += 0, Days
      if NewDate
         return NewDate
   }
}
Here is also a faster version for adding months:

Code: Select all

NextYear := AddMonths(A_Now, 12)
Msgbox % NextYear

AddMonths(Date, Months) {
   Date += 0, Days
   RegExMatch(Date, "O)(\d{4})(\d{2})(\d{2})(\d{6})", Match)
   Months += Match[2]
   YYYYMM := (Match[1] + Months//12) Format("{:02u}", Mod(Months, 12))
   if (Match[3] < 29)
      return YYYYMM Match[3] Match[4]
   Loop {
      NewDate := YYYYMM Format("{:02u}", Match[3] - A_Index + 1) Match[4]
      NewDate += 0, Days
      if NewDate
         return NewDate
   }
}
Note that for the 02/29/2024 date mentioned by @flyingDman above, adding 1 year or 12 months using my versions yields 02/28/2025 whereas @SKAN's version yields 03/01/2025, a day later. I will leave it up to the user to decide which is the preferred result. :)

Edit: Optimized functions.
Last edited by iPhilip on 13 May 2021, 11:48, edited 1 time in total.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: add 1 year to the current date

10 May 2021, 22:09

iPhilip wrote:
10 May 2021, 14:26
Note that for the 02/29/2024 date mentioned by @flyingDman above, adding 1 year or 12 months using my versions yields 02/28/2025 whereas @SKAN's version yields 03/01/2025, a day later.

Code: Select all

; DTSUB() - https://www.autohotkey.com/boards/viewtopic.php?t=58181
MsgBox % DTSUB("20240229", "20250301", "{1:} years, {2:} months & {3:} days")
 
If you can't reverse a result to its original value, your math is incorrect.
 
iPhilip wrote:
10 May 2021, 14:26
I will leave it up to the user to decide which is the preferred result. :)
 
I wouldn't. My result is correct. :)

PS:
Anybody here born on 29-Feb?
On a non-leap year 1-Mar is your birthday.
It is your preference when to celebrate, just don't try to mathematically prove it. :D
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: add 1 year to the current date

11 May 2021, 05:53

Hi @SKAN,

do you have a corresponding SubMonths() function?

AddMonths(20210130, 1) results in 20210302. February is one complete month, so it doesn't seem to be correct.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: add 1 year to the current date

11 May 2021, 07:18

just me wrote:
11 May 2021, 05:53
Hi @SKAN,

do you have a corresponding SubMonths() function?

AddMonths(20210130, 1) results in 20210302. February is one complete month, so it doesn't seem to be correct.
I should have written a DTADD() and checked it against DTSUB(). :(
My algo is wrong. Thank you.
iPhilip
Posts: 795
Joined: 02 Oct 2013, 12:21

Re: add 1 year to the current date

11 May 2021, 17:33

SKAN wrote:
10 May 2021, 22:09
Anybody here born on 29-Feb?
On a non-leap year 1-Mar is your birthday.
It is your preference when to celebrate, just don't try to mathematically prove it. :D
Hi @SKAN,

I don't know anyone born on February 29 or I would ask them but according to this article:
... most blow out their candles on February 28 or March 1.
One “leapling” or “leaper” says:
“I always celebrate on February 28 — my birthday isn’t in March!”
Cheers! :)

- iPhilip
Last edited by iPhilip on 11 May 2021, 17:56, edited 1 time in total.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 61 guests