The Date Command and its usage [Linux - Unix - macOS]

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"
Example: Display date in yyyy-MM-dd format
$ date +"%Y-%m-%d"
Example: Display date in MM-dd-yyyy format
$ date +"%m-%d-%Y"
Example: Display date in dd-MM-yy format
$ date +"%d-%m-%y"
Example: Display date in MM/dd/yy format
$ date +"%m/%d/%y"
Example: Display date in dd/MM/yyyy format
$ date +"%m/%d/%Y"
Example: Display date in dd-MMM-yyyy format
$ date +"%d-b%-%Y"
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"
Unix - Linux - macOS - date command examples
Unix - Linux - macOS - date command examples

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

Have Questions? Post them here!

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap