I learned from the question date - HH:MM:SS (with AM/PM) 12 Hour Timestamp for Bash Prompt - Unix & Linux Stack Exchange and fine-tune my prompt as:

export PS1='\D{%r}:\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]:\n$ '
08:52:31 PM:me@host:~:

The time format is not very comfortable, the result I desire is

   08:52PM me@host~:

How could I get such a HH:MM(AM/PM) format\

The final result

export  PS1='\D{%I:%M%p %d/%m %A}:\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]:\n$ '

10:45PM 18/03 Monday:me@host:~:

$ ls '$tty'

  • 1
    Why is the format in your question title different from what you actually want in the question? – Barmar Mar 18 at 15:48
  • 1
    @Alice You should not post answers in the question body. If you want to share what is the final solution that you used you can post your own answer. It's okay to answer your own question, if your answer adds something to the answers already present. – Bakuriu Mar 18 at 19:33

The supported formats are those documented in man date. So instead of

   %r     locale's 12-hour clock time (e.g., 11:11:04 PM)

you can use

   %I     hour (01..12)
   %M     minute (00..59)
   %p     locale's equivalent of either AM or PM; blank if not known

So change \D{%r} to \D{%I:%M%p}


Use \@ (the current time in 12-hour am/pm format) instead of \D{%r}, that produces times in HH:MM (AM|PM) format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.