How to convert 24 hour time to 12 hour timing?

Hi friends, I want to convert 24 hour timing to 12 hour please help me...

my data file looks like this..

13-Nov-2011    13:27:36    15.32044    72.68502
13-Nov-2011    12:08:31    15.31291    72.69807
16-Nov-2011    01:16:54    15.30844    72.74028
15-Nov-2011    20:09:25    15.35096    72.69842
16-Nov-2011    04:02:32    15.30826    72.65642
27-Nov-2011    13:08:25    15.48236    73.61669
13-Nov-2011    16:35:04    15.31626    72.68628
13-Nov-2011    15:17:47    15.31361    72.69728

I need output like this

27-Nov-2011 1:08:25 PM
awk ' {
 split($2,A,":");
 if(A[1]>12) {
  V=sprintf("%02d",A[1]-12);
  sub(/[0-9]+:/,V":",$2);
  $2=$2" PM";
 }
 else if(A[1]==12)
  $2=$2" PM";
 else if(A[1]==0) {
  sub(/[0-9]+:/,"12:",$2);
  $2=$2" AM"
 }
 else
  $2=$2" AM";
}1' OFS='\t' filename
1 Like

Try sth like this..

 
awk -F ":| +" '{if($2>12){t=sprintf("%02d", $2-12);print $1,t":"$3":"$4,"PM"}else{print $1,$2":"$3":"$4,"AM"}}' file
1 Like

The above suggestions don't strip off input fields 3 and 4. If you want something matching the output you said you want, try something like:

awk '{  split($2, df, /:/)
        $2 = sprintf("%d:%02d:%02d %sM",
                df[1] > 12 ? df[1] - 12 : df[1] ? df[1] : 12,
                df[2], df[3], df[1] >= 12 ? "P" : "A")
        print $1, $2
}' file

I misread Pamu's suggested script. It does strip off the last two input columns, but produces zero-filled hours where the requested output didn't have space or zero fill for hours less than 10.

1 Like
awk 'split($2,t,":") {if (t[1]>12) {print $1,t[1]-12":"t[2]":"t[3]" PM",$3,$4} else {print $1,t[1]+0":"t[2]":"t[3]" AM",$3,$4}}' file
13-Nov-2011 1:27:36 PM 15.32044 72.68502
13-Nov-2011 12:08:31 AM 15.31291 72.69807
16-Nov-2011 1:16:54 AM 15.30844 72.74028
15-Nov-2011 8:09:25 PM 15.35096 72.69842
16-Nov-2011 4:02:32 AM 15.30826 72.65642
27-Nov-2011 1:08:25 PM 15.48236 73.61669
13-Nov-2011 4:35:04 PM 15.31626 72.68628
13-Nov-2011 3:17:47 PM 15.31361 72.69728
1 Like

Thank you so much...all of you.

Hi Jotne:
Although a timestamp between midnight and 1am doesn't appear in the sample input, I assumed that any 24 hour input time was possible.

Your script will print 0:mm:ss AM rather than 12:mm:ss AM for such cases. And your script prints 12:mm:ss AM rather than 12:mm:ss PM for times between noon and 1pm.

awk -F ":|[ \t]+" '{i="AM"; h=$2+0} $2>12{h=$2-12} !$2{h=12} $2>11{i="PM"} {print $1,h":"$3":"$4,i}' file
awk -F ":|[ \t]+" '{print $1,($2 > 12 ? $2-12 : !$2 ? 12 : $2+0)":"$3":"$4,($2 > 11 ? "PM" : "AM")}' file

@Don Cragun
Thanks for pointing it out. We do use AM/PM her :slight_smile:
I see that Scrutinizer fix this by testing if its 0 then set it to 12 . !$2{h=12}

You're fortunate. When working with computers, 24 hour time makes processing much easier; having to convert to and from human readable form in locales that use AM/PM is a frequent source of errors. I also wish the US would convert to consistent metric measurements, but I doubt that will happen before I die. Oh, well.

1 Like

Stealing the AM/PM part from scrutinizer's proposal, I thought this might be interesting:

$ awk ' {print $1, ($2+11)%12+1 ":" $3 ":" $4, ($2>11?"PM":"AM")}' FS=":| *" file
13-Nov-2011 1:27:36 PM
13-Nov-2011 12:08:31 PM
16-Nov-2011 1:16:54 AM
etc...
1 Like

Nice, RudiC...

--
edit: ($2-1)%12+1 would also work..

Thanks!
Yes, but it would cast 0:00h to 0 not 12 AM.

edit: ($2-1)%12+1 would not work.. :slight_smile: