As a scripting language, Bash is widely used for system administration tasks and automation. One common task in Bash scripting is to get the current date and time. The `date` command returns the current date and time in a specific format, but you can also use a custom format string to get the date and time in any format
In this article, we will explore how to get the current date and time in a Bash script.
Getting the Current Date and Time
In Bash, you can use the date command to get the current date and time. By default, the date command will return the current date and time in the format of “Day_of_week Month Day Hour:Minute:Second Timezone Year”
dateSun Feb 28 23:26:13 UTC 2023
You can also store the output of command in a variable for further use.
Custom Format Output
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:
- Get date time in “YYYY-MM-DD HH:MM:SS” format:
date +"%Y-%m-%d %T"2019-03-25 14:40:32
- Here is more common date time formats:
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.
Now execute the script from command line and watch output.
./getDateTime.shCurrent 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
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)
%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)
Getting the current date and time in a Bash script is a common task that can be accomplished using the date command. By default, the date command returns the current date and time in a specific format, but you can also use a custom format string to get the date and time in any format you like. By understanding how to use the date command in a Bash script, you can build powerful automation scripts that make use of the current date and time.
Line 5 uses the same formatting string as line 3… shouldn’t the string be %s?
what ever is date and time today, how can I get start of today where hh:mm:ss is “00:00:00” for UTC timezone?
Thankfulness to my father who stated to me about this blog, this webpage is
CURRENTEPOCTIME=`date +Y-%m-%d %T”` this is wrong.
CURRENTEPOCTIME=`date +”%Y-%m-%d %T”` this is correct.
Thanks Animesh, Corrected the tutorial.
Hi I need a script to write in pipeline to extend vm where it need to ask for extend life that supports the below format:
NNN-Extend for number of days
N m-Extend for n months
N y- Extend for n years