Take the 2-minute tour ×

I am trying to get Month and Date from Date in Linux. this is my code

# Set Date 

# get day 

# get day 

# Day 
echo "Day:"$DD
echo "Month:"$MM
share|improve this question

3 Answers 3

up vote 5 down vote accepted

In sh or bash:

DAY=$(date -d "$D" '+%d')
MONTH=$(date -d "$D" '+%m')
YEAR=$(date -d "$D" '+%Y')

echo "Day: $DAY"
echo "Month: $MONTH"
echo "Year: $YEAR"
share|improve this answer

Or if you want the current date, use date +%Y/%m/%d. If you want them separately you can do something like this:

read YYYY MM DD <<<$(date +'%Y %m %d')
echo "Today is Day:$DD Month:$MM"

An easier approach is:

DD=$(date +%d)
MM=$(date +%m)

However in this case you're executing date twice, which is inefficient, and if you're really unlucky, the date could change between those two lines ;)

share|improve this answer
kent$  D="2013/01/17"

kent$  awk -F/ '{print "year:"$1,"Month:"$2,"Day:"$3}'<<<$D
year:2013 Month:01 Day:17

if you want just Month or Day, just leave $2 or $3 there, delete the parts you don't need


kent$  year=$(awk -F/ '{print $1}' <<<$D)                                                                                                                                   

kent$  echo $year
share|improve this answer
Kent , thanks can I store month year and day into variable. I am new to this . Thanks –  user1570210 Jan 23 '13 at 22:45
@user1570210 sure you can. see Edit. I just made it with year. you should know how to deal with month or day. just change $1 to $2 or $3. good luck –  Kent Jan 23 '13 at 22:51
Thanks A lot guys –  user1570210 Jan 23 '13 at 22:59

Your Answer


