-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)

8
  • 1
    Is it your homeworks? Can you show us the expected output? Mar 21 at 19:57
  • 1
    10Dec2022 at what time? In which timezone? How should it be rounded? Mar 21 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". Mar 22 at 7:14
  • 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"? Mar 22 at 8:19
  • 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 :-) Mar 29 at 10:00

2 Answers 2

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
  • 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. Mar 22 at 7: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.

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