0

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 !

3
Contributors
15
Replies
16
Views
11 Years
Discussion Span
Last Post by ivernrawks
0

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

0

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:

#!/usr/bin/perl

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

open(FH, "$timefile");
        while (<FH>) {
                chomp;
                ($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;
        }
close(FH);

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

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

0

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!
Cheers!
:D

0

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.

0

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?

0

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

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

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

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

Hahaha..

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.

0

Okay.

Abit impatient thats why i posted on three forums.

I realised only 2 forums replied.

My virgin thread.

Please understand.

Haha.

Sorry for any inconvenience caused.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.