• Login
Community
  • Login

Convert 24 hour military time to 12 hour time

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
12 Posts 5 Posters 3.2k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D
    Dawn Juchum
    last edited by Jan 1, 2019, 11:31 PM

    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

    M 1 Reply Last reply Jan 2, 2019, 3:26 AM Reply Quote 0
    • A
      Alan Kilborn
      last edited by Jan 2, 2019, 2:43 AM

      @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.

      1 Reply Last reply Reply Quote 3
      • M
        Meta Chuh moderator @Dawn Juchum
        last edited by Jan 2, 2019, 3:26 AM

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

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

        A 1 Reply Last reply Jan 2, 2019, 2:17 PM Reply Quote 1
        • A
          Alan Kilborn @Meta Chuh
          last edited by Alan Kilborn Jan 2, 2019, 2:18 PM Jan 2, 2019, 2:17 PM

          @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

          M S 2 Replies Last reply Jan 2, 2019, 5:48 PM Reply Quote 4
          • M
            Meta Chuh moderator @Alan Kilborn
            last edited by Jan 2, 2019, 5:48 PM

            @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

            A 1 Reply Last reply Jan 2, 2019, 6:44 PM Reply Quote 1
            • A
              Alan Kilborn @Meta Chuh
              last edited by Alan Kilborn Jan 2, 2019, 6:45 PM Jan 2, 2019, 6:44 PM

              @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.

              M 1 Reply Last reply Jan 3, 2019, 8:31 AM Reply Quote 1
              • D
                Dawn Juchum
                last edited by Jan 3, 2019, 1:08 AM

                Amazing! Well done and thank you!

                1 Reply Last reply Reply Quote 2
                • M
                  Meta Chuh moderator @Alan Kilborn
                  last edited by Jan 3, 2019, 8:31 AM

                  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 ;-) 👍

                  1 Reply Last reply Reply Quote 3
                  • G
                    guy038
                    last edited by guy038 May 17, 2019, 8:05 PM Jan 4, 2019, 3:50 PM

                    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 ;-))

                    M 1 Reply Last reply Jan 4, 2019, 6:28 PM Reply Quote 3
                    • M
                      Meta Chuh moderator @guy038
                      last edited by Jan 4, 2019, 6:28 PM

                      @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 ! 👍👍

                      1 Reply Last reply Reply Quote 0
                      • S
                        Sophia Cruz @Alan Kilborn
                        last edited by May 17, 2019, 4:45 PM

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

                        1 Reply Last reply Reply Quote 0
                        • A
                          Alan Kilborn
                          last edited by May 17, 2019, 5:15 PM

                          Related cross-link: https://notepad-plus-plus.org/community/topic/17649/12-hour-to-24-hour-clock-conversion

                          1 Reply Last reply Reply Quote 2
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors