A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/date-command-linux-examples/ below:

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

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, it makes it an essential command for system configuration and scripting. Uses of the date Command are

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,

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

In this, we're going to explore different methods of displaying the system's date and time in Linux.

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. 

Syntax:

date

Output:

Display Current Date and Time in Linux

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.

Syntax:

date -u

Output:

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=" datestring " + FORMAT

Example:

$date -d "12/25/2025"
$date -d "Dec 25 2025"
4. Display Past Dates

You can also display past dates using the --date option.

Example 1:

Displaying the date and time of 2 years ago:

Syntax:

date --date "2 year ago"
Date and time of 2 years ago. 

Example 2:

Displaying date and time of 5 seconds ago.

Syntax:

date --date "5 sec ago"

Output:

Date and time of 5 seconds ago

Example 3:

Date and time of previous day.

Syntax:

date --date "yesterday"

Output:

Date and time of previous day

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.  

Syntax:

date -d "next tuesday"

Output:

Example 2:

Date and time next day. 

Syntax:

date -d "tomorrow"

Output:

Example 3:

Date and time after 1 year on the current day

Syntax:

date -d "1 year"

Output:

6. Set the System Date and Time

There are different types of methods to set date and time for temporary changes and also for persistent changes on modern systems.

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

Syntax:  

sudo date -s "date to be set"

Example:

date -s "2025-07-27"
Method 2: By using date.

Syntax:

sudo date MMDDhhmmYYYY

Example:

sudo date 081210352025
Method 3: By using 'timedatectl'

Syntax:

sudo timedatectl set-time 'Enter the date and time'

Example:

sudo timedatectl set-time '2025-07-26 10:30:00'

For View Current Time Settings:

timedatectl
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

Syntax:

date "+%D"

Output:

Display Date in MM/DD/YY Format

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

Syntax:

date "+%D %T"

Output:

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

3. Display Date in YYYY-MM-DD Format

Syntax:

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

Output:

Display Date in YYYY-MM-DD Format

4. Display Date in YYYY/MM/DD Format

Syntax:

date "+%Y/%m/%d"

Output:

Display Date in YYYY/MM/DD Format

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

Syntax:

date "+%A %B %d %T %y"

Output:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4