Skip to content
Advertisement

How to get Date Month values in linux date command as integers to work on

I want to convert date and month as integers.

for example.

if the current date as per the command “Date +%m-%d-%y” output, is this 09-11-17 Then I am storing

cur_day=`date +%d`
cur_month=`date +%m`

the $cur_day will give me 11 and $cur_month will give me 09.

I want to do some operations on the month as 09. like i want to print all the numbers up to 09.

like this 01,02,03,04,05,06,07,08,09

Same way I want to display all the numbers up to cur_day

like 01,02,03,04,05,06,07,08,09,10,11

Please tell me how can i do it.

Thanks in Advance.

Advertisement

Answer

For months:

$ printf ',%02d' $(seq 1 $(date +%m)) | sed 's/,/like this /; s/$/n/'
like this 01,02,03,04,05,06,07,08,09

For days:

$ printf ',%02d' $(seq 1 $(date +%d)) | sed 's/,/like /; s/$/n/'
like 01,02,03,04,05,06,07,08,09,10,11

printf will print according to a format. In this case, the format ,%02d formats the numbers with commas and leading zeros.

The sed command puts the string you want at the beginning of the line and adds a newline at the end.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement