11

I started working at my current position since November 17th 2014. I would like to know how many days have run up to now. Any ideas on how to use Linux to come up with a simple and nice solution?

  • I ended up doing something similar in a perl script; I used the timelocal() function to get the number of seconds from the epoch to the given date (Nov 17, 2014 for example), then subtracted that from the current epoch-date-in-seconds, then divided by 86400 (seconds in a day). I'm sure someone will come through with a shell-only solution :) – Jeff Schaller Jul 14 '15 at 14:25
  • 2
    Time arithmetic has lots of fiddly special cases. While simple things like subtracting UNIX timestamps and dividing by 86,400 will give you an approximate answer, you are better off using a specialized time library that takes into account the special cases, for example Python's dateutil. – Reid Jul 14 '15 at 16:16
  • 1
    Some of the answers in Quickly calculate date differences may help here too. – manatwork Jul 14 '15 at 18:36
  • @msh210 you are banned for life and sentenced to prison for 1 month while serving community as code cleaner for ancient COBOL code. – ychaouche Apr 12 '17 at 8:46
14
echo $(( (`date +%s` - `date +%s -d '2014/11/17'`) / 86400 )) days ago
  • 4
    This gets leap seconds wrong. I mean, that hardly matters now, but in 10k years or so the accumulated error could be enough to matter. – Kevin Jul 14 '15 at 19:07
  • 2
    The probability leap seconds will trigger an error in about .000001%. This is negligible compared to the wrong number of days this method will give with locales where daylight saving time is observed: 2% of the cases ... – jlliagre Jul 14 '15 at 21:04
  • 2
    @Kevin: If he wants to know how many days he worked somewhere, we can (until further advance in cryogenisation or medicine) not bother too much about this ^^ – Olivier Dulac Jul 15 '15 at 0:37
  • 1
    @jlliagre: %s is Unix time, which is always UTC, which lacks DST. – Kevin Jul 15 '15 at 1:14
  • 1
    Chances are he will be wondering about the number of days employed while idling behind his desk at work. As that's probably not around midnight but somewhere between 8am and 5pm (and date -d 2014/11/17 gives a timestamp of midnight as there's no time mentioned) DST doesn't matter. – wurtel Jul 15 '15 at 7:32
5

Well, on the face of it:

$ date --date="-239 days"
Mon Nov 17 15:25:40 CET 2014

In a script (not very efficient, but... maybe it handles leap seconds? ;) )

i=0
result=""
while [ "$result" != "20141117" ]
do
    i=$((i+1))
    result=$(date --date="-$i days" +%Y%m%d)
done
echo "$i" days have passed since "$result"
  • Even ago is understood: date --date="239 days ago" – Digital Trauma Jul 14 '15 at 19:40
  • 1
    @DigitalTrauma too bad it's not localized. E.g. LC_ALL=ru_RU.utf8 date --date="239 дней назад" gives me the same result as date --date="239". – Ruslan Jul 15 '15 at 4:31
  • @Ruslan indeed :) – Digital Trauma Jul 15 '15 at 5:24
1

I tried python on the command line.

$ python -c "import datetime; print datetime.date.today() - datetime.date(2014,11,17)"
246 days, 0:00:00 
1

You can use the awk Velour library:

$ velour -n 'print t_secday(t_now() - t_utc(2014, 9, 8))'
1385.54

Or:

$ velour -n 'print t_secday(t_now() - t_utc(ARGV[1], ARGV[2], ARGV[3]))' 2014 9 8
1385.54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.