-1

For example I gave 31-dec-2020 it should return 365th I tried in bash with below but its not working.

start_of_year= "2020-01-01"
echo "enter date in yyy-mm-dd format"
read current_date
difference=  `expr $current_date - $start_of_year`
echo "day of the year $difference"
oguz ismail
  • 1
  • 16
  • 47
  • 69
DevOpsGeek
  • 302
  • 1
  • 4
  • 15
  • See this post https://stackoverflow.com/questions/10112453/how-to-get-day-of-the-year-in-shell – Haseeb Jehanzeb Jan 28 '20 at 07:51
  • That question was about getting current day of the month using date command, but what i need is random date will be give from user input – DevOpsGeek Jan 28 '20 at 08:01
  • @nullPointer no my question was specific to day of the year. and your post was about difference bw two dates – DevOpsGeek Jan 28 '20 at 08:03
  • Your usage of `expr` does not make sense. The arguments are strings, not numbers. You can't make a difference between strings. See _man expr_. – user1934428 Jan 28 '20 at 08:33

1 Answers1

0
date -d "$current_date" +%j

The %j formatter returns the day number in the year. See man date.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Haven't a clue why you were downvoted. This is the correct answer.```$ date -d "Mar 1 2021" +%j 060 $ date -d "Mar 1 2020" +%j 061``` – bob dylan Jan 28 '20 at 11:11