How to Get Current Date and Time in Bash Script

Channel: Linux
Abstract: 2019 Mar 25date +with Monday as first day of week (00..53) %x locale’s date representation (e.g.

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
ParameterOutputdate +」%m/%d/%Y」03/25/2019date +」%d-%b-%Y」25-Mar-2019date +」%Y %b %m」2019 Mar 25date +」%H:%M」14:40date +」%I:%M %p」02:40 PMdate +」%H:%M:%S」14:40:32date +」%I:%M:%S %p」02:40:32 PMdate +」%m/%d/%Y %H:%M」03/25/2019 14:40 date +」%A, %m %d %Y %H:%M」Monday, 03 25 2019 14:40date +」%A, %b %d, %Y %I:%M %p」Monday, Mar 25, 2019 02:40 PMdate +」%A, %b %d, %Y %H:%M:%S」Monday, Mar 25, 2019 14:40:32Use 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.

#!/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}12345678910#!/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)

Ref From: tecadmin

Related articles