Q. How do I format date to display on screen on for my scripts as per my requirements?
A. You need to use standard date command to format date or time for output or to use in a shell script.
Syntax to specify format
date +FORMAT
Task: Display date in mm-dd-yy format
Type the command as follows:
$ date +"%m-%d-%y"
Output:
02-27-07
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 command as follows:
$ date +"%T"
Output:
19:55:04
Display locale’s 12-hour clock time
$ date +"%r"
Output:
07:56:05 PM
Display time in HH:MM format:
$ date +"%H-%M"
How do I save time/date format to a variable?
Simply type command as follows at a shell prompt:
$ NOW=$(date +"%m-%d-%Y")
To display a variable use echo / printf command:
$ echo $NOW
Sample shell script:
#!/bin/bash NOW=$(date +"%m-%d-%Y") FILE="backup.$NOW.tar.gz" # rest of script
Complete list of FORMAT control characters supported by date command
FORMAT controls the output.It can be the combination of any one of the following:
- %%
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)
See also:
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 31 comments… read them below or add one }
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
alpha.nyc elapsed time: 0:30.82
beta.lax elapsed time: 1:01.81
gamma.lon elapsed time: 2:39.64
$
It’s great and I learned a lot from this topic…
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” my@emailc.om
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?
how can get week of the month?..
ex : 1102 (second week in the november)
how do i comapre time in 07:35:15AM format through shell script
Nice one.
Oh nice :-) And available via IPv6!
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
How to display yesterday date?
Try
Getting Yesterdays or Tomorrows day with shell date command
date –date=’yesterday’
date –date=’yesterday’ +%Y-%m-%d
Thanks a BUNCH !
That was simple and effective for the beginners.
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.
Thanks.
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:
#!/usr/bin/bash
# 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???
Thanks,
guy
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]
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;’`
echo $CURR_DATE
echo $START_DATE
This works great!!
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.
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@Balaswamy : easiest way to print months in alphabets format is using CASE
Thank you Bhavesh ,%b option also worked for me.
Thanks a lot it helped.
hellow…..
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…
Hi,
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”`
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 ?
echo 1313675999 | perl -e ‘use Time::localtime; $tm=localtime(); printf(“%d/%d/%d\n”, $tm->mday, $tm->mon+1, $tm->year+1900);’
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 getwthr.com (for GeekTool) but need the days of the week to apply it to.
Thanks.
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!
Thanks a lot… it really helped me..
How do i print the yesterday date
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?
Thanks!
that so didn’t work for me…. I wrote a dirty work around
my variable ALLDATES looks like this
02/11/13
02/12/13
02/13/13
02/14/13
02/15/13
…
—BEGIN—
for sorttime in $ALLDATES;
do
date +%s -d “$sorttime” >> $$
done
ALLDATES=`cat $$ | sort`
rm $$ -f
for sorttime in $ALLDATES;
do
date -d @$sorttime +%D >> $$
done
ALLDATES=`cat $$`
rm $$ -f
—END—
Yeah…. this is not pretty, but it does the jobs. It converts the time into ctime, sorts it and then puts it back.