The linux date command is used to display and set the date of the system.
To see the current date type the command “ date” on the terminal as shown the below
1 | $ date [ press enter on terminal] |
output
1 | Sat Jul 1 17:32:37 IST 2023 |
Linux date Format
Linux date format is as below
%Y
: 4-digit year (e.g., 2023)%y
: 2-digit year (e.g., 23)%m
: 2-digit month (e.g., 07 for July)%d
: 2-digit day of the month (e.g., 01)%H
: 2-digit hour in 24-hour format (e.g., 13 for 1 PM)%M
: 2-digit minute (e.g., 30)%S
: 2-digit second (e.g., 45)%A
: Full weekday name (e.g., Monday)%a
: Abbreviated weekday name (e.g., Mon)%B
: Full month name (e.g., July)%b
: Abbreviated month name (e.g., Jul)%Z
: Timezone name (e.g., PDT)
You can compile the above as per your need
date +"%Y-%m-%d %H:%M:%S"
Date With optioins
The above command will display the current day, date, month, year, and time as shown above.
Option:
the date command can also be used with the format specified.
Each format is preceded by the + symbol and followed by the % operator and the format is described as a single character.
We want to see only the current month use format +%m. as shown below.
1 | $ date +%m [ press enter] |
output
1 | 07 |
output is month 07
We want to see only the current month name using format +%h. as shown below.
1 | $ date +%h [ press enter] |
output
1 | Jul |
is month of July
We can also combine both commands as:
1 | $ date +"%m %h" [ press enter] |
output
1 | 07 Jul |
output is month 07 is July
Unix linux set date
Use the date command with the -s
option with the desired date
Syntax for set Date
1 | sudo date -s "YYYY-MM-DD HH:MM:SS" |
1 | sudo date -s "2023-07-01 12:00:00" |
Command for time in Linux/Unix
To display the current system time in Linux, you can use the date
command with the appropriate format option. Here’s an example:
1 | date +%T |
The %T
format code is used to display the time in the format “HH:MM:SS”
If you want to display the time in a specific timezone, you can use the %Z
format code along with the %T
code. For example:
1 | date +%T%Z |
To display only the current hour and minute in Linux, you can use the date
command with the %H
and %M
format codes. Here’s the command:
1 | date +%H:%M |
This command will provide the current system time in the format “HH:MM”, where HH represents the hour (in 24-hour format) and MM represents the minute.
shell script todays date
Open the editor and write the following commands
1 2 3 4 5 6 7 | #!/bin/bash # Get today's date today=$(date +%Y-%m-%d) # Print the date echo "Today's date is: $today" |
Save and close the file.
Give execute permissions to the script using the following command:
1 | chmod +x todays_date.sh |
Now, you can run the script in the terminal:
1 | ./todays_date.sh |
The script will display today’s date in the format “YYYY-MM-DD”.
Convert Date Formats
You can convert a date from one format to another using the date
command and specifying the desired format with the +%FORMAT
option. For example, to convert a date from “YYYY-MM-DD” format to “DD/MM/YYYY” format:
1 | date -d "2023-07-01" +%d/%m/%Y |
Calculate Future or Past Dates
To calculate a future or past date, you can use the -d
option with the date
command and provide a relative date expression. For example, to find the date 7 days from today:
1 | date -d "+7 days" |