List of the format for date commands in Bash Script

3 months ago
151 views

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".

Thouhedu Islam Suchi

Thouhedul Islam

Laravel Developer

Hi, I am a Laravel Developer with a passion for web development. I specialize in PHP, particularly Laravel, as well as Javascript, MySql, and other cutting-edge web technologies.

During the day, I work for GLEIF, where I apply my skills and experience to develop innovative web applications. At night, I enjoy contributing to the community and solving problems. You can often find me on Laracasts, where I love to stay active and keep up-to-date with the latest trends in web development.

I am excited about the potential of technology and its power to transform people's lives.