Transforming Dates to ISO-Dates

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Transforming Dates to ISO-Dates

02 Aug 2023, 04:53

Hi folks :)

Having used this script to great satisfaction for some time now, it seemed necessary to extend it for further use.

So I decided to do something about those strange German dates (day, month, year = "24. Dezember 1900"), which, apart from the noun for the month, also start with the smallest unit and end with the largest. This is not a good way to sort dates in a list. The ISO format "YYYY-MM-DD" is much better. (The above example would be "1900-12-24"). I get those German dates in long lists when I enter old German TV-shows in my Obsidian-Vault.

Here's a short example of what I get:

--------------------------------------------------------------

Code: Select all

27. Juli 1960
4. Oktober 1960
24. April 1961
6. April 1962
28. November 1962
--------------------------------------------------------------
and that's what I want/need:
My first idea, of course, was to transform them using cell formatting in LibreOffice Calc. I only get it to work half the time; it is very cumbersome.
--------------------------------------------------------------

Code: Select all

1960-07-27
1960-10-04
1961-04-24
1962-04-06
1962-11-28

Three steps

1. Step:
--------------------------------------------------------------
I need to add a leading zero to the single digit (for days) at the beginning of the line

Code: Select all

4. Oktober 1960     ->     04. Oktober 1960
6. April 1962     ->     06. April 1962
This seems (seems[\b]) simple enough and of course I select RegEx as my weapon of chouce as follows:

Code: Select all

needleRegEx := '^(\d\.)' ; this will find a single digit a the beginning of the line
replacement := '0$1'     ; this will add a leading zero
These expressions I have tested manually (Search & Replace) in SublimeText and Brackets where they work perfectly.


Now I remembered a script @mikeyww provided and I did nothing more than replace two values, i.e. needleRegEx and replacement (as shown above)

Code: Select all

!q:: {
    Static needleRegEx := '^(\d\.)'
        , replacement := '0$1'
    A_Clipboard := '', Send('^a'), Send('^c')
    If ClipWait(1) {
        A_Clipboard := RegExReplace(A_Clipboard, needleRegEx, replacement)
        Send '^v'
    } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 48
    return
}
Of course I can provide the original script

First I tested it in SublimeText (~90 minutes of tweaking, changing, cursing), then in an act of desperation I switched to Brackets (where it worked exactly once (!)). Then I tried the rest of my editors, not understanding why a routine/script running "outside" of them should produce different results at all.

What's going on?
I am stuck :o
Thank you for your help :D



My editors: VSCode, SublimeText, Brackets, Notepad++
Only one step at the moment ...
Last edited by ItisI on 02 Aug 2023, 06:00, edited 2 times in total.
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
boiler
Posts: 17211
Joined: 21 Dec 2014, 02:44

Re: Transforming Dates to ISO-Dates

02 Aug 2023, 05:17

@ItisI — You need to be specific about what format the original date is in and which ISO date format you want it in.
User avatar
mikeyww
Posts: 27193
Joined: 09 Sep 2014, 18:38

Re: Transforming Dates to ISO-Dates

02 Aug 2023, 06:28

Perhaps:

Code: Select all

#Requires AutoHotkey v2.0 ; Save script file as UTF-8 with BOM signature
str := '
(
27. Juli 1960
4. Oktober 1960
24. April 1961
6. April 1962
28. November 1962
)'
MsgBox dates(str)

dates(txt) {
 Static mo := 'Jan|Feb|Mär|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez'
 out := ''
 For each, line in StrSplit(txt, '`n', '`r') {
  part := StrSplit(line, ' ')
  out  .= Format('{}-{:02}-{:02u}`n', part[3], key(mo, SubStr(part[2], 1, 3)), part[1])
 }
 Return Trim(out, '`n')
}

key(str, val) {
 If pos := InStr('|' str '|', '|' val '|') {
  StrReplace(SubStr(str, 1, pos), '|',,, &n)
  Return n + 1
 }
}
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Transforming Dates to ISO-Dates

02 Aug 2023, 09:30

Thanks for your replies @boiler and @mikeyww
I am sorry, I probably wasn't precise enough. I don't want to consider these ...

--------------------------------------------------------------

Code: Select all

27. Juli 1960
4. Oktober 1960
24. April 1961
6. April 1962
28. November 1962
--------------------------------------------------------------

as dates.
Indeed I consider them as character strings of varied length with a common pattern. The complete list usually has 20 - 60 entries, but may be shorter or larger. The result is a revised edition of this list, still character strings with a fixed length of 10 characters ("YYYY-MM-DD" ) which is ready to be read as a date, but resides simply as a text value in Obsidian data sheets (notes)

