Convert 24 hour military time to 12 hour time



  • This seems it would be simple, but I have not found any post anywhere on how to change in Notepad++, time from the 24 hour format to 12 hour format. Ideally doing so without having to manually edit each line. Is there a macro, or some plugin that could automate this a bit?

    Tue, 01 Jan 2019 21:30-23:30 10.3 Copper Canyon

    For example, in the above, I would like ideally the format to change to this below.
    Tue, 01 Jan 2019 9:30 pm - 11:30 pm



  • @Dawn-Juchum

    I don’t know about a plugin or macro to do this, but you could do a regular expression replacement. Here’s how:

    Press ctrl+h to bring up the Replace window.

    Put the following in the find what box:

    (?:(00)|(01)|(02)|(03)|(04)|(05)|(06)|(07)|(08)|(09)|(10)|(11)|(12)|(13)|(14)|(15)|(16)|(17)|(18)|(19)|(20)|(21)|(22)|(23))(:[0-5][0-9])

    Put the following in the replace with box:

    (?{1}12$25a)(?{2}1$25a)(?{3}2$25a)(?{4}3$25a)(?{5}4$25a)(?{6}5$25a)(?{7}6$25a)(?{8}7$25a)(?{9}8$25a)(?{10}9$25a)(?{11}10$25a)(?{12}11$25a)(?{13}12$25p)(?{14}1$25p)(?{15}2$25p)(?{16}3$25p)(?{17}4$25p)(?{18}5$25p)(?{19}6$25p)(?{20}7$25p)(?{21}8$25p)(?{22}9$25p)(?{23}10$25p)(?{24}11$25p)m

    Press the Replace button or the ReplaceAll button.

    Like Holiday Magic…it works.



  • @Dawn-Juchum
    don’t forget to select search mode: regular expression

    @Alan-Kilborn
    holy copy pasty … but it does work like magic ;-) i like 👍



  • @Meta-Chuh said:

    holy copy pasty … but it does work like magic ;-) i like 👍

    Since you liked that, I thought I’d have some more fun and do the inverse: am/pm to military

    find what: (?:(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)|(10)|(11)|(12))(:[0-5][0-9])(?:(a)|(p))m
    replace with: (?{12}(?{15}12:00))(?{1}(?{15}13:01))(?{2}(?{15}14:02))(?{3}(?{15}15:03))(?{4}(?{15}16:04))(?{5}(?{15}17:05))(?{6}(?{15}18:06))(?{7}(?{15}19:07))(?{8}(?{15}20:08))(?{9}(?{15}21:09))(?{10}(?{15}22:10))(?{11}(?{15}23:11))$13

    Of course, there might be a debate about leading zeros (or not) for single-digit hours. The OP didn’t give an example of that. But that kind of thing is an easy adjustment



  • @Alan-Kilborn

    nice 👍

    NOW all this leads to a new notepad++ feature request:
    ability to create replace macros which contain both search and replace strings and a custom title which can be selected via a dropdown menu, eg 12h to 24h, etc.
    i want to have this feature thanks to you ;-)

    btw: i’ve added (?!am|pm) for the military to 12 conversion, to avoid conversion of an already converted 12h am/pm string if one presses replace or replace all again.

    find what:
    (?:(00)|(01)|(02)|(03)|(04)|(05)|(06)|(07)|(08)|(09)|(10)|(11)|(12)|(13)|(14)|(15)|(16)|(17)|(18)|(19)|(20)|(21)|(22)|(23))(:[0-5][0-9])(?!am|pm)

    replace with:
    (?{1}12$25a)(?{2}1$25a)(?{3}2$25a)(?{4}3$25a)(?{5}4$25a)(?{6}5$25a)(?{7}6$25a)(?{8}7$25a)(?{9}8$25a)(?{10}9$25a)(?{11}10$25a)(?{12}11$25a)(?{13}12$25p)(?{14}1$25p)(?{15}2$25p)(?{16}3$25p)(?{17}4$25p)(?{18}5$25p)(?{19}6$25p)(?{20}7$25p)(?{21}8$25p)(?{22}9$25p)(?{23}10$25p)(?{24}11$25p)m

    btw2: i bet @guy038 can strip this regex down to only a few characters :D



  • @Meta-Chuh said:

    ability to create replace macros which contain both search and replace strings and a custom title which can be selected via a dropdown menu

    You already have this capability. Start macro recording. Do one of the above replacements. Stop macro recording. Name macro appropriately. Done. :)

    btw2: i bet @guy038 can strip this regex down to only a few characters

    I’m sure it can be shortened somewhat (they always can be!) but most of this one can’t. If regex could do math, then yes, but since regex can’t do math one has to live with the long length.



  • Amazing! Well done and thank you!



  • and thanks again @Alan-Kilborn for the idea of using macros for regex replaces.

    i’ve tried it out and played around, creating some of my favourite regex replaces even twice, one macro where i hit replace once, and another where i hit replace all, and it all works like a charm … it’s magic ;-) 👍



  • Hello, @dawn-juchum, @alan-kilborn, @meta-chuh and **All,

    As for me, I thought about a coding table, giving all the correspondences between the 24-hour format and the 12-hour format. This table is just a single line which must be the last one of the current file, even without a final line-break. This line is :

    12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23 # This LAST line must be ADDED at the END of THE CURRENT file


    Then, I created 3 S/R, using this final line :

    • The S/R A will replace any form 8:23 am , 8:23 AM , 8:23am , 8:23AM , 08:23 am , 08:23 AM , 08:23am or 08:23AM, preceded by a non-word char, with the 24-hour value 08:23

    • The S/R B will replace any 24-hour form, not followed with \x20?(am|pm|AM|PM), such as 08:23, with the 12-Hour value 08:23 am

    • The S/R C will replace any 24-hour form, not followed with \x20?(am|pm|AM|PM), such as 08:23, with the 12-Hour value 8:23 am

    Note that, between the B and C S/R, the Search part remains identical. Just, the Replace part is slightly modified !


    And the different S/R are :

    • A S/R :

    SEARCH (?si)\b(?:(0[0-9]|1[0-2])|(\d))(:[0-5][0-9])\x20?(a|p)m(?=.+(?-s)^.*(?:\1|0\2)\4\x20(\d\d).*\Z)

    REPLACE \5\3

    • B S/R :

    SEARCH (?si)\b([0-1][0-9]|2[0-3])(:[0-5][0-9])(?!\x20?[ap]m)(?=.+(?-s)^.*(?:(0)|1)(\d)(a|p)\x20\1.*\Z)

    REPLACE (?{3}0:1)\4\2\x20\5m

    • C S/R :

    SEARCH (?si)\b([0-1][0-9]|2[0-3])(:[0-5][0-9])(?!\x20?[ap]m)(?=.+(?-s)^.*(?:(0)|1)(\d)(a|p)\x20\1.*\Z)

    REPLACE (?{3}:1)\4\2\x20\5m


    So, given the initial text, below :

    12:00am
    12:04 am
    1:07am
    03:17 am
    5:23AM
    07:31AM
    9:43AM
    11:57 AM
    12:00 pm
    02:04pm
    4:15 pm
    06:22PM
    8:33 PM
    10:45PM
    11:59 PM
    .....
    .....
    .....
    .....
    .....
    .....
    .....
    12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23   # This LAST line must be ADDED at the END of THE CURRENT file 
    

    The A S/R, above, would give :

    00:00
    00:04
    01:07
    03:17
    05:23
    07:31
    09:43
    11:57
    12:00
    14:04
    16:15
    18:22
    20:33
    22:45
    23:59
    .....
    .....
    .....
    .....
    .....
    .....
    .....
    12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23   # This LAST line must be ADDED at the END of THE CURRENT file 
    

    If, against this resulting text, we run the B S/R, above, we obtain, this time :

    12:00 am
    12:04 am
    01:07 am
    03:17 am
    05:23 am
    07:31 am
    09:43 am
    11:57 am
    12:00 pm
    02:04 pm
    04:15 pm
    06:22 pm
    08:33 pm
    10:45 pm
    11:59 pm
    .....
    .....
    .....
    .....
    .....
    .....
    .....
    12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23   # This LAST line must be ADDED at the END of THE CURRENT file
    
    

    And, finally, if we consider the initial text :

    00:00
    00:04
    01:07
    03:17
    05:23
    07:31
    09:43
    11:57
    12:00
    14:04
    16:15
    18:22
    20:33
    22:45
    23:59
    .....
    .....
    .....
    .....
    .....
    .....
    .....
    12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23   # This LAST line must be ADDED at the END of THE CURRENT file 
    

    The C S/R would produce, the text below :

    12:00 am
    12:04 am
    1:07 am
    3:17 am
    5:23 am
    7:31 am
    9:43 am
    11:57 am
    12:00 pm
    2:04 pm
    4:15 pm
    6:22 pm
    8:33 pm
    10:45 pm
    11:59 pm
    .....
    .....
    .....
    .....
    .....
    .....
    .....
    12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23   # This LAST line must be ADDED at the END of THE CURRENT file 
    

    Of course, just get rid of the last line, temporarily added, once all the replacements are done !

    Best Regards,

    guy038

    P.S. :

    The drawback of my method is the necessity to add the line, with all the couples 12-Hour <–> 24-Hour, at the end of current file. However, you may combine the 3 successive S/R, below, in a single macro :

    • First, the following S/R, add the conversion table at the very end of the file

    SEARCH \z

    REPLACE \r\n12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23

    • Secondly, either, the A, B or C S/R performs the desired hour-format conversion

    • Finally, the following S/R, just get rid of the conversion table, at end of file :

    SEARCH (?-s).+\Z

    REPLACE Leave EMPTY


    For instance, in the Macros section of your active shortcuts.xml file, the initial demand of @dawn-juchum, the macro, named 24to12, could be stored as :

            <Macro name="24to12" Ctrl="no" Alt="no" Shift="no" Key="0">
                <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    
                <Action type="3" message="1601" wParam="0" lParam="0" sParam="\z" />
                <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                <Action type="3" message="1602" wParam="0" lParam="0" sParam="\r\n12a 00 01a 01 02a 02 03a 03 04a 04 05a 05 06a 06 07a 07 08a 08 09a 09 10a 10 11a 11 12p 12 01p 13 02p 14 03p 15 04p 16 05p 17 06p 18 07p 19 08p 20 09p 21 10p 22 11p 23" />
                <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
                <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    
                <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?si)\b([0-1][0-9]|2[0-3])(:[0-5][0-9])(?!\x20?[ap]m)(?=.+(?-s)^.*(?:(0)|1)(\d)(a|p)\x20\1.*\Z)" />
                <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                <Action type="3" message="1602" wParam="0" lParam="0" sParam="(?{3}:1)\4\2\x20\5m" />
                <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
                <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    
                <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s).+\Z" />
                <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
                <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
            </Macro>
    

    Now, I just realize that adding the @alan-kilborn or @meta-chuh single regexes, though longer, were as simple as my 3 consecutive S/R :-(

    Anyway, do as you like and consider that it just was an exercise to “strengthen” my brain ;-))



  • @guy038

    i like your kind of exercises (the reason why i thought about you right away when i saw this regex)

    and … if you keep training like this, you will probably be able to compile the code of notepad++ in your brain natively and hex-type the resulting .exe by hand ;-)

    seriously: best regards and as always both thumbs up ! 👍👍



  • @Alan-Kilborn how can I convert a 12-hour clock to 24-hour up to ms using this syntax? thanks




Log in to reply