Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Date Command in Linux - DesignLinux

Oct 10 2020
designlinux 0 Comments
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.

Related

Tags: date, terminal

How to Install vnStat and vnStati to Monitor Network Traffic in Linux

Prev Post

How to Get and Change the Current Working Directory in Python

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 464
  • 606,214

DesignLinux.com © All rights reserved

Go to mobile version