12-hour to 24-hour clock conversion
-
I want to convert a 12-hour clock to 24-hour clock up to the ms.
Example: Today is 12:00:00:001 AM and it’s raining.
Output: Today is 00:00:00:001 and it’s raining. -
check regular expression in replace dialog and use
find what:(\d{1,2})(:\d{1,2}:\d{1,2}:\d{1,3}) AM
and replace with00\2 -
@Ekopalypse thanks for the answer but what if it’s multiple numbers in AM and PM, and the separator will be from . to :? Regex won’t work for other numbers.
Example:
01.25.31.934 PM -> 13:25:31:934
04.02.28.421 AM -> 04:02:28:421
09.55.03.732 PM -> 21:55:03:732 -
would you mind clarifying the exact format of your data as this is essential for
regular expressions to work correctly. For example, in the previous request you used :
to separate time fields and now you are using a dot. -
-
@Ekopalypse I’m sorry I wasn’t clear with my question. I’m really looking for an answer for my 2nd request. I posted the 1st one because I was still trying different regex which would answer my 2nd request and I was hoping I could formulate something with the answer that you provided.
-
@Alan-Kilborn thank you for this link but I already tried the answers provided but I was only able to convert am up to minutes using . as a separator.
-
It wasn’t for you, it was for future searchers of this site that may find this topic when what they are really looking for is the other way around. :)
BTW if Eko doesn’t solve your problem here in a reasonable amount of time, I’ll jump in. :)
-
Alan, LOL, please feel free to do so - you are much more in this regex then I’m.
-
This seems to do it:
Find:
^(?:(01)|(02)|(03)|(04)|(05)|(06)|(07)|(08)|(09)|(10)|(11)|(12))\.(\d\d)\.(\d\d)\.(\d{3}) (?:(A)|(P))M
Repl:(?{12}?{16}00:12)(?{1}?{17}13:01)(?{2}?{17}14:02)(?{3}?{17}15:03)(?{4}?{17}16:04)(?{5}?{17}17:05)(?{6}?{17}18:06)(?{7}?{17}19:07)(?{8}?{17}20:08)(?{9}?{17}21:09)(?{10}?{17}22:10)(?{11}?{17}23:11):$13:$14:$15
Search mode: Regular expressionI tried to be “fancy” and do the “AM” and “PM” part with “named groups” so that the replacement is more “readable”, but I ran into some trouble with that. If I get it working, and it truly is more readable, I may post that as well…
-
I got it to work with named groups. The problem was, and it makes it less elegant, is that mixing numbered and named capture groups seems to mess things up. So I had to artificially name group # 1 as “one” instead of just using its number. I’ll let the reader decide if this version is more readable than the previous:
Find:
^(?:(?<one>01)|(?<two>02)|(?<three>03)|(?<four>04)|(?<five>05)|(?<six>06)|(?<seven>07)|(?<eight>08)|(?<nine>09)|(?<ten>10)|(?<eleven>11)|(?<twelve>12))\.(?<mins>\d\d)\.(?<secs>\d\d)\.(?<ms>\d{3}) (?:(?<am>A)|(?<pm>P))MRepl:
(?{twelve}?{am}00:12)(?{one}?{pm}13:01)(?{two}?{pm}14:02)(?{three}?{pm}15:03)(?{four}?{pm}16:04)(?{five}?{pm}17:05)(?{six}?{pm}18:06)(?{seven}?{pm}19:07)(?{eight}?{pm}20:08)(?{nine}?{pm}21:09)(?{ten}?{pm}22:10)(?{eleven}?{pm}23:11):$+{mins}:$+{secs}:$+{ms}Search mode: Regular expression
(And I fully expect a “holy cow batman!” from @Meta-Chuh on that one!)
(And yes, doing 12 differently from the others in the replace was intentional, as it is a little “out of place” in the conversion)
-
LOL - if I could I would upvote it 12 times. :-D
-
Sometimes things get upvoted too quickly for people to have tried it out to verify/duplicate results. This is somewhat bad as if something doesn’t work (and the thread dies right there) it looks to future readers like a good solution…
BTW I think the “replace” is very readable with the named groups, but the “find” suffers…slightly…
-
:-D for me this looks like what we call - bohemian villages.
But I can participate on that thread now - I’ve tested it, and it looks good to me :-)
Let’s see what the OP thinks about it. -
@Alan-Kilborn it works perfectly. Thank you so much!
-
2nd solution is less confusing but both still produces the desired output. :D
-
holy cow batman! 😄👍
Sometimes things get upvoted too quickly for people to have tried it out to verify/duplicate results.
i guess sometimes your writing style is so worth an upvote, that people give you one, even if they have tested it’s content to fail.
greetings,
robin -
@Meta-Chuh said:
your writing style…worth an upvote…
My writing style? Nah. I don’t have the flowery prose of a @PeterJones or the diplomatic panache of a @Meta-Chuh . [Basically I don’t wanna put that kind of effort in–to the typing part–obviously with the above regexes staring us in the face I’ll put a little time/effort into THAT aspect. Trying to provide some hopefully accurate help, maybe without the greatest english composition accompanying.]
-
Hi, @sophia-cruz, @alan-kilborn, @eko-palypse, @meta-chuh and All,
Here is a variant of the Alan’s solution, which is less restrictive :
-
It just needs that the hour should be located after a non-word, instead of the beginning of line, due to the
\bsyntax -
It does not care of the synbol between the different parts of the hour ( just changing the
\.syntax by.! ) -
It allows the milliseconds part to be absent (
.MMM), in the hour, with the(?:.(\d{3}))?syntax -
It allows the
AMorPMpart to come next to the digits, with\x20? -
It does not care of the case , allowing the four syntaxes
am,pm,AMandPMwith the regex(?i:(AM)|PM)
Note :
In the remplacement regex, when group17exists (AM), I just rewrite the hour part (\1), except when hour =12, where the logic is reversed !
So, assuming this sample text :
12.00.47 AM 12.58.18.387 am 01 07 28 421 AM 03:17:31:934am 05.43.26.582 AM 07:23:03.732 am 09.31.08AM 11.57.59,003 am ~~~~~~~~~~~~~~~~~~~~ 12.00.45 PM 12.56.49.203 pm 01 04 51 387 PM 03:15:33:632pm 05.22.00.814 PM 07:33:55.548 pm 09.45.26PM 11.59.11,247 pmWith the regex S/R below :
SEARCH : \b(0(?:(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))|(10)|(11)|(12)).(\d\d).(\d\d)(?:.(\d{3}))?\x20?(?i:(AM)|PM) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <-- Groups REPLACE : (?2(?17\1:13))(?3(?17\1:14))(?4(?17\1:15))(?5(?17\1:16))(?6(?17\1:17))(?7(?17\1:18))(?8(?17\1:19))(?9(?17\1:20))(?10(?17\1:21))(?11(?17\1:22))(?12(?17\1:23))(?13(?{17}00:\1)):$14:$15(?16\:$16)We would obtain :
00:00:47 00:58:18:387 01:07:28:421 03:17:31:934 05:43:26:582 07:23:03:732 09:31:08 11:57:59:003 ~~~~~~~~~~~~~~~~~~~~ 12:00:45 12:56:49:203 13:04:51:387 15:15:33:632 17:22:00:814 19:33:55:548 21:45:26 23:59:11:247Best Regards,
guy038
-
-
Hi, All,
When I woke up this morning, I immediately saw a simplification ( in length ! ) of the replacement regex ;-))
So, my last version is :
SEARCH
\b(0(?:(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))|(10)|(11)|(12)).(\d\d).(\d\d)(?:.(\d{3}))?\x20?(?i:(AM)|PM)REPLACE
(?13(?{17}00:\1):(?17\1:(?{2}13)(?{3}14)(?{4}15)(?{5}16)(?{6}17)(?{7}18)(?{8}19)(?{9}20)(?{10}21)(?{11}22)(?{12}23))):$14:$15(?16\:$16)BR
guy038
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login