HowTo Format Date For Display or Use In a Shell Script

by on February 27, 2007 · 44 comments· LAST UPDATED January 15, 2014

in , ,

How do I format date to display on screen on for my shell scripts as per my requirements on Linux or Unix like operating systems?

You need to use the standard date command to format date or time. You can use the same command with the shell script.


Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Estimated completion time5m
The syntax is

date +FORMAT


date +"%FORMAT"





Task: Display date in mm-dd-yy format

Open a terminal and type the following date command:
$ date +"%m-%d-%y"
Sample output:


To turn on 4 digit year display:
$ date +"%m-%d-%Y"
Just display date as mm/dd/yy format:
$ date +"%D"

Task: Display time only

Type the following command:
$ date +"%T"


To display locale's 12-hour clock time, enter:
$ date +"%r"

07:56:05 PM

To display time in HH:MM format, type:
$ date +"%H-%M"
Sample outputs:


How do I save time/date format to the shell variable?

Simply type the following command at the shell prompt:
$ NOW=$(date +"%m-%d-%Y")
To display a variable use echo / printf command:
$ echo $NOW

A sample shell script

NOW=$(date +"%m-%d-%Y")
echo "Backing up data to /nas42/backup.$NOW.tar.gz file, please wait..."
# rest of script
# tar xcvf /nas42/backup.$NOW.tar.gz /home/ /etc/ /var

A complete list of FORMAT control characters supported by the date command

FORMAT controls the output. It can be the combination of any one of the following:

%FORMAT StringDescription
%%a literal %
%alocale's abbreviated weekday name (e.g., Sun)
%Alocale's full weekday name (e.g., Sunday)
%blocale's abbreviated month name (e.g., Jan)
%Blocale's full month name (e.g., January)
%clocale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%Ccentury; like %Y, except omit last two digits (e.g., 21)
%dday of month (e.g, 01)
%Ddate; same as %m/%d/%y
%eday of month, space padded; same as %_d
%Ffull date; same as %Y-%m-%d
%glast two digits of year of ISO week number (see %G)
%Gyear of ISO week number (see %V); normally useful only with %V
%hsame as %b
%Hhour (00..23)
%Ihour (01..12)
%jday of year (001..366)
%khour ( 0..23)
%lhour ( 1..12)
%mmonth (01..12)
%Mminute (00..59)
%na newline
%Nnanoseconds (000000000..999999999)
%plocale's equivalent of either AM or PM; blank if not known
%Plike %p, but lower case
%rlocale's 12-hour clock time (e.g., 11:11:04 PM)
%R24-hour hour and minute; same as %H:%M
%sseconds since 1970-01-01 00:00:00 UTC
%Ssecond (00..60)
%ta tab
%Ttime; same as %H:%M:%S
%uday of week (1..7); 1 is Monday
%Uweek number of year, with Sunday as first day of week (00..53)
%VISO week number, with Monday as first day of week (01..53)
%wday of week (0..6); 0 is Sunday
%Wweek number of year, with Monday as first day of week (00..53)
%xlocale's date representation (e.g., 12/31/99)
%Xlocale's time representation (e.g., 23:13:48)
%ylast 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)
%:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
%Zalphabetic time zone abbreviation (e.g., EDT)
See also:
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 44 comments… read them below or add one }

1 Michael Shuler February 27, 2007 at 10:38 pm

An alternate that I use for several scripts is to eval $TIME and add useful text – uses similar format statements as date – see the manpage.

$ for node in $( cat nodes ) ; do env TIME=”$node elapsed time: %E” time rsync -az data $node: ; done elapsed time: 0:30.82
beta.lax elapsed time: 1:01.81
gamma.lon elapsed time: 2:39.64


2 noel villeroz March 22, 2007 at 9:07 pm

It’s great and I learned a lot from this topic…


3 neo424 July 13, 2007 at 7:53 am

I need to send my analog hits to my email box automatically.

This is my command set,

cd /home/cpanelaccount/tmp/analog
ls -al
grep id=55 4.html | wc -l | mail -s “csppg hits”

analog create log files 1-12.htm. ex: 4.html for April. After year it replace these files.

How do I determent the month and make my commands automatic send information using cronjob?


