date command in Linux with Examples

Posted in

date command in Linux with Examples

Vinay Khatri
Last updated on July 13, 2024

    This article goes through the date command of Linux. We will discuss all the flags/options and format specifiers available with the command. We will also show examples of each command. These commands will work on any Linux-based Operating System.

    What is date command?

    The date command is used to get the current date and time in the Linux-based Operating Systems. We can set various options with the command to get the dates in various formats. We can even set the date and time of the system using this command. However, you need to have root access to set the date. The syntax for the command is

    $ date [option] [+format]

    Examples of using the date command

    1. Display time and date

    To get the current date and time, use the below-mentioned command This command will output the day of the month, month, year, current time, day of the week, and time zone of the current location.

    $ date

    2. Get GMT time

    The below command outputs the date in Greenwich Mean Time

    $ date -u

    3. Convert string into date format

    To convert the given string into date format, use the below command.

    $ date -date

    4. Get past dates

    We can also use the -date option to get paste dates. For this, we just need to tell Linux the time it needs to go back. For instance, we can type the string “3 year ago” to get the date and time, 2 years back.

    $ date -date="3 year ago"

    You can use the same kind of syntax for querying other types of past dates as well.

    $ date -date="previous"
    $ date -date="3 month ago"

    5. Get future dates

    You can use a similar command to get future dates as well.

    $ date -date="1 year"
    $ date -date="tomorrow"

    6. Set date

    To set the date in your system, use the below command

    $  date --set="20100513 05:30"

    As mentioned above, you need to have root access to execute this command.

    7. Last modification time of a file

    We can also get the date and time when a specified file was modified

    $ date -r test.txt

    8. Display date string of a file

    The --file option outputs the date string that appears at each line in the file. In contrast to the --date option, --file allows displaying several date strings on each line. Below is the syntax for this command.

    $ date --file=file..txt

    Format specifiers

    We can also use format specifiers to get the data in specified formats Below are the format specifiers available with the command.

    • %D: Display the date as mm/dd/yy.
    • %d: Display the month's day (01 to 31).
    • %a: Shows the shortened name for weekdays (Sun to Sat).
    • %A: Shows all weekdays (Sunday to Saturday).
    • %h: Displays a month name that has been shortened (Jan to Dec).
    • %b: Displays a month name that has been shortened (Jan to Dec).
    • %B: Shows the whole month's name (January to December).
    • %m: Shows the month of the year (01 to 12).
    • %y: Displays the year's last two digits (00 to 99).
    • %Y: Show the four-digit year.
    • %T: Show the time in 24-hour format as HH:MM:SS.
    • %H: Show the current time.
    • %M: Show the current minute.
    • %S: Display the seconds.

    Below are the examples of using format specifiers

    $ date "+%T"
    $ date “+%D %T”
    $ date “+%A”


    We went through all the major options available with the date command. This command can be used to get dates in various formats, get future and past dates, change the system time, and even allow us to use format specifiers with it.

    People are also reading:

    Leave a Comment on this Post