-5

Team,

Can someone help find code for below.. Unix: how to get a count of number of days from a given date like 10Dec2022 to today.

Looking for Answer: 102 as of (22Mar2023)

  • 1
    Is it your homeworks? Can you show us the expected output? – Gilles Quénot Mar 21 '23 at 19:57
  • 1
    10Dec2022 at what time? In which timezone? How should it be rounded? – Stéphane Chazelas Mar 21 '23 at 20:15
  • @StéphaneChazelas How is timezone relevant? Has a different number of days passed since 10 Dec 2022 in Tokyo and Washington? Also when asked "how many days till christmas" very few people would answer "281.15423342". – Peregrino69 Mar 22 '23 at 07:14
  • @Peregrino69, yes, there's 14 hours difference between 2022-12-10T00:00:00 Tokyo time and Washington time. 25 hours difference between Pacific/Midway and Pacific/Kiritimati – Stéphane Chazelas Mar 22 '23 at 07:56
  • 2
    @StéphaneChazelas ... Yeah, there is, it's now 21:18 March 21 in Midway, while Kiritimati is 10:18 March 22. But how often is this question interpreted as "How many days have passed in Kiritimati since it was December 22 2022 in Midway"? – Peregrino69 Mar 22 '23 at 08:19
  • @Peregrino69 In times of trans-continental air traffic ... ;) It all depends on circumstances (which the OP however failed to present so far) – AdminBee Mar 29 '23 at 09:41
  • 1
    @AdminBee Right, didn't think of the possibility that the OP's been sitting for months in a plane continuously circulating the Earth eastwards :-D Sometimes questions just are as simple as they seem :-) – Peregrino69 Mar 29 '23 at 10:00
  • @Peregrino69 :D:D:D – AdminBee Mar 29 '23 at 10:01

2 Answers2

2

If you need to fetch the number of days since the start of the year for given date:

With GNU date:

$ date -d '10Dec2022' +%j

With :

$ perl -MTime::Piece -sE '
    say Time::Piece->strptime($date, "%d%b%Y")->strftime("%j")
' -- -date="10Dec2022"

Output

344
  • 1
    Note that %j starts at 1 on January 1st 00:00:00, so strictly speaking, it's more like one plus the number of days since the start of the year. – Stéphane Chazelas Mar 22 '23 at 07:48
1

If it's about getting the number of days since that date, with ddiff from dateutils (sometimes called dateutils.ddiff):

$ date +%F
2023-03-22
$ dateutils.ddiff -i %d%b%Y 10Dec2022 now
102

That's independent of the current time of the day. 0 would be reported from 00:00 to 23:59 local time on 2022-12-10 and increments at midnight.