DATE COMMAND

DATE COMMAND


Introduction

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.


Prerequisites

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line

Linux date Command Syntax

The syntax for the date command is:

date [option]... [+format]


How to Use date Command different way

To show the current system time and date, type in the date command:

No alt text provided for this image



Date Command Format Options

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values

date +"Year: %Y, Month: %m, Day: %d"

Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:

No alt text provided for this image

date "+DATE: %D%nTIME: %T"


No alt text provided for this image


date +"Week number: %V Year: %y"


No alt text provided for this image


These are the most common command for the date command:

  • %D – Display date as mm/dd/yy
  • %Y – Year (e.g., 2021)
  • %m – Month (01-12)
  • %B – Long month name (e.g., September)
  • %b – Short month name (e.g., Sep)
  • %d – Day of month (e.g., 01)
  • %j – Day of year (001-366)
  • %u – Day of week (1-7)
  • date +%U – Week of Year with Sunday as first day of week
  • %A – Full weekday name (e.g., Friday)
  • %a – Short weekday name (e.g., Fri)
  • %H – Hour (00-23)
  • %I – Hour (01-12)
  • %M – Minute (00-59)
  • %S – Second (00-60)
  • date +%c – Locale’s date time
  • date +%x – Locale’s date
  • date +%X – Locale’s time
  • date +%T – HH:MM:SS time format
  • date +%p – Locale’s equivalent of AM or PM

Example

No alt text provided for this image



 
Display Past Date

        


Use the --date option to display past dates. The date command accepts values such as "tomorrow", "Friday", "last Friday", "next Friday", "next week", and similar.

date --date="3 year ago"

date --date="yesterday"

No alt text provided for this image



Display Future Dates

        

The --date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:


date --date="next monday"

date --date="4 day"

date --date="tomorrow"

No alt text provided for this image

To view or add a comment, sign in

More articles by Vishwanath Bhagat

  • AWS Serverless Computing or Lambda & S3

    AWS (Amazon web services) Lambda Lambda is a compute service that lets you run code without provisioning or managing…

Explore content categories