1D1C - date

print or set the system date and time

date - print or set the system date and time

1. date command displays the current date and time

$ date date

2. To display the time in GMT/UTC time zone

$ date -u

date -u

3. To display the given date string in the format of date (MM DD YYY)

  • $ date --date="1/04/2020"

date --date="1/04/2020"

  • $ date --date="June 3 2000"

date --date="June 3 2000"

4. To display past dates

  • $ date --date="3 year ago"

image.png

  • $ date --date="5 hours ago"

image.png

  • $ date --date="1 month ago" image.png

  • $ date --date="2 week ago"

image.png

  • $ date --date="10 day ago"

image.png

To display future date

  • $ date --date="next wed"

image.png

  • $ date --date="next month"

image.png

  • $ date -date="2 day"

image.png

  • $ date --date="1 year"

image.png

To set the system date and time

  • $ date --set="Wed Apr 27 14:20:55 PDT 2022"

To display the date string present at each line of file in the date and time format

$ cat >> datefile
May 07 2022
Apr 03 2022
$ date --file=datefile

image.png

Date format options

  • %D: Display date as mm/dd/yy.
  • %d: Display the day of the month (01 to 31).
  • %a: Displays the abbreviated name for weekdays (Sun to Sat).
  • %A: Displays full weekdays (Sunday to Saturday).
  • %h: Displays abbreviated month name (Jan to Dec).
  • %b: Displays abbreviated month name (Jan to Dec).
  • %B: Displays full month name(January to December).
  • %m: Displays the month of year (01 to 12).
  • %y: Displays last two digits of the year(00 to 99).
  • %Y: Display four-digit year.
  • %T: Display the time in 24 hour format as HH:MM:SS.
  • %H: Display the hour.
  • %M: Display the minute.
  • %S: Display the seconds.
$ date +%[format-option]

$ date "+%D"
$ date "+%D %T"
$ date "+%A %B %d %T %y"
$ date "+%Y/%m/%d"
$ date "+%Y-%m-%d"

image.png

Credits:

Did you find this article valuable?

Support Makereading by becoming a sponsor. Any amount is appreciated!