1. Find those where at the beginning of the line there's only one (1) digit followed by a "." -> '^(\d\.)' and prepend those lines with a "0" (zero). It is here that I'm stack because my RegExes (working in my editors) do nothing in the AHK-script.
2. Replace all month names with the appropriate number (01-12, again with leading zeros).
3. Switch the starting two digits (day) with the last four digits(year), and Bob's your uncle :D

@mikeyww , as I've just seen you have already informed Bob of his uncleship (just ran your script). I will now try to incorporate your script into my script. (I like the way VSC shows you the syntax of functions :D)

At the moment, two questions remain:
--------------------------------------------------------------
1. Why does my version not work?
--------------------------------------------------------------
2. Tried Google, Wikipedia ...
--------------------------------------------------------------
Save script file as UTF-8 with BOM signature
I used to think I knew what UTF-8 was, but now I have my doubts. And I definitely don't know how to save a file with a BOM signature. Or for that matter: without one...

Thanks very much for your continued help. Will be back tomorrow, (It's 16:31:13 over here, thinking is discouraged)
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

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

Re: Transforming Dates to ISO-Dates

02 Aug 2023, 09:37

Regarding your regex replacement, see :arrow: Multiline.
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 04:58

@mikeyww Bang on the midface protuberance :D

Code: Select all

needleRegEx := 'm)^(\d\.)'
Oh those effing details. And I specifically use ST. Supposedly with PCRE support. And I don't seem to be able to find an equivalent switch/option/button to turn on/OFF "m)" ...
--------------------------------------------------------------
Now I will attend to your fine script. As I see it
- something goes in ("input") -> "str"
- magic occurs ("black box") -> ???
- something goes out ("output") -> "dates(str)"

To get it to work like the rest of the routines/functions on a selected text, I "simply" need to pursuade your "input" to be selected text, and to pass your output via Send '^v' to the file. (What happens between "input" and "output" may remain a black box for a while)

Once that works, I will try to understand what your script actually does :) (Nice job on the abbreviations for the German month names. And on a US-Keyboard too...)

See you later
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
boiler
Posts: 17211
Joined: 21 Dec 2014, 02:44

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 05:34

@ItisI — In the future, please don’t go back and edit an earlier post in response to subsequent posts in an “Ask for Help” thread. It loses the context of the subsequent posts. When you first posted, you had no information in your first post other than the subject line. Then I posted saying more detail was required. Then you added a ton of detail to your first post. So when you now read the posts in order, it makes my post look kind of ridiculous — like why is he saying he needs to be specific about the date formats? Just reply in a subsequent post to keep the conversation coherent.

If your thinking was to post a blank post as a placeholder and then edit it to add the detail, don’t do that either. There’s no purpose in putting what you know is an incomplete post out there.
User avatar
mikeyww
Posts: 27193
Joined: 09 Sep 2014, 18:38

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 06:25

Code: Select all

#Requires AutoHotkey v2.0 ; Save script file as UTF-8 with BOM signature

F3:: {
 A_Clipboard := '', Send('^c')
 If ClipWait(1)
  Send dates(A_Clipboard)
 Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 48
}

dates(txt) {
 Static mo := 'Jan|Feb|Mär|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez'
 out := ''
 For each, line in StrSplit(txt, '`n', '`r') {
  part := StrSplit(line, ' ')
  If part.Length
   out .= Format('{}-{:02}-{:02u}`n', part[3], key(mo, SubStr(part[2], 1, 3)), part[1])
 }
 Return Trim(out, '`n')
}

key(str, val) {
 If pos := InStr('|' str '|', '|' val '|') {
  StrReplace(SubStr(str, 1, pos), '|',,, &n)
  Return n + 1
 }
}
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 08:09

@boiler I did not intend to post a blank post, and I am certain, after submitting I only edited minor points (spelling, omissions, better understanding). I don't recall anymore now. But the bulk of it I submitted in one go - maybe 99% :)

I usually prepare these post for a while having the editor window open, and preview the text frequently.

Somentimes I also hit "Full Editor & Preview" and get a message like "To few characters" or some such which I ignore and start typing my post.

So just to be clear: I do not post empty posts, and I submit my content mainly complete.

If there's a glitch in the forum software, let's find out :D
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 08:18

ItisI wrote:
03 Aug 2023, 08:09
Somentimes I also hit "Full Editor & Preview" and get a message like "To few characters" or some such which I ignore and start typing my post.
Actually I would have preferred to edit this line as it is not complete. It should read
-------------------------------------------------------------
When starting a new post I somentimes also hit "Full Editor & Preview" and get a message like "To few characters" or some such which I ignore and start typing my post.
-------------------------------------------------------------
This kind of editing is what I actually do. It makes things more understandable and/or easier to read.
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

gregster
Posts: 9087
Joined: 30 Sep 2013, 06:48

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 08:46

ItisI wrote:
03 Aug 2023, 08:09
So just to be clear: I do not post empty posts, and I submit my content mainly complete.

If there's a glitch in the forum software, let's find out :D
I can confirm boiler's observation. At first, your post was empty except the subject line (which got my attention, but I was too busy at the time to respond), and it was only after boiler's first post when more details were added.
As I see in the topic logs, your original post was also reported by another user for having no information in it. boiler closed that report shorty afterwards and then asked for more details. Then later, when it had more details, your post also had an automatic comment that it was edited by you.

Anyway, good to hear that you don't submit empty posts, at least not intentionally.
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 09:36

gregster wrote:
03 Aug 2023, 08:46
Anyway, good to hear that you don't submit empty posts, at least not intentionally.
A few weeks back I got a fleeting message about my name ("ItisI") being mentioned somewhere. It lasted for maybe 2 seconds, then disappeared. So I went back to my UCP to find it - no luck. It was in relation to a post whose author I recommended to use Google Translate because I couldn't understand what he/she was trying to say.

Otherwise my workflow is always

Post Reply -> Full Editor with Preview ->
Edit & Preview frequently <- this may take more than an hour!
Submit complete post
Edit oversights

I do not intentially submit empty posts.
---
edited once for typo
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 09:54

@mikeyww & @boiler
Thank you very much. I have sorted the input - black box - output and it works. I haven't really looked at the black box yet. When I do, I would like to ask my follow-up questions here. OK?

Code: Select all

F4:: {
    /*--------------------------------------------------------------
    Empty A_Clipboard
    Select complete list of dates (SelectAllCopy)
    Copy to "currentText" (SelectAllCopy)
    Return "CurrentText" (SelectAllCopy)
    Pass "currentText" to 'black box' "dates()"
    Safely fill A_Clipboard with result of "dates()" (SaveInsert)
    Paste Clipboard into file
    Inform Bob about the new discovery of a nephew :)
    --------------------------------------------------------------*/

    A_Clipboard := ""
    SaveInsert(dates(SelectAllCopy()))
    Send '^v'
    aTooting()
}
/*--------------------------------------------------------------
Here, There Be Dragons - courtesy of @mikeyww - thanks a lot
--------------------------------------------------------------*/
dates(txt) {
    Static mo := 'Jan|Feb|Mär|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez'
    out := ''
    For each, line in StrSplit(txt, '`n', '`r') {
        part := StrSplit(line, ' ')
        out .= Format('{}-{:02}-{:02u}`n', part[3], key(mo, SubStr(part[2], 1, 3)), part[1])
    }
    Return Trim(out, '`n')
}

key(str, val) {
    If pos := InStr('|' str '|', '|' val '|') {
        StrReplace(SubStr(str, 1, pos), '|', , , &n)
        Return n + 1
    }
}
/*--------------------------------------------------------------
End of Dragon
--------------------------------------------------------------*/
/*--------------------------------------------------------------
Selects all text
Copies the text to ClipBoard
Copies the Clipboard safely to "currentText"
Returns "currentText"
--------------------------------------------------------------*/

SelectAllCopy() {
    static currentText := ""
    A_Clipboard := ''
    Send('^a')
    Send('^c')
    If ClipWait(1) {
        currentText := A_Clipboard
    } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 48
    return currentText
}

/*--------------------------------------------------------------
Tooting for attention
--------------------------------------------------------------*/
aTooting() {
    SoundBeep(1760, 10)
    SoundBeep(880, 20)
    return
}
/*--------------------------------------------------------------
Transfers variable "myText" safely to A_Clipboard
--------------------------------------------------------------*/
SaveInsert(myText) {
    If ClipWait(2) {
        A_Clipboard := myText
        return

    } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 48
    aTooting()
    return
}

Started post around 16:40, previewed mayby 5-10 times, submitted around 16:53 complete
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
boiler
Posts: 17211
Joined: 21 Dec 2014, 02:44

Re: Transforming Dates to ISO-Dates

03 Aug 2023, 12:30

Thanks for confirming, @gregster. Yes, the main reason I saw the post at all was because it was reported as empty, so at least three of us saw it that way. As long as we’re all on the same page going forward, @ItisI, no harm done, however it may have occurred.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww and 37 guests