Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell script to get year, date and month from YYYY-MM-DD format

Tags:

date

shell

format

I am running a shell script which accepts date in "YYYY-MM-DD" format. from this date input, how can i get year, month and day separately?

Thanks for replies in advance.

like image 490
Sunil Avatar asked Aug 31 '25 22:08

Sunil


2 Answers

except for processing the string as text(with grep/sed/awk/cut/...), you could do with with date command:

kent$  date -d '2013-09-06' +%Y
2013

kent$  date -d '2013-09-06' +%m
09

kent$  date -d '2013-09-06' +%d
06
like image 128
Kent Avatar answered Sep 04 '25 00:09

Kent


You could do this to store them on variables with one command:

read YEAR MONTH DAY < <(date -d '2013-09-06' '+%Y %m %d')
printf "%s\n" "$YEAR" "$MONTH" "$DAY"
like image 35
konsolebox Avatar answered Sep 03 '25 22:09

konsolebox