Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 12-hour date/time to 24-hour date/time

I have a tab delimited file where each record has a timestamp field in 12-hour format:

mm/dd/yyyy hh:mm:ss [AM|PM].

I need to quickly convert these fields to 24-hour time:

mm/dd/yyyy HH:mm:ss.

What would be the best way to do this? I'm running on a Windows platform, but I have access to sed, awk, perl, python, and tcl in addition to the usual Windows tools.

like image 814
Patrick Cuff Avatar asked Jan 13 '09 17:01

Patrick Cuff


People also ask

How do I convert a date to 24-hour format?

We can change the pattern in the SimpleDateFormat for the conversion. The pattern dd/MM/yyyy hh:mm:ss aa is used for the 12 hour format and the pattern MM-dd-yyyy HH:mm:ss is used for the 24 hour format. In this program we are changing the format of the date by changing the patterns and formatting the input date.


3 Answers

To just convert the hour field, in python:

def to12(hour24):
    return (hour24 % 12) if (hour24 % 12) > 0 else 12

def IsPM(hour24):
    return hour24 > 11

def to24(hour12, isPm):
    return (hour12 % 12) + (12 if isPm else 0)

def IsPmString(pm):
    return "PM" if pm else "AM"

def TestTo12():    
    for x in range(24):
        print x, to12(x), IsPmString(IsPM(x))

def TestTo24():
    for pm in [False, True]:
        print 12, IsPmString(pm), to24(12, pm)
        for x in range(1, 12):
            print x, IsPmString(pm), to24(x, pm)
like image 56
Erik Avatar answered Nov 07 '22 03:11

Erik


It is a 1-line thing in python:

time.strftime('%H:%M:%S', time.strptime(x, '%I:%M %p'))

Example:

>>> time.strftime('%H:%M:%S', time.strptime('08:01 AM', '%I:%M %p'))
'08:01:00'
>>> time.strftime('%H:%M:%S', time.strptime('12:01 AM', '%I:%M %p'))
'00:01:00'
like image 36
Biswanath Avatar answered Nov 07 '22 03:11

Biswanath


Using Perl and hand-crafted regexes instead of facilities like strptime:

#!/bin/perl -w
while (<>)
{
    # 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' && $hh == 12);
        $hh += 12 if ($2 eq 'PM' && $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;
}

That's very fussy - but also converts possibly multiple timestamps per line.

Note that the transformation for AM/PM to 24-hour is not trivial.

  • 12:01 AM --> 00:01
  • 12:01 PM --> 12:01
  • 01:30 AM --> 01:30
  • 01:30 PM --> 13:30

Now tested:

perl ampm-24hr.pl <<!
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

Added:

In What is a Simple Way to Convert Between an AM/PM Time and 24 hour Time in JavaScript, an alternative algorithm is provided for the conversion:

$hh = ($1 % 12) + (($2 eq 'AM') ? 0 : 12);

Just one test...probably neater.

like image 24
Jonathan Leffler Avatar answered Nov 07 '22 05:11

Jonathan Leffler