Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using Perl and hand-crafted regexes instead of facilities like strptime: </p> <pre><code>#!/bin/perl -w while (&lt;&gt;) { # for date times that don't use leading zeroes, use this regex instead: # (?:\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(?::\d\d:\d\d) (AM|PM) while (m%(?:\d\d/\d\d/\d{4} )(\d\d)(?::\d\d:\d\d) (AM|PM)%) { my $hh = $1; $hh -= 12 if ($2 eq 'AM' &amp;&amp; $hh == 12); $hh += 12 if ($2 eq 'PM' &amp;&amp; $hh != 12); $hh = sprintf "%02d", $hh; # for date times that don't use leading zeroes, use this regex instead: # (\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(:\d\d:\d\d) (?:AM|PM) s%(\d\d/\d\d/\d{4} )(\d\d)(:\d\d:\d\d) (?:AM|PM)%$1$hh$3%; } print; } </code></pre> <p>That's very fussy - but also converts possibly multiple timestamps per line.</p> <p>Note that the transformation for AM/PM to 24-hour is not trivial.</p> <ul> <li>12:01 AM --> 00:01</li> <li>12:01 PM --> 12:01</li> <li>01:30 AM --> 01:30</li> <li>01:30 PM --> 13:30</li> </ul> <p>Now tested:</p> <pre><code>perl ampm-24hr.pl &lt;&lt;! 12/24/2005 12:01:00 AM 09/22/1999 12:00:00 PM 12/12/2005 01:15:00 PM 01/01/2009 01:56:45 AM 12/30/2009 10:00:00 PM 12/30/2009 10:00:00 AM ! 12/24/2005 00:01:00 09/22/1999 12:00:00 12/12/2005 13:15:00 01/01/2009 01:56:45 12/30/2009 22:00:00 12/30/2009 10:00:00 </code></pre> <hr> <p><em>Added</em>:</p> <p>In <a href="https://stackoverflow.com/questions/508752/whats-a-simple-way-to-convert-between-an-am-pm-time-to-24-hour-time-in-javascrip">What is a Simple Way to Convert Between an AM/PM Time and 24 hour Time in JavaScript</a>, an alternative algorithm is provided for the conversion:</p> <pre><code>$hh = ($1 % 12) + (($2 eq 'AM') ? 0 : 12); </code></pre> <p>Just one test...probably neater.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload