i wana know how to change time format from 12hr format to 24 hr format..

i browsed thru many websites and cannot find the answer to it..

played around with the codes and cant get the output right..

help !

I have a file that contains a time in each line..
The time in each line is in 12hour format..
How to change it to 24hour format..

This doesn't actually change it in the file.... but it writes a new file, you can just easily change the variable on the second open to be the file it read from, however. I'm sure that Rashakil or Narue will show you a much more effecient method, but for now this works:


$timefile = "times.txt";
$newfile = "newtimes.txt";

open(FH, "$timefile");
        while (<FH>) {
                ($tm, $pod) = split(/ /, $_);
                ($hr, $min, $sec) = split(/:/, $tm);
                if ($pod eq "PM") {
                        $hr = $hr + 12;
                        $tm = "$hr:$min:$sec";
                } else {
                        $tm = "$hr:$min:$sec";

                push @new_times, $tm;

open(FH, ">$newfile");
        foreach $ntime (@new_times) {
                print FH  "$ntime\n";

I'm confused.

Do you wanna see what's inside the text file?

Information goes as below:
Open;Nanyang Poly (527/CHYP);2565389;Opened;SW;20060224;09:01:01AM;100000756;RS_GROUP;SG FS SGP;;3738166;4992484;Medium
FW;Nanyang Poly (527/CHYP);2565389;In Planning;SW;20060224;09:01:05AM;100000756;RS_GROUP;SG FS SGP;;3738166;4992484;Medium
FW;Nanyang Poly (527/CHYP);2565389;In Planning;SW;20060224;09:01:21AM;100002324;RS_EMPLOYEE;Batalon, Mr. Michael;;3738166;4992484;Medium
FW;Nanyang Poly (527/CHYP);2565389;In Planning;SW;20060224;09:01:21AM;100002324;RS_EMPLOYEE;Batalon, Mr. Michael;;3738166;4992484;Medium
FW;Nanyang Poly (527/CHYP);2565389;Assigned;SW;20060224;09:01:26AM;100002324;RS_EMPLOYEE;Batalon, Mr. Michael;;3738166;4992484;Medium
Open;Toa Payoh (385/CHTP);2565401;Opened;SW;20060224;09:40:48AM;100000756;RS_GROUP;SG FS SGP;;3738171;4992494;Medium
FW;Toa Payoh (385/CHTP);2565401;In Planning;SW;20060224;09:40:55AM;100000756;RS_GROUP;SG FS SGP;;3738171;4992494;Medium

Hey Comatose.
Just to tell you that i have solved the problem with the help of someone from elsewhere
But thanks anyway Comatose..
At least you tried to help.
Have a nice day!

Glad to have tried to help.... if you could have posted that information previously, I would have done a better job. Sorry for the miscommunication.

My bad.

Sorry for the miscommunication.

I have gotten a minor problem.

For 12:48:00 PM, it is converted to 24:48:00 hrs.

By right it should be converted to 00:48:00 hrs.

How you go about doing it?

just stick an if in the code...for example:

($hr, $min, $sec) = split(/:/, $timevariable);

if ($hr eq "24") {
     $hr = "00";

$timevariable = "$hr:$min$sec";


I found a minor problem. Does this convert correctly??

($hr, $min, $sec) = split(/:/, $timevariable);

if ($hr eq "24") {
$hr = "12";

$timevariable = "$hr:$min$sec";

12 PM = 12 hrs??
12 AM = 0 hrs??

im kinda confused between the time format alrdy.


Abit impatient thats why i posted on three forums.

I realised only 2 forums replied.

My virgin thread.

Please understand.


Sorry for any inconvenience caused.

This article has been dead for over six months. Start a new discussion instead.