EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Linux Date

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Linux Date

Linux Date

Introduction to Linux Date

In the Linux ecosystem, the date is used to print or display the current date and system time zone. With the help of date command, we can print the date and time in a different format. The date commands provide the liberty to print the past and future dates also. The date command utility was written by David MacKenzie.

Syntax of Date Command:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

date [OPTION]... [+FORMAT]

  • date: We can use the date keyword in the syntax or command. It will take the two-argument as an option and format type (to which we need to print the date). Provide the end result with the requested format date.
  • OPTION: We can provide the different flags as options that are compatible with the date command.
  • FORMAT: As per the requirement, we can pass the different types of date format to the date command.

How Linux Date Command works?

The date command is able to print different type date format. By default, the date command will print the date as per the time zone which is configured in the UNIX or Linux operating system. We can also change the system time zone and print the different time zone date. For changing the time zone, you should have the root privileges. Generally, the date command will take the two set of arguments like option and date format. As per the inputs provided to it, it will manipulate the data and provide the date and time output in the request format.

Below is the list of formatting keywords which is commonly use with date command:

  • %a: It will display the weekday name example: Mon.
  • %A: It will display the full weekday name example: Monday.
  • %b: It will display the short month name example: Jan.
  • %B: It will display the long month name example: January.
  • %d: It will display the date of month example: 01.
  • %H: It will display the hour digit in 24hr format.
  • %I: It will display the hour digit in 12hr format.
  • %j: It will display the date of the year example: 001..366.
  • %m: It will display the month in digit format example: 01..12.
  • %M: It will display the minute value example: 00..59.
  • %S: It will display the second value example: 00..60.
  • %y: It will display the last two digit of year example: 20.
  • %Y: It will display the year example: 2020.

Examples of Linux Date

Given below are the examples mentioned:

Example #1

Date Command

It is very simple and common date command in a Linux environment. It will print the default date format of the system.

Code:

date

Explanation:

  • As per the above command, we are able to print the system time and date.
  • It will print the day (in short format), month (in short format), date, time (include the hour, minute, seconds), time zone (IST), year (2020).

Output:

Linux Date 1

Example #2

Date String

a. Date Command – Custom Format

The date command is very flexible, we can provide the different custom inputs to the date command and it will print the date format accordingly. We are having the privileges to change the default date format to get the result in own format.

Code:

date -d '21 Aug 1993' +'%A -> %d %B - %Y'

Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,008 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Explanation:

  • As per the below screenshot, we have print the custom date. As per the requirement, we can add the different special character also.

Output:

Custom Format

b. Date Command – String Format

In date command, we are having the functionality to pass the string parameters. Accordingly, the date command will print the output. It will not require to follow the date input format.

Code:

date -d "last month"

Explanation:

  • We can add the string option with the date command. It is a quick way to display the dates.

Output:

String Format

Example #3

Override the Time Zone

a. Date Command – With Different Time Zone

In date command, we are able to display the time and date in different time zones.

Code:

TZ='Africa/Djibouti' date

Explanation:

  • Using “timedatectl list-timezones” command, we are able to find the list of time zones available in the Linux.
  • As per the requirement, we can choose any time zone and pass the values to the TZ variable.

Output:

Linux Date 4

b. Date Command – Use Epoch Time Converter

The epoch timestamp is also called as the UNIX timestamp. In this conversion, the date command is to use the epoch converter. It will calculate the number of seconds that have elapsed from January 1, 1970 at 00:00:00 UTC.

Code:

date +%s

Explanation:

  • We are able to calculate the time in between the epoch timestamp and the current timestamp. The time difference will be in seconds.

Output:

Use Epoch

c. Date command – With File Operation

In the Linux environment, we can use the date command in a different way. Here, we are able to find the last modification time of the file. To get this timestamp, we need to use the “-r” option with the command.

Code:

date -r /etc/passwd

Explanation:

  • As per the below screenshot, we are able to find the last modification time of the file “/etc/passwd”.

Output:

Linux Date 6

d. Date Command – Display Past Date

In date command, we are having the functionality to display the past date in different formats.

Code:

date --date="2 day ago"

Explanation:

  • As per the above command, we are able to print the past 2 day’s date and time.

Output:

Linux Date 7

e. Date Command – Display Future Date

In date command, we are having the functionality to display the future date in different formats.

Code:

date --date="2 day"

Explanation:

  • As per the above command, we are able to print the future 2 day’s date and time.

Output:

Linux Date 8

f. Date Command – Set System time and Date

With the help of date command, we are able to set the required time in the system.

Note: It is not recommended to set the timestamp manually. In Linux, the server clock is coordinated using the ntp service or the systemd-timesyncd service.

Code:

date --set="20200101 17:30"

Explanation:

  • With the help of date command, we are able to set the system time. We have set the manual time i.e. 01/01/2020 05:30 PM.

Output:

set system time

Conclusion

We have seen the uncut concept of “Linux Date Command” with the proper example, explanation and command with different outputs. The date command is used to display the date and time in a different format. It will be widely used in shell and application jobs.

Recommended Articles

This is a guide to Linux Date. Here we discuss how linux date command works along with examples respectively. You may also have a look at the following articles to learn more –

  1.  Linux More Command
  2. Linux Touch Command
  3. Linux tr Command
  4. Linux tee Command

Linux Training Program (16 Courses, 3+ Projects)

16 Online Courses

3 Hands-on Projects

160+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Linux Training Program (16 Courses, 3+ Projects) Learn More