4 unique November 12, 2007 at 4:49 am

how can get week of the month?..
ex : 1102 (second week in the november)


5 vij January 11, 2008 at 12:38 pm

how do i comapre time in 07:35:15AM format through shell script


6 Dravid May 29, 2009 at 10:55 am

Nice one.


7 comrad June 17, 2009 at 9:21 am

Oh nice :-) And available via IPv6!


8 ashwani June 18, 2009 at 10:43 am

for space we can use \
see the output
Work Hard buddy [root@server1 04:05 PM]\ date +%F\ %r
2009-06-18 04:05:45 PM


9 Mangesh October 9, 2009 at 7:30 am

How to display yesterday date?


10 nixCraft October 9, 2009 at 8:02 am
11 Vinay December 14, 2011 at 11:08 am

date –date=’yesterday’
date –date=’yesterday’ +%Y-%m-%d


12 Valter Henrique September 6, 2013 at 12:43 pm

Thank you very much, I was looking for it !


13 Me October 30, 2009 at 7:52 am

Thanks a BUNCH !
That was simple and effective for the beginners.


14 Miroslav March 2, 2010 at 2:56 pm

I setup linux script to delete my backups older than few days:
RBRDAN=`date –date=”3 days ago” +%w`

rm /u02/oracle/backup/rman/$RBRDAN/*

But, I got error:
rm: cannot remove `/u02/oracle/backup/rman/6\r/*': No such file or directory

For some reason, it gives me “\r” at the end of the day. Instead of “6”, it gives me “6\r”.
Does anybody know what is the problem.



15 Guy March 24, 2010 at 8:41 pm

Hi everyone!
This is a great document that covers most of my date/time issues :-)

However I am trying to cron a job that will execute my bash script, the challenge that I am facing is I need to run the script for the last 12 hours, here is what I have:

# Set the current local time in this format: ’03/18/2010 17:52:18′
CURR_DATE=`date +%m/%d/%Y” “%H:%M:%S`
#Set the start time (current time – 12 hours) <– This is where I am stuck!!!
START_DATE=`date +%m/%d/%Y" "%T`

How do set the start time to CURR_DATE -12 hours???



16 Guy March 24, 2010 at 8:51 pm

I should mentioned that I am using SunOS 5.10
As I have tried
E=$(date –date=’-12 hours’)
and it’s not working!

I get this error:
date: illegal option — date=-12 hours
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]


17 Guy March 24, 2010 at 9:57 pm

So I got this from a friend, how recommended to use perl:

CURR_DATE=`perl -e ‘use POSIX qw(strftime);$d = strftime “%m/%e/%Y %H:%M:%S”, localtime(time);print $d;’`
START_DATE=` perl -e ‘use POSIX qw(strftime);$d = strftime “%m/%e/%Y %H:%M:%S”, localtime(time() – 43200);print $d;’`

This works great!!


18 Balaswamy vaddeman August 28, 2010 at 9:18 am

it is good one for unix beginners.
I want to display month in aphabets like Jan ,Feb and Mar.
anybody suggest me command for that. thank you in advance.


19 nixCraft August 28, 2010 at 9:26 am

Tested with GNU date:

date +"%b"
date +"%b" --date="1 month ago"
for d in {1..6}; do date +"%b" --date="$d month ago"; done
man date


20 bhavesh September 13, 2010 at 11:42 pm

@Balaswamy : easiest way to print months in alphabets format is using CASE


21 Balaswamy vaddeman September 22, 2010 at 5:19 am

Thank you Bhavesh ,%b option also worked for me.


22 Amit Jain December 15, 2010 at 6:48 am

Thanks a lot it helped.


23 saianth chemate March 5, 2011 at 5:56 am

I was thinking for writing code for the date formate comperison
if any one having any idea abt it plz informe me…….
i willl apriciate that…


24 soumita June 3, 2011 at 7:30 am


I am writing a script which needs 2 arguments – arg1: current date & time,arg2: 5 min after date & time.Can anyone help me how to get after 5 min time of current time in sun 5.10?I have tried with the below syntax, but it is not working.

a=`date “+%Y%m%d %T” -d “+5 min”`


25 Ashish July 7, 2013 at 12:17 pm

a= date “+%Y%m%d%T” -d +5 min”


26 phpsnook July 8, 2011 at 7:25 am

Hi i have a unix date in file a file like this ‘1313675999’
in oracle i would do it like this

select TO_CHAR ( TO_DATE (‘01011970′, ‘DDMMYYYY’)+ 1 / 24 / 60 / 60 * 1313675999,’YYYYMMDD’) from dual

how to achive the same in unix ?


27 phpsnook July 8, 2011 at 11:44 am

echo 1313675999 | perl -e ‘use Time::localtime; $tm=localtime(); printf(“%d/%d/%d\n”, $tm->mday, $tm->mon+1, $tm->year+1900);’


28 Another Guy August 23, 2011 at 7:29 pm

How do I get the day of the week for tomorrow and the day after? For example, if today is Monday, how can I get “Tuesday” and “Wednesday”? I have weather data for day2 and day3 from (for GeekTool) but need the days of the week to apply it to.



29 Lernen January 4, 2012 at 7:34 am

Thank you!! I tried to format a date string for myself but date –help didnt show anything about the “+” which is important for formating…. -.- thanks again!


30 bala June 19, 2012 at 2:03 pm

Thanks a lot… it really helped me..


31 Sathish Sundaravel August 31, 2012 at 6:36 am

How do i print the yesterday date


32 aleci October 3, 2012 at 2:18 pm

Hi guys! This site is really informative but I have one question. Given a specific date entered by the user how can I pass that for the date function to format and display?



33 Cr0t February 27, 2013 at 9:50 pm

that so didn’t work for me…. I wrote a dirty work around

my variable ALLDATES looks like this

for sorttime in $ALLDATES;
date +%s -d “$sorttime” >> $$

ALLDATES=`cat $$ | sort`
rm $$ -f

for sorttime in $ALLDATES;
date -d @$sorttime +%D >> $$

ALLDATES=`cat $$`
rm $$ -f

Yeah…. this is not pretty, but it does the jobs. It converts the time into ctime, sorts it and then puts it back.


34 danmark June 20, 2013 at 2:44 pm

great help for this noob.
works for
dehdate=$(date +%Y%m%d”)
dehdmsg1=”HELLO $dehdate”
echo dehdmsg1
does not work for
dehdmsg2 =”$dehdate HELLO”
echo dehdmsg2
Will do more looking.


35 Rick Carrick July 12, 2013 at 8:32 pm

date +%c –date=”tomorrow”


36 Tsunami Australia September 2, 2013 at 10:26 am

Thanks a heap for this document. I was looking for a method of naming cron’ed backup files on a CentOS webserver so could have backup-*date*-etc, backup-*date*-var_www etc

Using the methods you’ve shown I will be able to automate this backup process using a bash script and cron then just log in via VPN and download the backup files via SAMBA once a week.


37 Krishanu May 21, 2014 at 8:32 pm

I need to move a date file ( for ex, 22-05-2014.csv) from one folder to another folder in Unix. But the problem is that the file must be dynamic ( for ex, if I have to transfer the file today, the transferred file should look like 22052014.csv, if I transfer it tomorrow, it should look like 23052014.csv and so on).

I have the basic idea that I have to move the file date +”%d%m%Y”.csv to my destination folder. But not able to do it. Or rather, I do not know how to do it. Please help.


38 comrad May 22, 2014 at 4:28 am

You have to put the date-command in backticks or $(command).

Then bash executes the command and uses the result to continue.


39 Srikanth May 27, 2014 at 4:56 am

I got what I wanted. Thank you


40 kokane November 19, 2014 at 9:49 am

Great script!


41 notaposter March 13, 2015 at 9:51 pm
 NOW=$(date +"%m-%d-%Y")

does not work in Ubuntu


42 jack March 15, 2015 at 10:34 am

it does work Ubuntu 14.04 LTS. Beside that all distributions got the same binutils….


43 marcus March 17, 2015 at 10:53 am

I want to set a start-date to 2014-01-01
and then go through the whole year but I can’t figure out how to set the start-date to a specific date. someone kind enough to help? :)


44 Prasanna March 26, 2015 at 9:33 am

Can anyone help me get a script to calculate number of months since a date till current date? Ex: No of months till today from July 2013


Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: