close
The Wayback Machine - https://web.archive.org/web/20250310002013/https://www.geeksforgeeks.org/date-command-linux-examples/
Open In App

How to Display and Set Date and Time in Linux | date Command

Last Updated : 24 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The date command in Linux is one of the most basic and powerful tools that enable users to display the current date and time, as well as set the date and time on a Linux system. The date command in Linux can be used for the following:

  • Synchronizing System Time
  • Customizing Date Formats
  • Data manipulations (such as finding past or upcoming dates)

In this tutorial, we will walk you through the process of how to display date and time in Linux, change system settings, and work with Linux date format specifiers to format output.

What is the date Command in Linux

The date command in Linux allows the user to display the current date and time in a variety of formats and set the system date and time. Being a part of the core utilities in Linux and Unix-like operating systems, making it an essential command for system configuration and scriptings.

However, by default, the date displays the current time in the system’s configured timezone. It suupports formatting using the special date format specifiers to display the time exactly the way you need it.

Syntax of Date Command in Linux

The basic syntax for the date command is:

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

where,

  • OPTION: Refers to different flags & arguments that modify the behaviour of the command.
  • FORMAT: Refers to “how you want to display” the date, using format specifiers. (such as %Y, %m, %d).

How to Display and Set Date and Time in Linux: 9 Different Formats

In this guide, we’re going to explore different methods of displaying the system’s date and time in Linux. Let’s get this started:

1. Display Current Date and Time in Linux

The date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year. 

Command:

date

Output:

Display Current Date and Time in Linux

Display Current Date and Time in Linux

Note: Here unix system is configured in Pacific daylight time and displaying the day, date, time, timezone, and year.

2. Display Time in GMT/UTC

The -u option with the date command allows you to display the current time in GMT (Greenwich Mean Time) or UTC (Coordinated Universal Time) instead of your system’s local time zone.

Command:

date -u

Output:

Display Time in GMT

Display Time in GMT

3. Display the Given Date String in the Format of Date

–date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string. 

Syntax:  

date --date=" string "

Command:

$date --date="2/02/2010"
$date --date="Feb 2 2010"


Output:

Tue Feb  2 00:00:00 PST 2010
Tue Feb  2 00:00:00 PST 2010

4. Display Past Dates

You can also display past dates using tje –date option. Below, we’re displaying the date and time of 2 years ago:

Example 1:

date --date="2 year ago"
2024-02-06_16-06

Date and time of 2 years ago. 

Example 2:

Date and time of 5 seconds ago

date --date="5 sec ago"
Date and time of 5 seconds ago

Date and time of 5 seconds ago

Example 3:

Date and time of previous day

date --date="yesterday"
Date and time of previous day

Date and time of previous day

Example 4:

Date and time of 2 months ago

date --date="2 month ago"
Date and time of 2 months ago

Date and time of 2 months ago

Example 5:

Date and time of 10 days ago

date –date=”10 day ago”

Date and time of 10 days ago

Date and time of 10 days ago

Note: These commands will display time or date irrespective of the current date and time.

5. Display Future Dates

This command can be used to display any specific future date and time. Here’s the syntax below:

Example 1:

Date and time of upcoming particular week day.  

Command:

$date --date="next tue"
Output: 
Tue Oct 17 00:00:00 PDT 2017

Example 2:

Date and time after two days. 

Command:

$date --date="2 day"
Output: 
Fri Oct 13 00:05:52 PDT 2017

Example 3:

Date and time of next day. 

Command:

$date --date="tomorrow"
Output: 
Thu Oct 12 00:08:47 PDT 2017

Example 4:

Date and time after 1 year on the current day. 

Command:

$date --date="1 year"
Output: 
Thu Oct 11 00:11:38 PDT 2018

6. Set the System Date and Time

Use the -s or --set option to set the system date and time to a specific value.

Syntax:  

date --set="date to be set"

Command:

$date 

Output:

Wed Oct 11 15:23:26 PDT 2017

Command:

$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date

Output:

Tue Nov 13 15:23:34 PDT 2018

7. Display Date Strings from a File

–file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line. 

Syntax: 

date --file=file.txt
$cat >> datefile
 Sep 23 2018
 Nov 03 2019

Command:

$date --file=datefile

Output:

Sun Sep 23 00:00:00 PDT 2018
Sun Nov  3 00:00:00 PDT 2019

8. Display Last Modified Timestamp of a File

The -r option is used to display the last modified timestamp of a specified file.

Syntax:  

date -r file.txt

We can modify the timestamp of a datefile by using touch command:

$touch datefile 
$date -r datefile
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date -r datefile
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile

9. List of Format Specifiers Used with Date Command

The %d option is used to list of all format specifiers, adjoining to it different apprevations are used to specify the exact day, month or year. Have a look at the syntax below:

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

Syntax:  

date +%[format-option]

Examples:

1. Display Date in MM/DD/YY Format

date "+%D"
Display Date in MM/DD/YY Format

Display Date in MM/DD/YY Format

2. Display Date and Time in MM/DD/YY HH:MM:SS Format

date "+%D %T"
Display Date and Time in MM/DD/YY HH:MM:SS Format

Display Date and Time in MM/DD/YY HH:MM:SS Format

3. Display Date in YYYY-MM-DD Format

date "+%Y-%m-%d"
Display Date in YYYY-MM-DD Format

Display Date in YYYY-MM-DD Format

4. Display Date in YYYY/MM/DD Format

date "+%Y/%m/%d"
Display Date in YYYY/MM/DD Format

Display Date in YYYY/MM/DD Format

5. Display Full Date and Time with Day, Month, Date, Time, and Year

date "+%A %B %d %T %y"
Display Full Date and Time with Day, Month, Date, Time, and Year

Display Full Date and Time with Day, Month, Date, Time, and Year

Best Practices While Using the date Command in Linux

1. Ensure to cross-verify the changes that you’re about to make with the date command.

2. Also, ensure that the format you’re using is correct to display the correct desired timing or date. (you may also combine multiple specifiers for any custom format).

3. You can use date-based scripting to automate tasks such as file backup, log file rotations, or system maintenance tasks.

Conclusion

The date command in Linux is one of the most versatile and essential tool for managing date and time. Whether you’re trying to set the system date, or looking to display specific date formats, or calculating past and future dates, it works like a charm. By using the options and format specifiers outlined in this, you can automate tasks, format time (in different ways), and ensure your Linux system’s time settings are accurate.

How to Display and Set Date and Time in Linux | date Command – FAQs

How to display date and time in Linux?

To display the current date and time in Linux, use the date command:

date

This command will output the current system date and time.

How to set Linux time and date?

To set the date and time in Linux, use the date command with the appropriate format. For example, to set the date to 25th December 2024 and the time to 10:30 AM:

sudo date -s "2024-12-25 10:30:00"

You may need to restart the timedatectl service to apply changes:

sudo systemctl restart systemd-timesyncd

How to set date format in Linux?

To display the date in a specific format, use the date command with format specifiers. For example, to display the date in YYYY-MM-DD format:

date +"%Y-%m-%d"

How to display date in dd mm yyyy format in Linux?

To display the date in DD-MM-YYYY format in Linux, use the date command with the appropriate format specifiers:

date +"%d-%m-%Y"

How to use time command in Linux?

The time command in Linux is used to measure the duration of a command or script execution. It provides real, user, and system time taken to execute the command. For example, to measure the time taken by the ls command:

time ls

Output:

real    0m0.003s
user    0m0.002s
sys     0m0.001s

How do I display time in GMT/UTC?

You can use the -u option to display the date and time in the GMT/UTC format. The syntax for this is:

date -u


Next Article

Similar Reads

three90RightbarBannerImg