51

I want to add a timestamp to my command prompt in oh-my-zsh. This is the current theme prompt (robbyrussell):

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ %s)"
PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

ZSH_THEME_GIT_PROMPT_PREFIX="git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

Any idea how can add the timestamp?

6 Answers 6

69

According to the zshmisc man page there are several % codes for date and time, eg:

 %D     The date in yy-mm-dd format.
 %T     Current time of day, in 24-hour format.
 %t %@  Current time of day, in 12-hour, am/pm format.
 %*     Current time of day in 24-hour format, with seconds.
 %w     The date in day-dd format.
 %W     The date in mm/dd/yy format.
 %D{strftime-format}

The last one allows codes listed in the strftime(3) man page. Edit your ~/.zshrc file and add at the end a new PROMPT value, eg:

 PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} %D %T % %{$reset_color%}'
2
  • This doesn't stay after I close the terminal window. I have to do source ~/.zsh every time how do I avoid that?
    – user391339
    Commented Oct 24, 2019 at 22:59
  • 2
    There was a typing mistake in my answer. The file to use is ~/.zshrc not ~/.zsh.
    – meuh
    Commented Oct 25, 2019 at 6:07
56

If you want add date/time to the right, you set RPROMPT

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
RPROMPT="[%D{%y/%m/%f}|%@]"

enter image description here

5
  • 2
    its definitely goes better on the right side since I already have a lot of stuff on the left prompt
    – WhoSayIn
    Commented Mar 9, 2018 at 13:30
  • 2
    in my case nano ~/.zshrc and added RPROMPT="%{$fg[yellow]%}[%D{%f/%m/%y}|%@]" as last line to set right prompt with yellow color Commented Jun 25, 2019 at 17:32
  • 4
    @VictorR.Oliveira you would want to add %{$reset_color%} at the end to avoid having the yellow messing up the rest of the lines.
    – Raf
    Commented Nov 24, 2019 at 18:22
  • 3
    +1 for RPROMPT
    – ThisaruG
    Commented Mar 4, 2020 at 7:47
  • Is it possible to set the time zone to something else with this?
    – ADataGMan
    Commented Dec 3, 2022 at 2:13
11

I added this %D{%m/%f/%y}|%D{%L:%M:%S} to the main theme to display the date and time as mm/dd/yy | hh:mm:ss.

So here is the full command I use:

PROMPT='%D{%m/%f/%y}|%D{%L:%M:%S} ${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}${NEWLINE}$ '

Which gives:

enter image description here

5

To get full datetime and zone

[Sun 11 Aug 2019 20:41:53 AEST]

I place

RPROMPT="%{$fg[green]%}[%D{%c}]"

in my ~/.zshrc file

3

Add the following at the end of your .zshrc to get a similar output as shown in the GIF.

PROMPT='%{$fg_bold[green]%}➜% %{$fg_bold[blue]%} [%D{%L:%M:%S}]% ${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$reset_color%}'
TMOUT=1
TRAPALRM() {
    zle reset-prompt
}

GIF: live-time-oh-my-zsh.gif

2

This is what worked for me

%D{%b %d, %Y - %T}

Format: Aug 10, 2020 - 22:26:54

Reference: https://man7.org/linux/man-pages/man1/date.1.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .