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 !

Recommended Answers

All 15 Replies

#!/usr/bin/perl

($sec, $min, $hour, @junk) = localtime(time);

print "$hour:$min$sec\n";

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

like this in the file: 12:12:00? or like this: 12:12? Or like this: 12:12:00am?

12:12:00 AM<- like this.

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);

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

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";

Hey dude.

Thanks babe.

:D

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.

That's 24 hour format right? 12 is noon, and 0 is midnight.

The answer is on the other forum

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.