Add zeros to the left

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Add zeros to the left

15 Dec 2018, 04:03

I need a time stamp of the form:

%A_YYYY% %A_MM% %A_DD% %A_Hour% %A_Min% %A_Sec%

20181214123212

Problem is that when some of the variables (except for the year) are only 1 figure, for example minute 03 then the time stamp becomes only 13 chars: 2018121412312

So I add trailing zeros to the left of mm, dd, hh, mm and ss.

%A_YYYY% SubStr("0" . %A_MM%, -2) SubStr("0" . %A_DD%, -2) SubStr("0" . %A_Hour%, -2) SubStr("0" . %A_Min%, -2) SubStr("0" . %A_Sec%, -2)

It doesn't work as expected:

SubStr("0" . %A_Sec%, -2)

is still 1 figure when 0 < %A_Sec% <10

HOWEVER!!

SubStr("K" . %A_Sec%, -2) or SubStr("111" . %A_Sec%, -4)

works fine!

Seconds are now 'K3' or whatever the second is between 0 and 9, I can't add zeros to the left but I can add any other char/figure!

It seems like leading zeros are skipped since SubStr("100" . %A_Sec%, -4) works fine while SubStr("100" . %A_Sec%, -3) don't the leading zeros are gone.

Please help, this is REALLY testing my mental health!
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Add zeros to the left

15 Dec 2018, 06:18

Hallo,
try:

Code: Select all

Loop
{
Time1 = %A_YYYY%%A_MM%%A_DD%%A_Hour%%A_Min%%A_Sec%
FormatTime, Time2,, yyyyMMddHHmmss
Time3 := A_YYYY SubStr("0" . A_MM, -1) SubStr("0" . A_DD, -1) SubStr("0" . A_Hour, -1) SubStr("0" . A_Min, -1) SubStr("0" . A_Sec, -1)
ToolTip,%  "Time1: " Time1 " " StrLen(Time1) " chars`nTime2: " Time2 " " StrLen(Time2) " chars`nTime3: " Time3 " " StrLen(Time3) " chars"
Sleep, 200
}
All three times always have 14 characters.
I recommend Time2.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Add zeros to the left

15 Dec 2018, 06:28

Try A_Now:

Code: Select all

MsgBox, I need a time stamp of the form:`n...`n20181214123212`n`n%A_Now%
I hope that helps.

edit: which is exactly option2, as recommended by Rohwedder in the post above.
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Add zeros to the left

15 Dec 2018, 06:37

aircooled wrote:Problem is that when some of the variables (except for the year) are only 1 figure, for example minute 03 ...
They aren't! A_MM, A_DD, A_Hour, A_Min, and A_Sec contain 2 digits.

Code: Select all

DateTime := Format("{:04}{:02}{:02}{:02}{:02}{:02}", Year, Month, Day, Hour, Min, Sec)
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Add zeros to the left

15 Dec 2018, 10:48

Thanks Rohwedder

it works

but my error was ridiculous

writing with spaces in between like this

Time1 = %A_YYYY% %A_MM% %A_DD% %A_Hour% %A_Min% %A_Sec%

make the zeros to the left vanish like for example in second #2 it gives not 02 but 2

writing like you did without spaces

Time1 = %A_YYYY%%A_MM%%A_DD%%A_Hour%%A_Min%%A_Sec%

works fine and msgbox gives

20181215164709

I don't understand why but it works fine without formatting

go figure
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Add zeros to the left

16 Dec 2018, 01:12

Some code.

Code: Select all

FormatTime, vDate,, yyyy MM dd HH mm ss
MsgBox, % vDate
See also:
combining date variables is unreliable - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=36338
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 (v1)”

Who is online

Users browsing this forum: RandomBoy and 181 guests