12

Using the command

$ date +%j

gives an output of the day of the year (001-366). However I need to use the day of the current year as an input format i.e.

$ date --date='066 day'  +%F

Which for year 2021 I would expect 2021-03-07 as the output. Instead I get 2021-05-13. Does anyone know what is going on and if there is a way to get what I want using date.

0

2 Answers 2

19

To get the 66th day of the year with GNU date:

$ date --date='jan 1 + 65 days' +%F
2021-03-07

or

$ date --date='dec 31 last year + 66 days' +%F
2021-03-07

With date --date='066 day' +%F you get the date 66 days from today. On the 8th of March, this happens to be the 13th of May.


The above uses GNU date. On OpenBSD (not that you asked, but anyway), you could do something like the following:

$ date -u -r "$(( $(date -ju -f %m%d 0101 +%s) + 65*86400 ))" +%F
2021-03-07

which would be somewhat similar to the first GNU date command above in that it adds some time from January 1st.

3
  • Yes it is GNU date I am using.
    – Brennan
    Commented Mar 8, 2021 at 23:31
  • 4
    I think that the OpenBSD code is wrong in Southern hemisphere timezones with daylight savings time: it skips the day of the DST switch, since 86400 seconds after the start of that day, it's still the same day at 23:00. + $n days with GNU date calculates in calendar days, so its problem is even more subtle: it doesn't handle day skips correctly, e.g. Tokelau: TZ=Pacific/Fakaofo date -d '28 December 2011 + 2 days' → Dec 31. The fix is easy: always use TZ=UTC0 for date calculations. @Brennan Commented Mar 9, 2021 at 13:47
  • @Gilles'SO-stopbeingevil' Fixed by using -u with both date invocations. Thanks.
    – Kusalananda
    Commented Mar 9, 2021 at 18:01
5

I propose this option: if for example you want to know the date of the 80th day:

# take the number of day today
$ n=$(date +%j)
# set some number of days you want to know the date
$ t=80
# subtract the 'n', and add the 't'
$ date -d "-$n day +$t day" %F
2021-03-21

Adapted to your example:

$ n=$(date +%j)
$ t=66
$ date -d "-$n day +$t day" %F
2021-03-07
5
  • That would work for me as well, thanks. Kusalananda's answer is more intuitive for me at least. I tried to upvote your answer anyways but it wouldn't let me because of my rep.
    – Brennan
    Commented Mar 8, 2021 at 23:34
  • @Brennan don't worry! Thanks for your feedback. :) gave you +1. Commented Mar 8, 2021 at 23:36
  • @Brennan You should now have enough reps. to upvote, if you wish to do so. :-)
    – Kusalananda
    Commented Mar 9, 2021 at 12:06
  • 4
    + $n days with GNU date calculates in calendar days, so it mostly avoids DST issues, but it doesn't handle day skips correctly, e.g. Tokelau: TZ=Pacific/Fakaofo date -d '28 December 2011 + 2 days'. The fix is easy: always use TZ=UTC0 for date calculations. Commented Mar 9, 2021 at 13:49
  • Wow. Day skip! And I thought I knew a lot about time zones. Commented Mar 9, 2021 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .