Getting unexpected results with "Yesterday" using FormatTime

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
clarkj0388
Posts: 13
Joined: 11 Aug 2022, 18:50

Getting unexpected results with "Yesterday" using FormatTime

Post by clarkj0388 » 11 Aug 2022, 18:57

Code: Select all

FormatTime, Today,, M.d.yy
FormatTime, NOW,, M.d.yy-h.mm.sst
FormatTime, HOUR,, H

Yesterday += -1, days
FormatTime, Yesterday, %Yesterday%, M.d.yy

if HOUR < 2
	{
	 FileCopy, C:\PrintChit\pVenmo\resources\chit.png, C:\PrintChit\pArchive\dated\%Yesterday%\%SubjectClean%_%NOW%.png, 1
	}
else
	{
	 FileCopy, C:\PrintChit\pVenmo\resources\chit.png, C:\PrintChit\pArchive\dated\%Today%\%SubjectClean%_%NOW%.png, 1
	}
Trying to sort PNG files being created with ImageMagik, but getting unexpected results. When I run this code with message box tests, it works fine, but in real life, all PNGs are being sorted into their %Today% folder, without the exception for the %Yesterday% folder for the first 2 hours past midnight.

This script runs on a loop, and waits 2 seconds between times it runs. So it is STARTED on the initial date, but then runs over past midnight. I wouldn't think this would matter, as the HOUR var should be resetting to the current hour on each loop right? After midnight, HOUR would = 0, or 1, so it should be then running the sort to "Yesterday" folder, but it's not... it's sorting to the current dates folder... ugh.

Help?

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by mikeyww » 11 Aug 2022, 21:16

Welcome to this AutoHotkey forum!

One debugging strategy is to display the values of your variables.

24-hour time is given by H rather than h. An example is found on line 3 of your script.

In 12-hour time, the range for hour is 1-12. The 12-hour time at one minute after midnight is 12:01.

Explained: Time formats

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by BoBo » 11 Aug 2022, 23:58

If you think about using this script for longer than 5 month, I'd go the way and create/save the files into a descending Year/Month/Day folder structure.

Code: Select all

#Persistent
SetTimer, storeFiles, 2000 ;2s detection frequency
return

storeFiles:
   now := A_Now
   FormatTime, fn,% now, M.d.yy-h.mm.sst
   FormatTime, dt,% now, yyyy-MM-dd
   d:=StrSplit(dt,"-")
   FileCopy,% "C:\PrintChit\pVenmo\resources\chit.png",% "C:\PrintChit\pArchive\dated\" d.1 "\" d.2 "\" d.3 "\" SubjectClean "_" fn  ".png", 1
   return
Not tested.

clarkj0388
Posts: 13
Joined: 11 Aug 2022, 18:50

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by clarkj0388 » 13 Aug 2022, 02:51

Thanks for your help @mikeyww

I intentionally used the capital H in my formattime, so that the hour would be 0 past midnight and 1 past 1 am. Hence why "if HOUR < 2"... I want the files created for the first 2 hours of the new day, to still sort into the previous days folder.

When I test it in a "sandbox" it works as expected... but for the life of me, I can't get it to work live.

I'm so lost. I just don't know why the its always sorting to the current date, and ignoring the if HOUR < 2....

Ugh...

clarkj0388
Posts: 13
Joined: 11 Aug 2022, 18:50

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by clarkj0388 » 13 Aug 2022, 02:55

@BoBo Thanks for this. This is so far beyond my system, it's crazy. I just manually move everything in the folder once a year, to a dated year backup folder.

Currently I'm struggling with the

if HOUR < 2

It's not performing properly after midnight. I think my code says as long as it's before 2am, to still sort it into the same folder as it was using before the date changed... by using the %Yesterday% var. But it's not working...

It's working when I do a simple sandbox test with msgbox to show the %HOUR% value, and the %Yesterday% value, but for the life of me, it won't work correctly live...

Struggling.

clarkj0388
Posts: 13
Joined: 11 Aug 2022, 18:50

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by clarkj0388 » 13 Aug 2022, 02:57

image.png
image.png (33.86 KiB) Viewed 676 times
the value of HOUR should be 0, and it's sorting to the %Today% folder, not the %Yesterday% folder...

grrr

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by just me » 13 Aug 2022, 03:44

Code: Select all

Loop, 2 {
   FormatTime, Today,, M.d.yy
   FormatTime, NOW,, M.d.yy-h.mm.sst
   FormatTime, HOUR,, H
   Yesterday += -1, days
   FormatTime, Yesterday, %Yesterday%, M.d.yy
   MsgBox, 0, Iteration %A_Index% , Today: %Today%`nNOW: %NOW%`nHOUR: %HOUR%`nYesterday: %Yesterday%
}
Yesterday += -1, days requires Yesterday to be empty or a valid timestamp (see EnvAdd).

In your code snippet Yesterday is probably empty at the first run so Yesterday += -1, days will work properly. But after FormatTime, Yesterday, %Yesterday%, M.d.yy it contains a string like 08.13.22. The next attempt to add one day will fail and result in an empty variable wich is treated as the current date-time in FormatTime, Yesterday, %Yesterday%, M.d.yy.

Result: You must initialize Yesterday before you use it again:

Code: Select all

   Yesterday := A_Now
   Yesterday += -1, days
   FormatTime, Yesterday, %Yesterday%, M.d.yy

clarkj0388
Posts: 13
Joined: 11 Aug 2022, 18:50

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by clarkj0388 » 13 Aug 2022, 04:09

@just me

Thank you! I haven't tested it yet. But it makes sense to me. I was thinking it could be something like that... but was stuck on the wrong section of code. I was thinking something needed to be reset, like using RANDOM, but this never occurred to me.

I'll just initialize it every loop, and that should give it the same starting point every time.

I won't know until the day changes over again tomorrow, but this has to be it. This old code used to work when the script was triggered once each time a file was created, but now I have it running in a loop, so the script is started at the beginning of our night, and just runs... it's all pointing in that direction, I just couldn't see it.

Appreciate you seeing through my issue.

ragegraylin
Posts: 2
Joined: 14 Aug 2022, 08:03

Re: Getting unexpected results with "Yesterday" using FormatTime

Post by ragegraylin » 14 Aug 2022, 08:06

thanks for sharing

Post Reply

Return to “Ask for Help (v1)”