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?
4 Answers
echo $(( (`date +%s` - `date +%s -d '2014/11/17'`) / 86400 )) days ago
-
5This gets leap seconds wrong. I mean, that hardly matters now, but in 10k years or so the accumulated error could be enough to matter.– KevinCommented Jul 14, 2015 at 19:07
-
2The 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 ...– jlliagreCommented Jul 14, 2015 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 ^^ Commented Jul 15, 2015 at 0:37
-
1@jlliagre: %s is Unix time, which is always UTC, which lacks DST.– KevinCommented Jul 15, 2015 at 1:14
-
1Chances 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.– wurtelCommented Jul 15, 2015 at 7:32
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"
-
-
1@DigitalTrauma too bad it's not localized. E.g.
LC_ALL=ru_RU.utf8 date --date="239 дней назад"
gives me the same result asdate --date="239"
.– RuslanCommented Jul 15, 2015 at 4:31 -
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
Here is example with PHP:
<?php
$o1 = date_create('2014-11-17');
$o2 = date_create();
$o3 = date_diff($o2, $o1);
echo 'days: ', $o3->days, "\n";
Result:
days: 1927
dateutil
.