Linux date Command
The date command in Linux is used to display or set the system date and time. It is a powerful utility that allows you to format the output in various ways.
Basic Syntax
date [OPTION]... [+FORMAT]
Common Options
+%Y: Displays the year (e.g., 2023).+%y: Displays the last two digits of the year (e.g., 23).+%m: Displays the month (01-12).+%d: Displays the day of the month (01-31).+%H: Displays the hour (00-23).+%M: Displays the minute (00-59).+%S: Displays the second (00-59).+%A: Displays the full weekday name (e.g., Monday).+%a: Displays the abbreviated weekday name (e.g., Mon).+%B: Displays the full month name (e.g., January).+%b: Displays the abbreviated month name (e.g., Jan).+%c: Displays the date and time in the locale's format.+%x: Displays the date in the locale's format.+%X: Displays the time in the locale's format.+%s: Displays the number of seconds since the epoch (1970-01-01 00:00:00 UTC).+%z: Displays the time zone offset from UTC.+%Z: Displays the time zone name.--date="STRING": Displays the date and time specified by the string.--set="STRING": Sets the system date and time.--utc: Displays the date in UTC (Coordinated Universal Time).--rfc-3339=TYPE: Displays the date in RFC 3339 format.--iso-8601=TYPE: Displays the date in ISO 8601 format.
Examples
1. Display the Current Date and Time
date
Output:
Tue Oct 10 14:30:00 UTC 2023
2. Display Only the Current Year
date +%Y
Output:
2023
3. Display Date in Custom Format
date "+%Y-%m-%d %H:%M:%S"
Output:
2023-10-10 14:30:00
4. Set the System Date and Time
sudo date -s "2023-10-10 14:30:00"
This command sets the system date and time to the specified value.
5. Display the Date in RFC 3339 Format
date --rfc-3339=seconds
Output:
2023-10-10 14:30:00+00:00
6. Display the Date in ISO 8601 Format
date --iso-8601=seconds
Output:
2023-10-10T14:30:00+00:00
7. Display the Date in UTC
date --utc
Output:
Tue Oct 10 14:30:00 UTC 2023
8. Display the Date in a Specific Time Zone
TZ="America/New_York" date
Output:
Tue Oct 10 10:30:00 EDT 2023
9. Display the Date in a Specific Format
date "+%A, %B %d, %Y"
Output:
Tuesday, October 10, 2023
Some Other method
0525115625.30
This Time formate is "mmddHHmmyy.ss"
Where:
mm: Month (01-12)dd: Day of the month (01-31)HH: Hour (00-23)mm: Minute (00-59)yy: Year (last two digits)ss: Seconds (00-59)
To convert this format to a standard date format, you can use the date command with the -d option:
date -d "0525115625.30" +"%Y-%m-%d %H:%M:%S"
Output:
2025-05-25 11:56:25
Set Time zone
To set the time zone in Linux, you can use the timedatectl command. For example, to set the time zone to "America/New_York", you can use:
sudo timedatectl set-timezone America/New_York
To check the current time zone, you can use:
timedatectl
Output:
Local time: Tue 2023-10-10 14:30:00 UTC
Time zone: UTC (UTC, +0000)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
To list all available time zones, you can use:
timedatectl list-timezones
This will display a list of all available time zones. You can scroll through the list or use the grep command to filter for a specific time zone.
For example, to find all time zones that contain "Asia", you can use:
timedatectl list-timezones | grep Asia
This will display a list of all time zones that contain "Asia".
To set the time zone to a specific time zone, you can use the timedatectl set-timezone command followed by the name of the time zone. For example:
sudo timedatectl set-timezone Asia/Kolkata
This will set the time zone to "Asia/Kolkata".
To verify that the time zone has been set correctly, you can use the timedatectl command again:
timedatectl
Output:
Local time: Tue 2023-10-10 14:30:00 IST
Time zone: Asia/Kolkata (IST, +5:30)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
Note: You may need to restart your system or log out and log back in for the changes to take effect.