Cheatsheet & Examples: date
The date command is used to display or set the system date and time. It can also format and manipulate dates in various ways.
Display the current date and time
Example Usage:date
What it does:
Displays the current date and time in the default format, which includes the day, month, date, time, time zone, and year.
Command-line Arguments Explained:
- None: No arguments are required for basic usage, as the command defaults to showing the current date and time.
Format date and time with custom specifications
Example Usage:date +"%Y-%m-%d %H:%M:%S"
What it does:
Prints the date and time in a custom format (e.g., "2023-10-25 14:30:00").
Command-line Arguments Explained:
+"format": Specifies a custom format string using directives like%Y(year),%m(month),%d(day),%H(hour),%M(minute),%S(second).
Set the system date and time
Example Usage:date -s "2023-04-05 14:30:00"
What it does:
Sets the system date and time to the specified value (requires root privileges).
Command-line Arguments Explained:
-s: Sets the system date and time."2023-04-05 14:30:00": The target date and time in a recognizable format.
Display the current date in UTC time
Example Usage:date -u
What it does:
Shows the current date and time in Coordinated Universal Time (UTC).
Command-line Arguments Explained:
-uor--utc: Displays the date in UTC rather than the local time zone.
Parse and display a specific date
Example Usage:date --date="next Friday"
What it does:
Displays the date and time for the next Friday.
Command-line Arguments Explained:
--dateor-d: Accepts a date string (e.g., "next Friday", "2023-12-25", "1 hour ago") to compute and display.
Show the current timestamp in seconds since the epoch
Example Usage:date +%s
What it does:
Prints the current date and time as a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).
Command-line Arguments Explained:
+%s: Output the Unix epoch timestamp.
Display the date in a specific locale or timezone
Example Usage:date -r "Europe/London"
What it does:
Displays the current date and time in the specified timezone (e.g., London).
Command-line Arguments Explained:
-r: Uses theTZenvironment variable to set the timezone."Europe/London": A valid timezone identifier (e.g., "America/New_York", "Asia/Tokyo").
Show the last modification time of a file
Example Usage:date -r file.txt
What it does:
Displays the last modification time of the specified file (file.txt).
Command-line Arguments Explained:
-r: Displays the file's modification time instead of the system date.file.txt: The file whose timestamp is to be shown.
Convert a date to a different timezone
Example Usage:date -u -d "2023-04-05 14:30:00" +"%Y-%m-%d %H:%M:%S %Z"
What it does:
Displays the given date in UTC, including the timezone abbreviation.
Command-line Arguments Explained:
-u: Outputs the date in UTC.-d "2023-04-05 14:30:00": Parses the specified date.+"%Y-%m-%d %H:%M:%S %Z": Custom format showing the timezone.
Display the date in RFC 2822 format
Example Usage:date -R
What it does:
Shows the date and time in RFC 2822 format, commonly used for email headers.
Command-line Arguments Explained:
-Ror--rfc-2822: Formats the date according to the RFC 2822 standard.
Display the date in ISO 8601 format
Example Usage:date +%F
What it does:
Outputs the current date in ISO 8601 format (e.g., "2023-10-25").
Command-line Arguments Explained:
+%F: Uses the ISO 8601 format for output (equivalent to%Y-%m-%d).
Show the day of the week
Example Usage:date +"%A"
What it does:
Prints the full name of the current day (e.g., "Thursday").
Command-line Arguments Explained:
+"%A": Uses the%Adirective to output the full weekday name.
Show the date relative to a specific date
Example Usage:date -d "2023-04-05 14:30:00 + 3 days"
What it does:
Calculates the date and time three days after the specified date.
Command-line Arguments Explained:
-d: Specifies the base date and time."2023-04-05 14:30:00 + 3 days": A date string with relative time.
Display the date in a different language or locale
Example Usage:LC_TIME=en_US.UTF-8 date +"%A %B %d"
What it does:
Displays the date in English (United States) locale, including the weekday, month, and day.
Command-line Arguments Explained:
LC_TIME: Sets the locale for date formatting.+"%A %B %d": Format directives for localized output.
Show the last modification time of a file in a custom format
Example Usage:date -r file.txt +"%Y-%m-%d %H:%M"
What it does:
Displays the last modification time of file.txt in a custom format.
Command-line Arguments Explained:
-r: Uses the file's timestamp instead of the system date.+"%Y-%m-%d %H:%M": Custom format for the file's modification time.
Set the system date from a file
Example Usage:date -f datefile
What it does:
Reads the date from a file and sets the system date accordingly.
Command-line Arguments Explained:
-f: Specifies a file containing the date to set.datefile: A file with a valid date string (e.g., "2023-04-05 14:30:00").

