Simpel formattime

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Simpel formattime

21 Jul 2018, 17:50

Hi guys

I have a date string thats YYYYMMDD-XXXX

If i put it through

FormatTime, time , [YYMMDD XXXX] Will it come out as YYMMDD XXXX?

Or im i missing somthing?
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simpel formattime

22 Jul 2018, 03:37

Hallo,
try this:

Code: Select all

FormatTime, time,, yyyyMMdd-HHmm
MsgBox, %  "Time now:`n" time
FormatTime, time, yyyyMMdd-HHmm, yyyyMMdd HHmm
MsgBox, %  "Time reformatted:`n" time
and that:

Code: Select all

FormatTime, time,, yyyyMMdd-HHmm
MsgBox, %  "Time now:`n" time
time := StrReplace(time,"-"," ")
MsgBox, %  "Time reformatted:`n" time
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 05:26

Hi

Thx for your code. Two questions. I'm gonnea use both of them? And they seem to only give todays date? I would like the to give a date thats in the format YYYYMMDD-XXXX thats on the clipboard and that comes out as the script that you posted.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 06:07

- What is XXXX? Is it HHmm? That information is crucial.
- FormatTime can only handle blank (for the time now) or a number of the form yyyyMMddHHmmss, or a partial string e.g. of the form yyyyMMdd or yyyyMMddHHmm.
- So, some possible input values: 20060504030201, 200605040302, 2006050403, 20060504, 200605, 2006, (blank string, for the time now).
- Here's some code:

Code: Select all

q:: ;convert dates - 'yyyyMMdd-HHmm' to 'yyyyMMdd HHmm'
;conversion approach 1:
vDate := "20060504-0302"
vDate := StrReplace(vDate, "-", " ")
MsgBox, % vDate

;conversion approach 2:
vDate := "20060504-0302"
vDate := StrReplace(vDate, "-")
FormatTime, vDate, % vDate, yyyyMMdd HHmm
MsgBox, % vDate
return
- [EDIT:] I slightly misread your first post, here's the exact format you specified:

Code: Select all

q:: ;convert dates - 'yyyyMMdd-HHmm' to 'yyMMdd HHmm'
;conversion approach 1:
vDate := "20060504-0302"
vDate := SubStr(StrReplace(vDate, "-", " "), 3)
MsgBox, % vDate

;conversion approach 2:
vDate := "20060504-0302"
vDate := StrReplace(vDate, "-")
FormatTime, vDate, % vDate, yyMMdd HHmm
MsgBox, % vDate
return
- [EDIT:] Here are some basic RegEx checks to check the datestamp:

Code: Select all

q:: ;RegEx - check datestamp
vDate := "20060504-0302"
MsgBox, % RegExMatch(vDate, "^\d{8}-\d{4}$") ;yyyyMMdd-HHmm

vDate := "060504 0302"
MsgBox, % RegExMatch(vDate, "^\d{6} \d{4}$") ;yyMMdd HHmm
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 06:21

XXXX is not HHmm , its number going from 0101 to 9999.

Hmm. I realy just want to get the "-" and "19" to go away so the number presents itself as YYMMDD XXXX. Rohwedeers code did that to some degree
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simpel formattime

22 Jul 2018, 06:40

Hallo,
@jeeswg:
FormatTime can only handle … a number of the form yyyyMMddHHmmss … ??
try:

Code: Select all

FormatTime, time,, MM*yyyy:dd+mmHH
MsgBox, %  "Time coded`n" time
FormatTime, time, MM*yyyy:dd+mmHH, yyyyMMdd HHmm
MsgBox, %  "Time reformatted:" time
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 07:27

Yo Rohwedder

Code: Select all

FormatTime, time,, yyyyMMdd-HHmm
MsgBox, %  "Time now:`n" time
FormatTime, time, yyyyMMdd-HHmm, yyMMdd HHmm
MsgBox, %  "Time reformatted:`n" time
does exactly what I want it to do (gone use it on clipboard but, thats not important right now.)

The only problem is that ist only formating todays date and not the date that i copied on to the cliboard.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 07:28

- That sounds like a simple string manipulation, one of my examples above achieved that, here it is again. You replace hyphens with spaces, and crop the first 2 characters via SubStr.

Code: Select all

q:: ;convert dates - 'yyyyMMdd-XXXX' to 'yyMMdd XXXX'
vDate := "20060504-9999"
vDate := SubStr(StrReplace(vDate, "-", " "), 3)
MsgBox, % vDate
return
- @Rohwedder: I'm not sure what you mean, can you give a concrete example? Cheers.
FormatTime - Syntax & Usage | AutoHotkey
https://autohotkey.com/docs/commands/FormatTime.htm
YYYYMMDD...
Leave this parameter blank to use the current local date and time. Otherwise, specify all or the leading part of a timestamp in the YYYYMMDDHH24MISS format.
(Where YYYYMMDDHH24MISS is yyyyMMddHHmmss.) (Note: that's in reference to the *input* parameter, not the format parameter.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 07:41

Jeeswg, simple:P I'm trying to learn string manipulation, but I think its hard. Maybe you can help me with another one after this :)

It worked like a charm :D:D
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 08:26

Im going to post my other problem here to, since you guys where so awesome

Code: Select all

[Send, ^c
IfLessOrEqual,A_Now, EnvAdd, ymdhms, -6574, days
{
send,^+F
}
else 
loop,4
{
send, ^+F
}
]/code]/code]

I trying to get THE code to preform a action of date is more then 18 years Ago.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 09:50

- I would do it like this, using a backport of the DateDiff function from AHK v2.
- Note: the dates must be in the appropriate format: 'all or the leading part of a timestamp in the YYYYMMDDHH24MISS format'.

Code: Select all

q:: ;dates - are dates at least 18 years apart
vDate1 := 20180722120000 ;midday 2018-07-22
vList := "20010101,20000101"

Loop, Parse, vList, % ","
{
	vDate2 := A_LoopField
	;18 years ~= 18*365 days = 6570 days
	;note: DateDiff does vDate1 subtract vDate2 (you could use Abs to get the absolute difference)
	if (DateDiff(vDate1, vDate2, "D") > 6570)
		MsgBox, % "over 18 years ago"
	else
		MsgBox, % "less than 18 years ago"
}
return

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

DateDiff(DateTime1, DateTime2, TimeUnits)
{
    EnvSub DateTime1, %DateTime2%, %TimeUnits%
    return DateTime1
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 10:05

The format Will be as before YYYYMMDD-XXXX, is that a problem with that coding? The last three digits are obselete in this context, I only need YYYYMMDD.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 10:10

If the hyphen and last 4 digits are unnecessary, you can crop them like this:

Code: Select all

q:: ;convert dates - 'yyyyMMdd-XXXX' to 'yyyyMMdd'
vDate := "20060504-9999"
vDate := SubStr(vDate, 1, -5)
MsgBox, % vDate
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 10:17

jeeswg wrote:If the hyphen and last 4 digits are unnecessary, you can crop them like this:
Code: [Select all]GeSHi © Codebox Plus

q:: ;convert dates - 'yyyyMMdd-XXXX' to 'yyyyMMdd'
vDate := "20060504-9999"
vDate := SubStr(vDate, 1, -5)
MsgBox, % vDate
retur
hmm I did it like this

Code: Select all

vDate := "clipboard"
vDate := SubStr(vDate, 1, -5)
vList := clipboard

Loop, Parse, vList, % ","
{
	vDate2 := A_LoopField
	;18 years ~= 18*365 days = 6570 days
	;note: DateDiff does vDate1 subtract vDate2 (you could use Abs to get the absolute difference)
	if (DateDiff(vDate1, vDate2, "D") > 6570)
		MsgBox, % "over 18 years ago"
	else
		MsgBox, % "less than 18 years ago"
}
And I got "less than 18 years ago" no matter what number i Put in.
And also a little daft question. To get it to work as I mentioned from the begining I just substitute "MSG box" with { send,^+F } andloop,4 { send, ^+F }
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 10:35

This is probably more like what you want:

Code: Select all

q:: ;dates - is date at least 18 years ago
vDate1 := A_Now
;vDate2 := "20060504-9999"

Clipboard := ""
SendInput, ^c
ClipWait, 3
if ErrorLevel
{
	MsgBox, % "error: failed to retrieve clipboard text"
	return
}
vDate2 := Clipboard

if !RegExMatch(vDate2, "^\d{8}-\d{4}$") ;yyyyMMdd-XXXX
{
	MsgBox, % "error: invalid datestamp:`r`n" vDate2
	return
}

vDate2 := SubStr(vDate2, 1, -5)

;18 years ~= 18*365 days = 6570 days
;note: DateDiff does vDate1 subtract vDate2 (you could use Abs to get the absolute difference)
if (DateDiff(vDate1, vDate2, "D") > 6570)
	MsgBox, % "over 18 years ago"
else
	MsgBox, % "less than 18 years ago"
return

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

DateDiff(DateTime1, DateTime2, TimeUnits)
{
    EnvSub DateTime1, %DateTime2%, %TimeUnits%
    return DateTime1
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 11:33

jeeswg wrote:This is probably more like what you want:

Code: Select all

q:: ;dates - is date at least 18 years ago
vDate1 := A_Now
;vDate2 := "20060504-9999"

Clipboard := ""
SendInput, ^c
ClipWait, 3
if ErrorLevel
{
	MsgBox, % "error: failed to retrieve clipboard text"
	return
}
vDate2 := Clipboard

if !RegExMatch(vDate2, "^\d{8}-\d{4}$") ;yyyyMMdd-XXXX
{
	MsgBox, % "error: invalid datestamp:`r`n" vDate2
	return
}

vDate2 := SubStr(vDate2, 1, -5)

;18 years ~= 18*365 days = 6570 days
;note: DateDiff does vDate1 subtract vDate2 (you could use Abs to get the absolute difference)
if (DateDiff(vDate1, vDate2, "D") > 6570)
	MsgBox, % "over 18 years ago"
else
	MsgBox, % "less than 18 years ago"
return

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

DateDiff(DateTime1, DateTime2, TimeUnits)
{
    EnvSub DateTime1, %DateTime2%, %TimeUnits%
    return DateTime1
}

awsome, now it does exactly what I want it to do. Im going to live test it tomorrow.

string manipulation is a bit tricky. you seem to get the hang of it. It should be possible to get it to work the clipbord to change text from for exampel: Mom: to Mom: 23132131561531
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 12:08

- Check the documentation for InStr, SubStr, StrReplace, and look on the forum for examples of if statements.
E.g. if (var = "text")
E.g. if (var1 = var2)

Code: Select all

q::
vText := "Mom:"
if InStr(vText, "Mom:")
	vText := "Mom: 23132131561531"
MsgBox, % vText
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 13:26

jeeswg wrote:This is probably more like what you want:

Code: Select all

q:: ;dates - is date at least 18 years ago
vDate1 := A_Now
;vDate2 := "20060504-9999"

Clipboard := ""
SendInput, ^c
ClipWait, 3
if ErrorLevel
{
	MsgBox, % "error: failed to retrieve clipboard text"
	return
}
vDate2 := Clipboard

if !RegExMatch(vDate2, "^\d{8}-\d{4}$") ;yyyyMMdd-XXXX
{
	MsgBox, % "error: invalid datestamp:`r`n" vDate2
	return
}

vDate2 := SubStr(vDate2, 1, -5)

;18 years ~= 18*365 days = 6570 days
;note: DateDiff does vDate1 subtract vDate2 (you could use Abs to get the absolute difference)
if (DateDiff(vDate1, vDate2, "D") > 6570)
	MsgBox, % "over 18 years ago"
else
	MsgBox, % "less than 18 years ago"
return

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

DateDiff(DateTime1, DateTime2, TimeUnits)
{
    EnvSub DateTime1, %DateTime2%, %TimeUnits%
    return DateTime1
}
Can I put another IF statement to make it do an action if var is text?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simpel formattime

22 Jul 2018, 14:10

It depends on what you mean by 'text'. No numbers? Contains at least 1 letter? Contains at least one space? Or some other condition like that. If you can come up with some criteria, it should be doable.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marsa
Posts: 25
Joined: 11 Jul 2018, 10:36

Re: Simpel formattime

22 Jul 2018, 14:29

jeeswg wrote:It depends on what you mean by 'text'. No numbers? Contains at least 1 letter? Contains at least one space? Or some other condition like that. If you can come up with some criteria, it should be doable.
If i remember correctly, its at least one letter and maybe a space. How would one go by too implement that in the code? Im trying to learn the IF condition also, im trying to read att the hompage, but im having a hard time puzzeling it togheter

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Google [Bot] and 250 guests