How to format dates in shell scripts

in date time, shell scripts

When using dates in a shell script, you can format them like this:

$(date +[format])

Here are some examples:


$(date +”%Y%m%d)

20080508

——————–

$(date +”%Y-%m-%d)
2008-05-08

——————–

$(date +%D) which is shorthand equivalent for $(date +”%m/%d/%Y)

05/08/2008

To include time:

$(date +%r)

10:15:36 PM

——————–

$(date +%T)

22:15:36

A typical date format for a log entry:

$(date +”%Y-%m-%d %T”)

2008-05-29 14:31:34

To test a date format on the command line:

echo $(date +”%Y-%m-%d %T”)

2008-05-29 14:31:34

To see all the date format options, look at the man page for date:

man date

Comments on this entry are closed.

Previous post:

Next post: