List of the format for date commands in Bash Script
Posted on:February 27, 2023 at 10:00 AM
In Bash, you can use the date
command to format dates and times in a variety of ways. Here are some common format codes that you can use with the date
command:
%a
: abbreviated weekday name (e.g. “Sun”)%A
: full weekday name (e.g. “Sunday”)%b
: abbreviated month name (e.g. “Jan”)%B
: full month name (e.g. “January”)%c
: locale’s date and time (e.g. “Sun Jan 22 11:33:01 2017”)%C
: century (e.g. “20” for the 21st century)%d
: day of the month (e.g. “01”)%D
: date in the format “mm/dd/yy”%e
: day of the month, with a leading space for single-digit days (e.g. ” 1” for January 1st)%F
: date in the format “yyyy-mm-dd”%h
: same as %b%H
: hour (24-hour format, e.g. “23”)%I
: hour (12-hour format, e.g. “11”)%j
: day of the year (e.g. “022” for January 22nd)%k
: hour, with a leading space for single-digit hours (e.g. ” 1” for 1am)%l
: hour, with a leading space for single-digit hours (12-hour format, e.g. ” 1” for 1am)%m
: month (e.g. “01” for January)%M
: minute (e.g. “33”)%n
: newline character%p
: AM/PM indicator%r
: time in the format “hh:mm:ss AM/PM”%R
: time in the format “hh:mm”%s
: seconds since the Unix Epoch (January 1, 1970)%S
: seconds (e.g. “01”)%t
: tab character%T
: time in the format “hh:mm:ss”%u
: day of the week (1 for Monday, 7 for Sunday)%U
: week number of the year, with Sunday as the first day of the week%V
: week number of the year, with Monday as the first day of the week%w
: day of the week (0 for Sunday, 6 for Saturday)%W
: week number of the year, with Monday as the first day of the week%x
: locale’s date (e.g. “01/22/17”)%X
: locale’s time (e.g. “11:33:01”)%y
: year without century (e.g. “17” for 2017)%Y
: year with century (e.g. “2017”)%z
: timezone offset (e.g. “-0800”)%Z
: timezone name (e.g. “PST”)
You can use these codes to format dates and times in any way you like using the date command in Bash. For example, to display the current date in the format “Weekday, Month Day, Year”, you could use the following command:
date +"%A, %B %d, %Y"
This would output something like “Sunday, February 27, 2023”.