date - command can be used in Linux/Unix/macOS Terminal to display or set date and time. You can use the date command with various arguments to display date in various formats and timezones as well as manipulate the date and time.
Let's see examples of how you can use date commands with some examples,
Syntax: date [argument(s)] [+Format(s)]
1. Example: date
Let's first use the simple command date, you will see that the default output you get is in format (day of the week, month, date, hour:minute:seconds TimeZone Year)
$ date
Sat Jun 26 23:09:15 CDT 2021
2. Example: date -u
If you use the option -u along with the date command, you will see that the outputted date time is in UTC timezone.
$ date -u
Sun Jun 27 05:55:11 UTC 2021
3. Example: Displaying date/time in various formats
You make make use of the below date format options to output the date and time in various formats as you want,
Date Format Option | Command | Output | Usage |
---|---|---|---|
%a | data +"%a" | Sun | Displays day of the weak in short form i.e. Sun, Mon, Tue, Wed, Thu, Fri, Sat. |
%A | data +"%A" | Sunday | Displays day of the week in long form i.e. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday. |
%b | date +"%b" | Jun | Displays the current month in short form i.e. Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec. |
%B | data +"%B" | June | Displays the current month in long form i.e. January, February, March, April, May, June, July, August, September, October, November, December. |
%d | data +"%d" | 27 | Displays day of the month in numeric format. |
%Y | data +"%Y" | 2021 | Displays the current year in format yyyy. |
%D | data +"%D" | 06/27/2021 | Displays the current date in MM/dd/yyyy format. |
%H | data +"%H" | 09 | Displays the current hour in HH format (01-23) |
%m | data +"%m" | 06 | Displays the current month in MM format: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12. |
%I | data +"I" | 11 | Displays the current hour 01-11 format. |
%M | data +"M" | 51 | Displays the current minutes in mm format: (00-59) |
%S | data +"%S" | 25 | Displays the current seconds: (00-59) |
Let's see some examples with using the date/time format string in conjunction.
Example: Display date in dd-MM-yyyy format$ date +"%d-%m-%Y"
27-06-2021
Example: Display date in yyyy-MM-dd format
$ date +"%Y-%m-%d"
2021-06-27
Example: Display date in MM-dd-yyyy format
$ date +"%m-%d-%Y"
06-27-2021
Example: Display date in dd-MM-yy format
$ date +"%d-%m-%y"
27-06-21
Example: Display date in MM/dd/yy format
$ date +"%m/%d/%y"
06/27/21
Example: Display date in dd/MM/yyyy format
$ date +"%m/%d/%Y"
27/06/2021
Example: Display date in dd-MMM-yyyy format
$ date +"%d-b%-%Y"
27-Jun-2021
Example: Display date and time: dd-MM-yyyy HH:mm:ss format
$ date +"%d-%m-%Y %I:%M:%S"
27-06-2021 12:28:52
Example: Display time in HH:mm format
$ date +"%I:%M"
12:28
4. Example: Displaying date/time in epoch time
In order to display the date in Epoch time you can use of the format string %s
$ date %s
1624777451
Have Questions? Post them here!
- Command to know the Available Memory on Linux System
- How to install curl on Alpine Linux
- How to backup a file in Linux/Unix
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu
- What is the Default Admin user and Password for Jenkins
- How to tar.gz a directory or folder Command
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: bash: ipconfig: command not found on Linux
- Command to check Last Login or Reboot History of Users and TTYs
- Linux: Create a New User and Password and Login Example
- ls command to list only directories
- bash: cls: command not found
- How to exit from nano command
- Installing and using unzip Command to unzip a zip file using Terminal
- What does apt-get update command does?
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- How to remove or uninstall Java from Ubuntu using apt-get
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- Sort ls command by last modified date and time
- Create Nested Directories using mkdir Command
- How to Exit a File in Terminal (Bash/Zsh)
- Command to know the installed Debian version?
- How to connect to SSH port other than default 22
- How to save a file in Nano Editor and Exit
- Install OpenSSL on Linux/Ubuntu
- How to check your installed version of Git - Git
- Notepad++ select all above or below lines - NotepadPlusPlus
- [Mac] How to Mute People in a Microsoft Teams Meeting - Teams
- Android Studio Button onClickListener Example - Android-Studio
- How to come out of Full Screen Mode Roblox on Mac - MacOS
- How to change background color in Notepad++ - NotepadPlusPlus
- Mac - Open Finder App using Terminal Current Location - MacOS
- How to change SharePoint Online site collection or subsite URL address - SharePoint