Show date as previous 5/10/15/20th day of month

Get help with using AutoHotkey and its commands and hotkeys

Show date as previous 5/10/15/20th day of month

31 May 2017, 15:32

Hi all,

I've been trying to create scripts for the first time and most of my questions have been answered by a quick Google search, but I've now run into a situation that I can't seem to solve:

I want my script to return today's date if the day of the month is the 5th, 10th, 15th, 20th, 25th, or 30th. If today's date falls somewhere in between these days, I want it to return the most recent previous 5/10/15/20/25/30 date.

Here's my best try at it, but still can't get it to work:

FormatTime, Date1, %Date1%, MMddyy

If A_dd between 01 and 04, dd=30 AND MM += -1
If A_dd between 06 and 09, dd=05
If A_dd between 11 and 14, dd=10
If A_dd between 16 and 19, dd=15
If A_dd between 21 and 24, dd=20
If A_dd between 26 and 29, dd=25
If A_dd equals 31, dd=30

Else (??? Not sure if I need an "else" line, or it will just default to today's date if none of above conditions are met. If I need another line, maybe something like If A_DD equals 5 OR 10 OR 15 OR 20 OR 25 OR 30, Date1)

Send, %Date1%


I could probably get it to work by adding an IF line for each day of the month, eg.
If A_DD equals 07, Date1 += -2, days
IF A_DD equals 08, Date1 += -3, days
But I figure there's probably an easier way

Thanks for any help you can provide!
Posts: 108
Joined: 15 Apr 2017, 21:06

Re: Show date as previous 5/10/15/20th day of month

31 May 2017, 16:33

Code: Select all

	if (mod(a_dd,5) == 0)
		msgbox %a_dd%
		msgbox % a_dd - mod(a_dd,5)

Re: Show date as previous 5/10/15/20th day of month

31 May 2017, 17:21

Thanks! That's a great simple solution that will work for most of the month, I used it to put together this:

if (mod(a_dd,5) == 0)
Send, %a_dd%
Send, % a_dd - mod(a_dd,5)

StringRight, A_YY, A_YYYY, 2 ; get the 2 digit year

This will output the date in the format I want (eg. 053017)

2 issues though:
- Between the 1st and 4th of each month, this script will return a day of 0. I need some kind of additional statement where between 1st and 4th, it returns the 30th and also shows the month as previous month.
- Between 5th and 9th, the day is displaying as 1 digit instead of including the leading zero (5 instead of 05), not sure how to change this format? Since above is using the format "dd" I thought it would return the leading zero already.
Posts: 108
Joined: 15 Apr 2017, 21:06

Re: Show date as previous 5/10/15/20th day of month

31 May 2017, 17:54

heres an example of how to fix issue 1

Code: Select all

date := 4
if (date < 5)
		send, % a_mm - 1 "/30/"a_yyyy
just me
Posts: 8592
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Show date as previous 5/10/15/20th day of month

01 Jun 2017, 02:18

Code: Select all

MyDate := A_Now                                          ; set MyDate to the currrent DateTime
If (A_DD < 5)                                            ; if the current day is less 5
   MyDate += -%A_DD%, D                                  ; ... go back to the last day of the previous month
MyDay := SubStr(MyDate, 7, 2)                            ; get MyDay from MyDate
MyDay -= Mod(MyDay, 5)                                   ; set MyDay to a multiple of 5
MyDate := Format(SubStr(MyDate, 1, 6) . "{:02}", MyDay)  ; rebuild MyDate (the day's part needs two digits)
FormatTime, MyDate, %MyDate%, MMddyy                     ; create the output format
MsgBox, %MyDate%
User avatar
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Show date as previous 5/10/15/20th day of month

27 May 2019, 21:38

Interesting script requirement.
Here is a function version, using essentially the same logic as just me's code.

Code: Select all

q:: ;round down to the nearest 5 days
vDate := A_Now
vOutput := ""
Loop 40
	FormatTime, vDate1, % vDate, yyyy-MM-dd
	vDate2 := DateRoundDown(vDate, "yyyy-MM-dd")
	vOutput .= vDate1 "`t" vDate2 "`r`n"
	EnvAdd, vDate, 1, Day
Clipboard := vOutput
MsgBox, % "done"

;expects dates of the form yyyyMMddHHmmss (or yyyyMMddHHmmss with later bits omitted)
;round down to a date where the day is a multiple of 5
DateRoundDown(vDate, vFormat:=" ")
	vSfx := SubStr(vDate, 9)
	if (StrLen(vDate) < 8)
		FormatTime, vDate, % vDate, yyyyMMdd

	vDay := SubStr(vDate, 7, 2)
	if (vDay < 5)
		EnvAdd, vDate, % -vDay, Days
		vDay := SubStr(vDate, 7, 2)

	vDay -= Mod(vDay, 5)
	vDate := SubStr(vDate, 1, 6) Format("{:02}", vDay) vSfx

	if !(vFormat = " ")
		FormatTime, vDate, % vDate, % vFormat
	return vDate
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Scra and 46 guests