• Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Get Current Date and Time in Bash Script

Written by Rahul, Updated on June 6, 2019

You can use date command on Linux shell script to get current Date and Time. The date command is the part of the Linux Coreutils package. This tutorial will help you to get the current date and time in a shell script.

Uses of date Command:

Simple date command returns the current date and time with the current timezone set in your system.

date

Mon Mar  6 14:40:32 IST 2019

You can also store the output of command in a variable for further use.

currentDate=`date`
echo $currentDate

Mon Mar 25 14:40:32 IST 2019

Formated Output of date Command:

There are several switches, you can use to format the output of date command.

Get date time in MM/DD/YY HH:MM:SS format:

date +"%D %T"

03/25/17 14:40:32

Get current Unix epoch time:

date +%s

1554542637

Get date time in YYYY-MM-DD HH:MM:SS format:

date +"%Y-%m-%d %T"

2019-03-25 14:40:32
Parameter
Output
date +”%m/%d/%Y”
03/25/2019
date +”%d-%b-%Y”
25-Mar-2019
date +”%Y %b %m”
2019 Mar 25
date +”%H:%M”
14:40
date +”%I:%M %p”
02:40 PM
date +”%H:%M:%S”
14:40:32
date +”%I:%M:%S %p”
02:40:32 PM
date +”%m/%d/%Y %H:%M”
03/25/2019 14:40
date +”%A, %m %d %Y %H:%M”
Monday, 03 25 2019 14:40
date +”%A, %b %d, %Y %I:%M %p”
Monday, Mar 25, 2019 02:40 PM
date +”%A, %b %d, %Y %H:%M:%S”
Monday, Mar 25, 2019 14:40:32

Use Date in Shell Script:

You can simply use date command inside shell script similar to use on the command prompt. Create a bash script getDateTime.sh with the following content.

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
 
CURRENTDATE=`date +"%Y-%m-%d %T"`
CURRENTDATEONLY=`date +"%b %d, %Y"`
CURRENTEPOCTIME=`date +"%Y-%m-%d %T"`
 
echo Current Date is: ${CURRENTDATEONLY}
echo Current Date and Time is: `date +"%Y-%m-%d %T"`
echo Current Date and Time is: ${CURRENTDATE}
echo Current Unix epoch time is: ${CURRENTEPOCTIME}

Now execute the script from command line and watch output.

./getDateTime.sh

Current Date is: Mar 25, 2019
Current Date and Time is: 2019-03-25 17:18:19
Current Date and Time is: 2019-03-05 17:18:19
Current Unix epoch time is: 1488541699

Available Options with date Command:

You can find all available options of date command using –help parameter

date --help

You will find the output like below with some more options.

  • %% a literal %
  • %a locale’s abbreviated weekday name (e.g., Sun)
  • %A locale’s full weekday name (e.g., Sunday)
  • %b locale’s abbreviated month name (e.g., Jan)
  • %B locale’s full month name (e.g., January)
  • %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
  • %C century; like %Y, except omit last two digits (e.g., 21)
  • %d day of month (e.g, 01)
  • %D date; same as %m/%d/%y
  • %e day of month, space padded; same as %_d
  • %F full date; same as %Y-%m-%d
  • %g last two digits of year of ISO week number (see %G)
  • %G year of ISO week number (see %V); normally useful only with %V
  • %h same as %b
  • %H hour (00..23)
  • %I hour (01..12)
  • %j day of year (001..366)
  • %k hour ( 0..23)
  • %l hour ( 1..12)
  • %m month (01..12)
  • %M minute (00..59)
  • %n a newline
  • %N nanoseconds (000000000..999999999)
  • %p locale’s equivalent of either AM or PM; blank if not known
  • %P like %p, but lower case
  • %r locale’s 12-hour clock time (e.g., 11:11:04 PM)
  • %R 24-hour hour and minute; same as %H:%M
  • %s seconds since 1970-01-01 00:00:00 UTC
  • %S second (00..60)
  • %t a tab
  • %T time; same as %H:%M:%S
  • %u day of week (1..7); 1 is Monday
  • %U week number of year, with Sunday as first day of week (00..53)
  • %V ISO week number, with Monday as first day of week (01..53)
  • %w day of week (0..6); 0 is Sunday
  • %W week number of year, with Monday as first day of week (00..53)
  • %x locale’s date representation (e.g., 12/31/99)
  • %X locale’s time representation (e.g., 23:13:48)
  • %y last two digits of year (00..99)
  • %Y year
  • %z +hhmm numeric timezone (e.g., -0400)
  • %:z +hh:mm numeric timezone (e.g., -04:00)
  • %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
  • %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
  • %Z alphabetic time zone abbreviation (e.g., EDT)

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

3 Comments

  1. Avatar Zak Reply
    August 13, 2020 at 4:26 pm

    Thankfulness to my father who stated to me about this blog, this webpage is
    actually remarkable.

  2. Avatar Animesh Kumar Reply
    June 6, 2019 at 4:41 am

    CURRENTEPOCTIME=`date +Y-%m-%d %T”` this is wrong.

    CURRENTEPOCTIME=`date +”%Y-%m-%d %T”` this is correct.

    • Rahul Rahul Reply
      June 6, 2019 at 4:47 am

      Thanks Animesh, Corrected the tutorial.

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Debian 10
  • Download Ubuntu 20.04 LTS – DVD ISO Images
  • Linux Run Commands As Another User
  • How to Check PHP Version (Apache/Nginx/CLI)
  • How To Install and Configure GitLab on Ubuntu 20.04
  • How to Install PyCharm on Ubuntu 20.04
  • How to Check Ubuntu Version with Command or Script
  • How to Set all directories to 755 And all files to 644
© 2013-2021 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy