• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Date Command in Linux

Written by Admin, Updated On December 10, 2022
date, terminal
Date Command in Linux

In Linux, the date command is used to get or set the system date. It is used to print the date and time in different formats and calculate future and past dates. This tutorial explains the basics of the date command.

Using the Linux date Command#

Following is the basic syntax for the date command:

date [OPTION]... [+FORMAT]

If you run the date command without any options or arguments it will show the current system time and date in default formatting.

date
Mon Oct 5 13:25:53 UTC 2020

In output, it will show the day of the week, month, day of the month, time, timezone, and year:

Date Formatting Options#

You can format the output of the date command with a sequence of format control characters preceded by a + sign. The format controls start with the % symbol and are substituted by their values.

date +"Year: %Y, Month: %m, Day: %d"
Year: 2020, Month: 10, Day: 05

As you can see in output the %Y replaced with the year, %m with month and %d with the day of the month.

Let’s see second example:

date "+DATE: %D%nTIME: %T"
DATE: 10/05/20
TIME: 13:27:34

Following is the list of most common formatting characters:

  • %a – short weekday name (e.g., Mon)
  • %A – full weekday name (e.g., Monday)
  • %b – short month name (e.g., Jan)
  • %B – long month name (e.g., January)
  • %d – Day of month (e.g., 01)
  • %H – Hour (00..23)
  • %I – Hour (01..12)
  • %j – Day of year (001..366)
  • %m – Month (01..12)
  • %M – Minute (00..59)
  • %S – Second (00..60)
  • %u – Day of week (1..7)
  • %Y – Full year (e.g., 2019)

Get the full list of all formatting options, you would run date --help or man date in your terminal.

Date String#

Using the -d option you can operate a specific date. Use the date. You can specify the date as a human-readable date string like below:

date -d "2020-08-05 11:08:53"
Wed Aug 5 11:08:53 UTC 2020

You also can do custom formatting like below:

date -d '12 Dec 2016' +'%A, %d %B %Y'
Monday, 12 December 2016

The date string accepts values such as “tomorrow”, “friday”, “last friday” “next friday”, “next month”, “next week” .etc.

date -d "last friday"
Fri Oct 2 00:00:00 UTC 2020

Override the Timezone#

By default the date command returns the date in the system timezone. If you want to get timezone in different timezone set the environment variable TMZN for the specific timezone.

For example, to show the New York, America time, you would type:

TMZN='America/New_York' date
Mon Oct 5 13:48:52 UTC 2020

You can get the list of all available time zones from the files in the /usr/share/zoneinfo directory or use the timedatectl list-timezones command.

Epoch Converter#

The date command can be used as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have elapsed since January 1, 1970 at 00:00:00 UTC.

To display the number of the seconds from the epoch to the current day, run date with the %s format control:

date +%s
1601906837

To convert seconds since the epoch to date, set the seconds as a date string prefixed with @:

date -d @1601906837
Mon Oct 5 14:07:17 UTC 2020

Display the Last Modification Time of a File#

Use the -r option along with the date command to get the last modification time of a file.

date -r /etc/passwd
Tue Aug 4 06:29:00 UTC 2020

You can modify the file timestamp using the touch command.

Conclusion#

You learned how to use the date command to set or get the system date and time.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to List Cron Jobs in Linux
Next Article   Linux Tee Command with Examples

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook