Jump to content
frew

How to send as text the current date and time in a certain format?

Recommended Posts

Hello,

Can someone please show me how to Send as text

the current date and time in this format, for example:

10-9-19_4_29pm

 

I can then run my script with a hotkey combination so that

the current date and time will be sent whenever I press the hotkey combination.

 

For example, at the caret in the File name field in a Save As dialog box.

Thank you.

Share this post


Link to post
Share on other sites

Have you looked at the available date/time @Macro variables and tried to concatenate those with the separation characters you want?

Jos

 

Share this post


Link to post
Share on other sites

Bored, so here is one way to do it (requires testing):

HotKeySet("{ESC}", "_Exit")
HotKeySet("`", "_DateTime")

While 1
    Sleep(100)
WEnd

Func _DateTime()
    ;~ Declare @HOUR
    Local $iHour = @HOUR
        ;~ Determine am/pm
        $sHour = ($iHour >= 0 And $iHour <= 11) ? "am" : "pm"
        ;~ Change Hour 0 to 12
        $iHour = $iHour = 0 ? 12 : $iHour
        ;~ Change Hour to 12 Hour format
        $iHour = $iHour > 12 ? $iHour - 12 : $iHour
    ;~ Format Date/Time
    Local $sDateTime = StringFormat("%1i-%1i-%2i_%1i_%1i" & $sHour, @MON, @MDAY, StringRight(@YEAR, 2), $iHour, @MIN)
    Send($sDateTime)
EndFunc

Func _Exit()
    Exit
EndFunc

 

Share this post


Link to post
Share on other sites
47 minutes ago, Jos said:

Have you looked at the available date/time @Macro variables and tried to concatenate those with the separation characters you want?

Jos

 

Thanks, at your suggestion I looked into using the Macros.

I came up with this, which will do.

It's not exactly the format I want, but close.

#include <Date.au3>

Send("_" & @MON & "-" & @MDAY & "-" & @YEAR & "_" & @HOUR & "h_" & @MIN & "m")

Gives a result like this:

_10-09-2019_17h_49m

 

Share this post


Link to post
Share on other sites
Posted (edited)
46 minutes ago, Subz said:

Bored, so here is one way to do it (requires testing):

HotKeySet("{ESC}", "_Exit")
HotKeySet("`", "_DateTime")

While 1
    Sleep(100)
WEnd

Func _DateTime()
    ;~ Declare @HOUR
    Local $iHour = @HOUR
        ;~ Determine am/pm
        $sHour = ($iHour >= 0 And $iHour <= 11) ? "am" : "pm"
        ;~ Change Hour 0 to 12
        $iHour = $iHour = 0 ? 12 : $iHour
        ;~ Change Hour to 12 Hour format
        $iHour = $iHour > 12 ? $iHour - 12 : $iHour
    ;~ Format Date/Time
    Local $sDateTime = StringFormat("%1i-%1i-%2i_%1i_%1i" & $sHour, @MON, @MDAY, StringRight(@YEAR, 2), $iHour, @MIN)
    Send($sDateTime)
EndFunc

Func _Exit()
    Exit
EndFunc

 

Wow, I like this very much.

I wish I could understand how you created this.

I'll study it a bit.

I have a hotkey set up, so I think  I'll just make use of this part for now:

;~ Declare @HOUR
    Local $iHour = @HOUR
        ;~ Determine am/pm
        $sHour = ($iHour >= 0 And $iHour <= 11) ? "am" : "pm"
        ;~ Change Hour 0 to 12
        $iHour = $iHour = 0 ? 12 : $iHour
        ;~ Change Hour to 12 Hour format
        $iHour = $iHour > 12 ? $iHour - 12 : $iHour
    ;~ Format Date/Time
    Local $sDateTime = StringFormat("%1i-%1i-%2i_%1i_%1i" & $sHour, @MON, @MDAY, StringRight(@YEAR, 2), $iHour, @MIN)
    Send($sDateTime)

with

#include <Date.au3>

at the top of the script I think.

I love the resulting format of the Send with this script.

Thank you very much!

Edit: Here's an example of the result:

10-9-19_6_17pm

 

Edited by frew
clarification and additional relevant info

Share this post


Link to post
Share on other sites

can save some steps i believe..

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & mod(@HOUR , 12) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

Edited by iamtheky
fix more formatting stuff

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
3 hours ago, iamtheky said:

can save some steps i believe..

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & mod(@HOUR , 12) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

Okay, looks interesting.

However, just now I tried it and got this result, 23 minutes after midnight.

10-10-19_0_23am

So what might you suggest in order to get the 0_23 to 12_23?

I may not have mentioned I prefer a 12 hour clock in this case.

Thanks for the ideas.

Share this post


Link to post
Share on other sites

probably another ternary if i was fixing it quickly, but there has to be a more fun way...

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

Edited by iamtheky
little better

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
16 minutes ago, iamtheky said:

probably another ternary if i was fixing it quickly, but there has to be a more fun way...

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

Wow, nice, thanks. Works great.

Now I need to study what you've presented here.

Thanks again!

Share this post


Link to post
Share on other sites
3 hours ago, frew said:

Now I need to study what you've presented here.

The key term for further studies is ternary operator. Number and StringRight should be self-explanatory ;).


Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites
12 hours ago, iamtheky said:

probably another ternary if i was fixing it quickly, but there has to be a more fun way...

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

Well, actually now that it's 12 mid-day here, I notice this format result with the above script:

10-10-19_0_47pm

Any way to get that 0_47 to show 12_47 instead.

Sorry, I'm not quite skilled enough to be able to make  the required adjustments to the script, but I am studying it.

Thanks.

Share this post


Link to post
Share on other sites
19 hours ago, Subz said:

Bored, so here is one way to do it (requires testing):

HotKeySet("{ESC}", "_Exit")
HotKeySet("`", "_DateTime")

While 1
    Sleep(100)
WEnd

Func _DateTime()
    ;~ Declare @HOUR
    Local $iHour = @HOUR
        ;~ Determine am/pm
        $sHour = ($iHour >= 0 And $iHour <= 11) ? "am" : "pm"
        ;~ Change Hour 0 to 12
        $iHour = $iHour = 0 ? 12 : $iHour
        ;~ Change Hour to 12 Hour format
        $iHour = $iHour > 12 ? $iHour - 12 : $iHour
    ;~ Format Date/Time
    Local $sDateTime = StringFormat("%1i-%1i-%2i_%1i_%1i" & $sHour, @MON, @MDAY, StringRight(@YEAR, 2), $iHour, @MIN)
    Send($sDateTime)
EndFunc

Func _Exit()
    Exit
EndFunc

 

I think I may go back to using this one.

The only reason I avoided it a bit was it seemed even further over my head than

a kind of single line script with date and time Macros.

So I've got a lot to study here. I'm still a beginner I guess.

Thanks Subz!

Edit: I like the format result:

10-10-19_12_57pm

Edited by frew
clarification

Share this post


Link to post
Share on other sites

Now i am curious about what your troubleshooting (or lack thereof) looks like....

If this fixed that thing, and broke everything else, what changed in a very broken fashion?  I only ask because most answers here are rushed ideas, and not foolproof solutions.

***Feel free to not entertain me, but we are largely the 'teach a man to fish' type.  answer in the spoiler****

 

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 OR 12 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))
Spoiler

 

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 OR @HOUR = 12 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

 

 

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
6 hours ago, iamtheky said:

Now i am curious about what your troubleshooting (or lack thereof) looks like....

If this fixed that thing, and broke everything else, what changed in a very broken fashion?  I only ask because most answers here are rushed ideas, and not foolproof solutions.

***Feel free to not entertain me, but we are largely the 'teach a man to fish' type.  answer in the spoiler****

 

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 OR 12 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))
  Hide contents

 

msgbox(0, '' , @MON & "-" & number(@MDAY) & "-" & stringright(@YEAR , 2) & "_" & (@HOUR = 0 OR @HOUR = 12 ? 12 : mod(@HOUR , 12)) & "_" & @MIN & (@HOUR > 11 ? "pm" : "am"))

 

 

 

 

I only know the very basics of making little scripts to send some keys, open some folders, etc.

I like to be able to press a hotkey and send the date and time at the end of filenames I save in Save As dialog boxes.

I think maybe the latest script you provide may give 12 for noon and midnight.

I look at this

(@HOUR = 0 OR @HOUR = 12 ? 12 : mod(@HOUR , 12)

and because I am beginner with scripting still, I do not understand what it does.

I do need to take a programming basics course I think.

Thank you for the modification.

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
6 hours ago, frew said:

I look at this

(@HOUR = 0 OR @HOUR = 12 ? 12 : mod(@HOUR , 12)

and because I am beginner with scripting still, I do not understand what it does.

(@HOUR = 0 OR @HOUR = 12 ? 12 : Mod(@HOUR , 12))

can also be written as :

If @HOUR = 0 OR @HOUR = 12 Then
    ; @HOUR=00 or @HOUR=12 ==> write 12
    ConsoleWrite('> IF   : 12' & @CRLF)
Else
    ; @HOUR=01..11 or @HOUR=13..23 ==> write @HOUR modulo 12
    ConsoleWrite('> ELSE : ' & Mod(@HOUR , 12) & @CRLF)
EndIf

Mod stands for modulo operation https://en.wikipedia.org/wiki/Modulo_operation

ConsoleWrite('< --------------------------------------------- ' & @CRLF)
ConsoleWrite('> @HOUR = ' & @HOUR & @CRLF)
ConsoleWrite('< --------------------------------------------- ' & @CRLF)

If @HOUR = 0 OR @HOUR = 12 Then
    ; @HOUR=00 or @HOUR=12 ==> write 12
    ConsoleWrite('> IF   : 12' & @CRLF)
Else
    ; @HOUR=01..11 or @HOUR=13..23 ==> write @HOUR modulo 12
    ConsoleWrite('> ELSE : ' & Mod(@HOUR , 12) & @CRLF)
EndIf

; @HOUR : Hours value of clock in 24-hour format. Range is 00 to 23
ConsoleWrite('< ----- All values ---------------------------- ' & @CRLF)
ConsoleWrite('+ 00 modulo 12 = ' & Mod(0 , 12) & @CRLF)
ConsoleWrite('+ 01 modulo 12 = ' & Mod(1 , 12) & @CRLF)
ConsoleWrite('+ 02 modulo 12 = ' & Mod(2 , 12) & @CRLF)
ConsoleWrite('+ 03 modulo 12 = ' & Mod(3 , 12) & @CRLF)
ConsoleWrite('+ 04 modulo 12 = ' & Mod(4 , 12) & @CRLF)
ConsoleWrite('+ 05 modulo 12 = ' & Mod(5 , 12) & @CRLF)
ConsoleWrite('+ 06 modulo 12 = ' & Mod(6 , 12) & @CRLF)
ConsoleWrite('+ 07 modulo 12 = ' & Mod(7 , 12) & @CRLF)
ConsoleWrite('+ 08 modulo 12 = ' & Mod(8 , 12) & @CRLF)
ConsoleWrite('+ 09 modulo 12 = ' & Mod(9 , 12) & @CRLF)
ConsoleWrite('+ 10 modulo 12 = ' & Mod(10, 12) & @CRLF)
ConsoleWrite('+ 11 modulo 12 = ' & Mod(11, 12) & @CRLF)
ConsoleWrite('+ 12 modulo 12 = ' & Mod(12, 12) & @CRLF)
ConsoleWrite('+ 13 modulo 12 = ' & Mod(13, 12) & @CRLF)
ConsoleWrite('+ 14 modulo 12 = ' & Mod(14, 12) & @CRLF)
ConsoleWrite('+ 15 modulo 12 = ' & Mod(15, 12) & @CRLF)
ConsoleWrite('+ 16 modulo 12 = ' & Mod(16, 12) & @CRLF)
ConsoleWrite('+ 17 modulo 12 = ' & Mod(17, 12) & @CRLF)
ConsoleWrite('+ 18 modulo 12 = ' & Mod(18, 12) & @CRLF)
ConsoleWrite('+ 19 modulo 12 = ' & Mod(19, 12) & @CRLF)
ConsoleWrite('+ 20 modulo 12 = ' & Mod(20, 12) & @CRLF)
ConsoleWrite('+ 21 modulo 12 = ' & Mod(21, 12) & @CRLF)
ConsoleWrite('+ 22 modulo 12 = ' & Mod(22, 12) & @CRLF)
ConsoleWrite('+ 23 modulo 12 = ' & Mod(23, 12) & @CRLF)

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites

Thank you very much  Musashi.

I can see I need to find some scripting tutorials, etc.

I get lost with some of the basics.

Thanks all, for the help and ideas to try.

Are there any known good places to start.

Can anybody suggest a common approach to learning to script?

I'd like to be able to utilize some more of AutoIt's vast potentials.

Thanks again!

Share this post


Link to post
Share on other sites

@frew

Start from the Help file and look in the Forums whenever you need it :)

EDIT:

@Musashi

For $i = 0 To 23 Step 1
    ConsoleWrite(" + " & $i & " modulo 12 = " & Mod($i, 12) & @CRLF)
Next

:D

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
22 minutes ago, FrancescoDiMuro said:

Musashi

For $i = 0 To 23 Step 1
    ConsoleWrite(" + " & $i & " modulo 12 = " & Mod($i, 12) & @CRLF)
Next

:D

I know. It was just my intention to make it crystal clear 😛 .


Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...