Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash time format HH:MM in 12 hour format AM/PM

I am trying to print out the time in AM / PM format with this code:

 #!/bin/bash
 while [[ $# -gt 0 ]]
  do
  key="$1"


  case $key in
  --AMPM| --ampm)
  while true;
  do
   #Time in AMPM format:
   echo $(date +"%r")
   sleep 1s;
   clear;
done
esac
done

I get this: HH:MM:SS I want to get this: HH:MM

How can i alter the code to do so ? or why doesnt it work ?

like image 725
AtSim1 Avatar asked Sep 06 '16 21:09

AtSim1


2 Answers

If you are using at least bash 4.2, you don't even need date:

printf '%(%I:%M %p)T\n'
like image 151
chepner Avatar answered Sep 23 '22 13:09

chepner


Format date output:

date +"%I:%M %p"
date +"%I:%M %P"

Where:

%I     hour (01..12)
%M     minute (00..59)
%p     locale's equivalent of either AM or PM; blank if not known
%P     like %p, but lower case
like image 26
Dave Grabowski Avatar answered Sep 24 '22 13:09

Dave Grabowski