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 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 !
Common pitfall and the correct rule:
A simple "if PM then add 12" produces 24 for 12:xx PM and leaves 12:xx AM wrong. The correct mapping is: 12 AM -> 00, 12 PM -> 12, and for PM hours other than 12 add 12. This also means you must preserve leading zeros (e.g. 09:01:01) and handle optional seconds or spaces before AM/PM.
A robust Perl approach (works with the semicolon-delimited lines shown in the thread — time is the 7th field):
;, match the time with a regex that accepts hh:mm[:ss][AM|PM] with optional whitespace, convert the hour using the rule above, zero-pad with sprintf, then join the fields back and write to a temp file before renaming. This avoids in-place corruption and keeps a backup if something goes wrong.Example Perl snippet:
use strict;
use warnings;
use File::Copy qw(move);
my $in = 'times.txt';
my $out = "$in.tmp";
open my $IN, '<', $in or die "open $in: $!";
open my $OUT, '>', $out or die "open $out: $!";
while (<$IN>) {
chomp;
my @f = split /;/;
if (defined $f[6] && $f[6] =~ m/^\s*(\d{1,2}):(\d{2})(?::(\d{2}))?\s*([AaPp][Mm])\s*$/) {
my ($h,$m,$s,$ampm) = ($1,$2, $3||'00', uc $4);
$h = int($h);
if ($ampm eq 'AM') { $h = 0 if $h == 12 }
else { $h += 12 if $h != 12 }
$f[6] = sprintf("%02d:%02d:%02d", $h, $m, $s);
}
print $OUT join(';', @f), "\n";
}
close $IN;
close $OUT;
move($out, $in) or die "move: $!"; Notes and troubleshooting:
File::Copy::move to atomically replace the original is safer than editing in-place.Jump to Post— Comatose 290#!/usr/bin/perl ($sec, $min, $hour, @junk) = localtime(time); print "$hour:$min$sec\n";
Jump to Post— Comatose 290like this in the file: 12:12:00? or like this: 12:12? Or like this: 12:12:00am?
Jump to Post— Comatose 290This 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 …
Jump to Post— Comatose 290Glad to have tried to help.... if you could have posted that information previously, I would have done a better job. Sorry for the miscommunication.
#!/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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.