Quickly calculate date differences

Clash Royale CLAN TAG#URR8PPP
up vote
71
down vote
favorite
I often want to make some quick date calculations, such as:
- What is the difference between these two dates?
- What is the date n weeks after this other date?
I usually open a calendar and count the days, but I think there should be a program/script that I can use to do these kinds of calculations. Any suggestions?
date
add a comment |Â
up vote
71
down vote
favorite
I often want to make some quick date calculations, such as:
- What is the difference between these two dates?
- What is the date n weeks after this other date?
I usually open a calendar and count the days, but I think there should be a program/script that I can use to do these kinds of calculations. Any suggestions?
date
3
See also Tool in UNIX to subtract dates for when GNU date is not available.
â Gilles
Nov 15 '11 at 23:26
add a comment |Â
up vote
71
down vote
favorite
up vote
71
down vote
favorite
I often want to make some quick date calculations, such as:
- What is the difference between these two dates?
- What is the date n weeks after this other date?
I usually open a calendar and count the days, but I think there should be a program/script that I can use to do these kinds of calculations. Any suggestions?
date
I often want to make some quick date calculations, such as:
- What is the difference between these two dates?
- What is the date n weeks after this other date?
I usually open a calendar and count the days, but I think there should be a program/script that I can use to do these kinds of calculations. Any suggestions?
date
date
asked Nov 15 '11 at 11:46
daniel kullmann
5,04272842
5,04272842
3
See also Tool in UNIX to subtract dates for when GNU date is not available.
â Gilles
Nov 15 '11 at 23:26
add a comment |Â
3
See also Tool in UNIX to subtract dates for when GNU date is not available.
â Gilles
Nov 15 '11 at 23:26
3
3
See also Tool in UNIX to subtract dates for when GNU date is not available.
â Gilles
Nov 15 '11 at 23:26
See also Tool in UNIX to subtract dates for when GNU date is not available.
â Gilles
Nov 15 '11 at 23:26
add a comment |Â
14 Answers
14
active
oldest
votes
up vote
97
down vote
accepted
The "n weeks after a date" is easy with GNU date(1):
$ date -d 'now + 3 weeks'
Tue Dec 6 23:58:04 EST 2011
$ date -d 'Aug 4 + 3 weeks'
Thu Aug 25 00:00:00 EST 2011
$ date -d 'Jan 1 1982 + 11 weeks'
Fri Mar 19 00:00:00 EST 1982
I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a shell function.
datediff()
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
$ datediff '1 Nov' '1 Aug'
91 days
Swap d1 and d2 if you want the date calculation the other way, or get a bit fancier to make it not matter. Furthermore, in case there is a non-DST to DST transition in the interval, one of the days will be only 23 hours long; you can compensate by adding ý day to the sum.
echo $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days
add a comment |Â
up vote
35
down vote
For a set of portable tools try my very own dateutils. Your two examples would boil down to one-liners:
ddiff 2011-11-15 2012-04-11
=>
148
or in weeks and days:
ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
21 1
and
dadd 2011-11-15 21w
=>
2012-04-10
3
+1 your tools rock (thoughdateadd -i '%m%d%Y' 01012015 +1ddoesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)
â don_crissti
Nov 22 '15 at 2:17
1
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
add a comment |Â
up vote
28
down vote
A python example for calculating the number of days I've walked the planet:
$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
2
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days"12813ychaouche@ychaouche-PC ~ $
â ychaouche
Jul 14 '15 at 14:56
1
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
add a comment |Â
up vote
9
down vote
I usually prefer having the time/date in unix utime format (number of seconds since the epoch, when the seventies begun, UTC). That way it always boils down to plain subtraction or addition of seconds.
The problem the usually becomes transforming a date/time into this format.
If you have GNU date, you can get it with date '+%s'
At the time of writing, the current time is 1321358027.
To compare with 2011-11-04 (my birthday), date '+%s' -d 2011-11-04, yielding 1320361200. Subtract: expr 1321358027 - 1320361200 gives 996827 seconds, which is expr 996827 / 86400 = 11 days ago.
The problem is converting from utime (1320361200 format) into a date. I don't know of a readily available tool to do this, but it's very simple to do in for instance C or perl.
5
With GNU date,date -d @1234567890converts from seconds since the epoch to whatever date format you specify.
â Gilles
Nov 15 '11 at 23:21
1
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
1
@MattBianco, seeinfo date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂ
â manatwork
Nov 16 '11 at 10:15
add a comment |Â
up vote
5
down vote
This came up when using date -d "$death_date - $y years - $m months - $d days" to get a birth date (for genealogy). That command is WRONG. Months aren't all the same length, so (date + offset) - offset != date. Ages, in year/month/day, are measures going forwards from the date of birth.
$ date --utc -d 'mar 28 1867 +72years +11months +2days'
Fri Mar 1 00:00:00 UTC 1940
$ date --utc -d 'mar 1 1940 -72years -11months -2days'
Sat Mar 30 00:00:00 UTC 1867
# (2 days later than our starting point)
Date gives the correct output in both cases, but in the second case you were asking the wrong question. It matters WHICH 11 months of the year the +/- 11 cover, before adding/subtracting days. For example:
$ date --utc -d 'mar 31 1939 -1month'
Fri Mar 3 00:00:00 UTC 1939
$ date --utc -d 'mar 31 1940 -1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
$ date --utc -d 'jan 31 1940 +1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
For subtracting to be the inverse operation of adding, the order of operations would have to be reversed. Adding adds years, THEN months, THEN days. If subtracting used the opposite order, then you'd get back to your starting point. It doesn't, so you don't, if the days offset crosses a month boundary in a different length month.
If you need to work backwards from an end date and age, you could do it with multiple invocations of date. First subtract the days, then the months, then the years. (I don't think it's safe to combine the years and months in a single date invocation, because of leap years altering the length of February.)
add a comment |Â
up vote
4
down vote
If a graphical tool is OK for you, I heartily recommend qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, IIRC). There you can say e.g.
days(1900-05-21, 1900-01-01)
to get the number of days (140, since 1900 was not a leap year) between the dates, but of course you can also do the same for times:
17:12:45 â 08:45:12
yields 8.4591667 hours or, if you set the output to time formatting, 8:27:33.
That's great, and even more so because qalculate does have a CLI. Tryqalc, thenhelp days.
â Sparhawk
Jul 4 '14 at 12:19
add a comment |Â
up vote
3
down vote
I frequently use SQL for date calculations. For example MySQL, PostgreSQL or SQLite:
bash-4.2$ mysql <<< "select datediff(current_date,'1980-06-14')"
datediff(current_date,'1980-06-14')
11477
bash-4.2$ psql <<< "select current_date-'1980-06-14'"
?column?
----------
11477
(1 row)
bash-4.2$ sqlite2 <<< "select julianday('now')-julianday('1980-06-14');"
11477.3524537035
Other times I just feel in mood for JavaScript. For example SpiderMonkey, WebKit, Seed or Node.js:
bash-4.2$ js -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.477526192131
bash-4.2$ jsc-1 -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.47757960648
bash-4.2$ seed -e '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11477.4776318287
bash-4.2$ node -pe '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11624.520061481482
(Watch out when passing the month to the JavaScript Date object's constructor. Starts with 0.)
add a comment |Â
up vote
3
down vote
Another way to calculate the difference between two dates of the same calendar year you could use this:
date_difference.sh
1 #!/bin/bash
2 DATEfirstnum=`date -d "2014/5/14" +"%j"`
3 DATElastnum=`date -d "12/31/14" +"%j"`
4 DAYSdif=$(($DATElastnum - $DATEfirstnum))
5 echo "$DAYSdif"
- Line 1 declares to the shell which interpreter to use.
- Line 2 assigns the value from the out of
dateto the variable
DATEfirstnum. The-dflag displays the string in a time format in
this case May 14th 2014 and+"%j"tellsdateto format the output
to just the day of the year (1-365). - Line 3 is the same as Line 2 but with a different date and
different format for the string, December 31st, 2014. - Line 4 assigns the value
DAYSdifto the difference of the two
days. - Line 5 displays the value of
DAYSdif.
This works with the GNU version of date, but not on the PC-BSD/FreeBSD version. I installed coreutils from ports tree and used the command /usr/local/bin/gdate instead.
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:DATEfirstnum=$(date -d "$1" +%s)DATElastnum=$(date -d "$2" +%s)Also, this script will not be able to calculate the difference between two different years.+%jrefers to day of year (001..366) so./date_difference.sh 12/31/2001 12/30/2014outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.
â Six
Feb 28 '15 at 13:29
You don't need$inside arithmetic expression:$((DATElastnum - DATEfirstnum))will also work.
â Ruslan
Jun 2 at 14:26
add a comment |Â
up vote
2
down vote
With the help of dannas solutions this can be done in one line with following code:
python -c "from datetime import date as d; print(d.today() - d(2016, 7, 26))"
(Works in both Python 2.x and Python 3.)
1
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
add a comment |Â
up vote
1
down vote
There's also GNU unit's time calculations combined with GNU date:
$ gunits $(gdate +%s)sec-$(gdate +%s -d -1234day)sec 'yr;mo;d;hr;min;s'
3 yr + 4 mo + 16 d + 12 hr + 37 min + 26.751072 s
$ gunits $(gdate +%s -d '2015-1-2 3:45:00')sec-$(gdate +%s -d '2013-5-6 7:43:21')sec 'yr;mo;d;hr;min;s'
1 yr + 7 mo + 27 d + 13 hr + 49 min + 26.206759 s
(gunits is units in Linux, gdate is date)
add a comment |Â
up vote
1
down vote
datediff.sh on github:gist
#!/bin/bash
#Simplest calculator two dates difference. By default in days
# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]
first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")
case "$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=$((60*60));;
"--days" | "-d" | "") period=$((60*60*24));;
esac
datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff
add a comment |Â
up vote
1
down vote
date and bash can do date differences (OS X options shown). Place the latter date first.
echo $((($(date -jf%D "04/03/16" +%s) - $(date -jf%D "03/02/16" +%s)) / 86400))
# 31
add a comment |Â
up vote
0
down vote
You can use the awk Velour library:
velour -n 'print t_secday(t_utc("2017-4-12") - t_utc("2017-4-5"))'
Or:
velour -n 'print t_secday(t_utc(ARGV[1]) - t_utc(ARGV[2]))' 2017-4-12 2017-4-5
Result:
7
add a comment |Â
up vote
0
down vote
Can someone explain those strange results ?????
$ date -d "10/30/2018 8:00:00 + 3 hours"
Tue Oct 30 07:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 + 0 hours + 2 hours"
Tue Oct 30 12:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 - 01:00"
Tue Oct 30 10:00:00 CET 2018
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
97
down vote
accepted
The "n weeks after a date" is easy with GNU date(1):
$ date -d 'now + 3 weeks'
Tue Dec 6 23:58:04 EST 2011
$ date -d 'Aug 4 + 3 weeks'
Thu Aug 25 00:00:00 EST 2011
$ date -d 'Jan 1 1982 + 11 weeks'
Fri Mar 19 00:00:00 EST 1982
I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a shell function.
datediff()
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
$ datediff '1 Nov' '1 Aug'
91 days
Swap d1 and d2 if you want the date calculation the other way, or get a bit fancier to make it not matter. Furthermore, in case there is a non-DST to DST transition in the interval, one of the days will be only 23 hours long; you can compensate by adding ý day to the sum.
echo $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days
add a comment |Â
up vote
97
down vote
accepted
The "n weeks after a date" is easy with GNU date(1):
$ date -d 'now + 3 weeks'
Tue Dec 6 23:58:04 EST 2011
$ date -d 'Aug 4 + 3 weeks'
Thu Aug 25 00:00:00 EST 2011
$ date -d 'Jan 1 1982 + 11 weeks'
Fri Mar 19 00:00:00 EST 1982
I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a shell function.
datediff()
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
$ datediff '1 Nov' '1 Aug'
91 days
Swap d1 and d2 if you want the date calculation the other way, or get a bit fancier to make it not matter. Furthermore, in case there is a non-DST to DST transition in the interval, one of the days will be only 23 hours long; you can compensate by adding ý day to the sum.
echo $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days
add a comment |Â
up vote
97
down vote
accepted
up vote
97
down vote
accepted
The "n weeks after a date" is easy with GNU date(1):
$ date -d 'now + 3 weeks'
Tue Dec 6 23:58:04 EST 2011
$ date -d 'Aug 4 + 3 weeks'
Thu Aug 25 00:00:00 EST 2011
$ date -d 'Jan 1 1982 + 11 weeks'
Fri Mar 19 00:00:00 EST 1982
I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a shell function.
datediff()
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
$ datediff '1 Nov' '1 Aug'
91 days
Swap d1 and d2 if you want the date calculation the other way, or get a bit fancier to make it not matter. Furthermore, in case there is a non-DST to DST transition in the interval, one of the days will be only 23 hours long; you can compensate by adding ý day to the sum.
echo $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days
The "n weeks after a date" is easy with GNU date(1):
$ date -d 'now + 3 weeks'
Tue Dec 6 23:58:04 EST 2011
$ date -d 'Aug 4 + 3 weeks'
Thu Aug 25 00:00:00 EST 2011
$ date -d 'Jan 1 1982 + 11 weeks'
Fri Mar 19 00:00:00 EST 1982
I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a shell function.
datediff()
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
$ datediff '1 Nov' '1 Aug'
91 days
Swap d1 and d2 if you want the date calculation the other way, or get a bit fancier to make it not matter. Furthermore, in case there is a non-DST to DST transition in the interval, one of the days will be only 23 hours long; you can compensate by adding ý day to the sum.
echo $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days
edited Nov 15 '11 at 23:24
Gilles
515k12210231552
515k12210231552
answered Nov 15 '11 at 13:05
camh
23.9k66051
23.9k66051
add a comment |Â
add a comment |Â
up vote
35
down vote
For a set of portable tools try my very own dateutils. Your two examples would boil down to one-liners:
ddiff 2011-11-15 2012-04-11
=>
148
or in weeks and days:
ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
21 1
and
dadd 2011-11-15 21w
=>
2012-04-10
3
+1 your tools rock (thoughdateadd -i '%m%d%Y' 01012015 +1ddoesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)
â don_crissti
Nov 22 '15 at 2:17
1
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
add a comment |Â
up vote
35
down vote
For a set of portable tools try my very own dateutils. Your two examples would boil down to one-liners:
ddiff 2011-11-15 2012-04-11
=>
148
or in weeks and days:
ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
21 1
and
dadd 2011-11-15 21w
=>
2012-04-10
3
+1 your tools rock (thoughdateadd -i '%m%d%Y' 01012015 +1ddoesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)
â don_crissti
Nov 22 '15 at 2:17
1
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
add a comment |Â
up vote
35
down vote
up vote
35
down vote
For a set of portable tools try my very own dateutils. Your two examples would boil down to one-liners:
ddiff 2011-11-15 2012-04-11
=>
148
or in weeks and days:
ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
21 1
and
dadd 2011-11-15 21w
=>
2012-04-10
For a set of portable tools try my very own dateutils. Your two examples would boil down to one-liners:
ddiff 2011-11-15 2012-04-11
=>
148
or in weeks and days:
ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
21 1
and
dadd 2011-11-15 21w
=>
2012-04-10
answered Apr 11 '12 at 6:37
hroptatyr
82188
82188
3
+1 your tools rock (thoughdateadd -i '%m%d%Y' 01012015 +1ddoesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)
â don_crissti
Nov 22 '15 at 2:17
1
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
add a comment |Â
3
+1 your tools rock (thoughdateadd -i '%m%d%Y' 01012015 +1ddoesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)
â don_crissti
Nov 22 '15 at 2:17
1
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
3
3
+1 your tools rock (though
dateadd -i '%m%d%Y' 01012015 +1d doesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)â don_crissti
Nov 22 '15 at 2:17
+1 your tools rock (though
dateadd -i '%m%d%Y' 01012015 +1d doesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?)â don_crissti
Nov 22 '15 at 2:17
1
1
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
@don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98)
â hroptatyr
Nov 23 '15 at 6:21
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
Do you have window's binary distrib of dateutils?
â mosh
Feb 12 '17 at 4:18
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
@mosh no, and I don't have the facilities to try.
â hroptatyr
Feb 12 '17 at 8:21
add a comment |Â
up vote
28
down vote
A python example for calculating the number of days I've walked the planet:
$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
2
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days"12813ychaouche@ychaouche-PC ~ $
â ychaouche
Jul 14 '15 at 14:56
1
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
add a comment |Â
up vote
28
down vote
A python example for calculating the number of days I've walked the planet:
$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
2
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days"12813ychaouche@ychaouche-PC ~ $
â ychaouche
Jul 14 '15 at 14:56
1
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
add a comment |Â
up vote
28
down vote
up vote
28
down vote
A python example for calculating the number of days I've walked the planet:
$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
A python example for calculating the number of days I've walked the planet:
$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
answered Nov 15 '11 at 12:25
Daniel Näslund
1,07211016
1,07211016
2
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days"12813ychaouche@ychaouche-PC ~ $
â ychaouche
Jul 14 '15 at 14:56
1
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
add a comment |Â
2
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days"12813ychaouche@ychaouche-PC ~ $
â ychaouche
Jul 14 '15 at 14:56
1
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
2
2
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :
ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days" 12813 ychaouche@ychaouche-PC ~ $â ychaouche
Jul 14 '15 at 14:56
Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter :
ychaouche@ychaouche-PC ~ $ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days" 12813 ychaouche@ychaouche-PC ~ $â ychaouche
Jul 14 '15 at 14:56
1
1
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required
â Kiran Telukunta
Oct 14 '16 at 5:18
add a comment |Â
up vote
9
down vote
I usually prefer having the time/date in unix utime format (number of seconds since the epoch, when the seventies begun, UTC). That way it always boils down to plain subtraction or addition of seconds.
The problem the usually becomes transforming a date/time into this format.
If you have GNU date, you can get it with date '+%s'
At the time of writing, the current time is 1321358027.
To compare with 2011-11-04 (my birthday), date '+%s' -d 2011-11-04, yielding 1320361200. Subtract: expr 1321358027 - 1320361200 gives 996827 seconds, which is expr 996827 / 86400 = 11 days ago.
The problem is converting from utime (1320361200 format) into a date. I don't know of a readily available tool to do this, but it's very simple to do in for instance C or perl.
5
With GNU date,date -d @1234567890converts from seconds since the epoch to whatever date format you specify.
â Gilles
Nov 15 '11 at 23:21
1
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
1
@MattBianco, seeinfo date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂ
â manatwork
Nov 16 '11 at 10:15
add a comment |Â
up vote
9
down vote
I usually prefer having the time/date in unix utime format (number of seconds since the epoch, when the seventies begun, UTC). That way it always boils down to plain subtraction or addition of seconds.
The problem the usually becomes transforming a date/time into this format.
If you have GNU date, you can get it with date '+%s'
At the time of writing, the current time is 1321358027.
To compare with 2011-11-04 (my birthday), date '+%s' -d 2011-11-04, yielding 1320361200. Subtract: expr 1321358027 - 1320361200 gives 996827 seconds, which is expr 996827 / 86400 = 11 days ago.
The problem is converting from utime (1320361200 format) into a date. I don't know of a readily available tool to do this, but it's very simple to do in for instance C or perl.
5
With GNU date,date -d @1234567890converts from seconds since the epoch to whatever date format you specify.
â Gilles
Nov 15 '11 at 23:21
1
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
1
@MattBianco, seeinfo date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂ
â manatwork
Nov 16 '11 at 10:15
add a comment |Â
up vote
9
down vote
up vote
9
down vote
I usually prefer having the time/date in unix utime format (number of seconds since the epoch, when the seventies begun, UTC). That way it always boils down to plain subtraction or addition of seconds.
The problem the usually becomes transforming a date/time into this format.
If you have GNU date, you can get it with date '+%s'
At the time of writing, the current time is 1321358027.
To compare with 2011-11-04 (my birthday), date '+%s' -d 2011-11-04, yielding 1320361200. Subtract: expr 1321358027 - 1320361200 gives 996827 seconds, which is expr 996827 / 86400 = 11 days ago.
The problem is converting from utime (1320361200 format) into a date. I don't know of a readily available tool to do this, but it's very simple to do in for instance C or perl.
I usually prefer having the time/date in unix utime format (number of seconds since the epoch, when the seventies begun, UTC). That way it always boils down to plain subtraction or addition of seconds.
The problem the usually becomes transforming a date/time into this format.
If you have GNU date, you can get it with date '+%s'
At the time of writing, the current time is 1321358027.
To compare with 2011-11-04 (my birthday), date '+%s' -d 2011-11-04, yielding 1320361200. Subtract: expr 1321358027 - 1320361200 gives 996827 seconds, which is expr 996827 / 86400 = 11 days ago.
The problem is converting from utime (1320361200 format) into a date. I don't know of a readily available tool to do this, but it's very simple to do in for instance C or perl.
edited Nov 16 '11 at 9:46
answered Nov 15 '11 at 12:02
MattBianco
2,19731839
2,19731839
5
With GNU date,date -d @1234567890converts from seconds since the epoch to whatever date format you specify.
â Gilles
Nov 15 '11 at 23:21
1
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
1
@MattBianco, seeinfo date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂ
â manatwork
Nov 16 '11 at 10:15
add a comment |Â
5
With GNU date,date -d @1234567890converts from seconds since the epoch to whatever date format you specify.
â Gilles
Nov 15 '11 at 23:21
1
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
1
@MattBianco, seeinfo date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂ
â manatwork
Nov 16 '11 at 10:15
5
5
With GNU date,
date -d @1234567890 converts from seconds since the epoch to whatever date format you specify.â Gilles
Nov 15 '11 at 23:21
With GNU date,
date -d @1234567890 converts from seconds since the epoch to whatever date format you specify.â Gilles
Nov 15 '11 at 23:21
1
1
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
@Gilles: that is brilliant. Couldn't find that on the manpage. Where did you learn that?
â MattBianco
Nov 16 '11 at 9:48
1
1
@MattBianco, see
info date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂâ manatwork
Nov 16 '11 at 10:15
@MattBianco, see
info date, especially the Seconds since the Epoch node: âÂÂIf you precede a number with `@', it represents an internal time stamp as a count of seconds.âÂÂâ manatwork
Nov 16 '11 at 10:15
add a comment |Â
up vote
5
down vote
This came up when using date -d "$death_date - $y years - $m months - $d days" to get a birth date (for genealogy). That command is WRONG. Months aren't all the same length, so (date + offset) - offset != date. Ages, in year/month/day, are measures going forwards from the date of birth.
$ date --utc -d 'mar 28 1867 +72years +11months +2days'
Fri Mar 1 00:00:00 UTC 1940
$ date --utc -d 'mar 1 1940 -72years -11months -2days'
Sat Mar 30 00:00:00 UTC 1867
# (2 days later than our starting point)
Date gives the correct output in both cases, but in the second case you were asking the wrong question. It matters WHICH 11 months of the year the +/- 11 cover, before adding/subtracting days. For example:
$ date --utc -d 'mar 31 1939 -1month'
Fri Mar 3 00:00:00 UTC 1939
$ date --utc -d 'mar 31 1940 -1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
$ date --utc -d 'jan 31 1940 +1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
For subtracting to be the inverse operation of adding, the order of operations would have to be reversed. Adding adds years, THEN months, THEN days. If subtracting used the opposite order, then you'd get back to your starting point. It doesn't, so you don't, if the days offset crosses a month boundary in a different length month.
If you need to work backwards from an end date and age, you could do it with multiple invocations of date. First subtract the days, then the months, then the years. (I don't think it's safe to combine the years and months in a single date invocation, because of leap years altering the length of February.)
add a comment |Â
up vote
5
down vote
This came up when using date -d "$death_date - $y years - $m months - $d days" to get a birth date (for genealogy). That command is WRONG. Months aren't all the same length, so (date + offset) - offset != date. Ages, in year/month/day, are measures going forwards from the date of birth.
$ date --utc -d 'mar 28 1867 +72years +11months +2days'
Fri Mar 1 00:00:00 UTC 1940
$ date --utc -d 'mar 1 1940 -72years -11months -2days'
Sat Mar 30 00:00:00 UTC 1867
# (2 days later than our starting point)
Date gives the correct output in both cases, but in the second case you were asking the wrong question. It matters WHICH 11 months of the year the +/- 11 cover, before adding/subtracting days. For example:
$ date --utc -d 'mar 31 1939 -1month'
Fri Mar 3 00:00:00 UTC 1939
$ date --utc -d 'mar 31 1940 -1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
$ date --utc -d 'jan 31 1940 +1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
For subtracting to be the inverse operation of adding, the order of operations would have to be reversed. Adding adds years, THEN months, THEN days. If subtracting used the opposite order, then you'd get back to your starting point. It doesn't, so you don't, if the days offset crosses a month boundary in a different length month.
If you need to work backwards from an end date and age, you could do it with multiple invocations of date. First subtract the days, then the months, then the years. (I don't think it's safe to combine the years and months in a single date invocation, because of leap years altering the length of February.)
add a comment |Â
up vote
5
down vote
up vote
5
down vote
This came up when using date -d "$death_date - $y years - $m months - $d days" to get a birth date (for genealogy). That command is WRONG. Months aren't all the same length, so (date + offset) - offset != date. Ages, in year/month/day, are measures going forwards from the date of birth.
$ date --utc -d 'mar 28 1867 +72years +11months +2days'
Fri Mar 1 00:00:00 UTC 1940
$ date --utc -d 'mar 1 1940 -72years -11months -2days'
Sat Mar 30 00:00:00 UTC 1867
# (2 days later than our starting point)
Date gives the correct output in both cases, but in the second case you were asking the wrong question. It matters WHICH 11 months of the year the +/- 11 cover, before adding/subtracting days. For example:
$ date --utc -d 'mar 31 1939 -1month'
Fri Mar 3 00:00:00 UTC 1939
$ date --utc -d 'mar 31 1940 -1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
$ date --utc -d 'jan 31 1940 +1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
For subtracting to be the inverse operation of adding, the order of operations would have to be reversed. Adding adds years, THEN months, THEN days. If subtracting used the opposite order, then you'd get back to your starting point. It doesn't, so you don't, if the days offset crosses a month boundary in a different length month.
If you need to work backwards from an end date and age, you could do it with multiple invocations of date. First subtract the days, then the months, then the years. (I don't think it's safe to combine the years and months in a single date invocation, because of leap years altering the length of February.)
This came up when using date -d "$death_date - $y years - $m months - $d days" to get a birth date (for genealogy). That command is WRONG. Months aren't all the same length, so (date + offset) - offset != date. Ages, in year/month/day, are measures going forwards from the date of birth.
$ date --utc -d 'mar 28 1867 +72years +11months +2days'
Fri Mar 1 00:00:00 UTC 1940
$ date --utc -d 'mar 1 1940 -72years -11months -2days'
Sat Mar 30 00:00:00 UTC 1867
# (2 days later than our starting point)
Date gives the correct output in both cases, but in the second case you were asking the wrong question. It matters WHICH 11 months of the year the +/- 11 cover, before adding/subtracting days. For example:
$ date --utc -d 'mar 31 1939 -1month'
Fri Mar 3 00:00:00 UTC 1939
$ date --utc -d 'mar 31 1940 -1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
$ date --utc -d 'jan 31 1940 +1month' # leap year
Sat Mar 2 00:00:00 UTC 1940
For subtracting to be the inverse operation of adding, the order of operations would have to be reversed. Adding adds years, THEN months, THEN days. If subtracting used the opposite order, then you'd get back to your starting point. It doesn't, so you don't, if the days offset crosses a month boundary in a different length month.
If you need to work backwards from an end date and age, you could do it with multiple invocations of date. First subtract the days, then the months, then the years. (I don't think it's safe to combine the years and months in a single date invocation, because of leap years altering the length of February.)
answered Jan 31 '15 at 3:39
Peter Cordes
4,1331132
4,1331132
add a comment |Â
add a comment |Â
up vote
4
down vote
If a graphical tool is OK for you, I heartily recommend qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, IIRC). There you can say e.g.
days(1900-05-21, 1900-01-01)
to get the number of days (140, since 1900 was not a leap year) between the dates, but of course you can also do the same for times:
17:12:45 â 08:45:12
yields 8.4591667 hours or, if you set the output to time formatting, 8:27:33.
That's great, and even more so because qalculate does have a CLI. Tryqalc, thenhelp days.
â Sparhawk
Jul 4 '14 at 12:19
add a comment |Â
up vote
4
down vote
If a graphical tool is OK for you, I heartily recommend qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, IIRC). There you can say e.g.
days(1900-05-21, 1900-01-01)
to get the number of days (140, since 1900 was not a leap year) between the dates, but of course you can also do the same for times:
17:12:45 â 08:45:12
yields 8.4591667 hours or, if you set the output to time formatting, 8:27:33.
That's great, and even more so because qalculate does have a CLI. Tryqalc, thenhelp days.
â Sparhawk
Jul 4 '14 at 12:19
add a comment |Â
up vote
4
down vote
up vote
4
down vote
If a graphical tool is OK for you, I heartily recommend qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, IIRC). There you can say e.g.
days(1900-05-21, 1900-01-01)
to get the number of days (140, since 1900 was not a leap year) between the dates, but of course you can also do the same for times:
17:12:45 â 08:45:12
yields 8.4591667 hours or, if you set the output to time formatting, 8:27:33.
If a graphical tool is OK for you, I heartily recommend qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, IIRC). There you can say e.g.
days(1900-05-21, 1900-01-01)
to get the number of days (140, since 1900 was not a leap year) between the dates, but of course you can also do the same for times:
17:12:45 â 08:45:12
yields 8.4591667 hours or, if you set the output to time formatting, 8:27:33.
answered Aug 29 '13 at 7:30
quazgar
356211
356211
That's great, and even more so because qalculate does have a CLI. Tryqalc, thenhelp days.
â Sparhawk
Jul 4 '14 at 12:19
add a comment |Â
That's great, and even more so because qalculate does have a CLI. Tryqalc, thenhelp days.
â Sparhawk
Jul 4 '14 at 12:19
That's great, and even more so because qalculate does have a CLI. Try
qalc, then help days.â Sparhawk
Jul 4 '14 at 12:19
That's great, and even more so because qalculate does have a CLI. Try
qalc, then help days.â Sparhawk
Jul 4 '14 at 12:19
add a comment |Â
up vote
3
down vote
I frequently use SQL for date calculations. For example MySQL, PostgreSQL or SQLite:
bash-4.2$ mysql <<< "select datediff(current_date,'1980-06-14')"
datediff(current_date,'1980-06-14')
11477
bash-4.2$ psql <<< "select current_date-'1980-06-14'"
?column?
----------
11477
(1 row)
bash-4.2$ sqlite2 <<< "select julianday('now')-julianday('1980-06-14');"
11477.3524537035
Other times I just feel in mood for JavaScript. For example SpiderMonkey, WebKit, Seed or Node.js:
bash-4.2$ js -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.477526192131
bash-4.2$ jsc-1 -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.47757960648
bash-4.2$ seed -e '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11477.4776318287
bash-4.2$ node -pe '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11624.520061481482
(Watch out when passing the month to the JavaScript Date object's constructor. Starts with 0.)
add a comment |Â
up vote
3
down vote
I frequently use SQL for date calculations. For example MySQL, PostgreSQL or SQLite:
bash-4.2$ mysql <<< "select datediff(current_date,'1980-06-14')"
datediff(current_date,'1980-06-14')
11477
bash-4.2$ psql <<< "select current_date-'1980-06-14'"
?column?
----------
11477
(1 row)
bash-4.2$ sqlite2 <<< "select julianday('now')-julianday('1980-06-14');"
11477.3524537035
Other times I just feel in mood for JavaScript. For example SpiderMonkey, WebKit, Seed or Node.js:
bash-4.2$ js -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.477526192131
bash-4.2$ jsc-1 -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.47757960648
bash-4.2$ seed -e '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11477.4776318287
bash-4.2$ node -pe '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11624.520061481482
(Watch out when passing the month to the JavaScript Date object's constructor. Starts with 0.)
add a comment |Â
up vote
3
down vote
up vote
3
down vote
I frequently use SQL for date calculations. For example MySQL, PostgreSQL or SQLite:
bash-4.2$ mysql <<< "select datediff(current_date,'1980-06-14')"
datediff(current_date,'1980-06-14')
11477
bash-4.2$ psql <<< "select current_date-'1980-06-14'"
?column?
----------
11477
(1 row)
bash-4.2$ sqlite2 <<< "select julianday('now')-julianday('1980-06-14');"
11477.3524537035
Other times I just feel in mood for JavaScript. For example SpiderMonkey, WebKit, Seed or Node.js:
bash-4.2$ js -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.477526192131
bash-4.2$ jsc-1 -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.47757960648
bash-4.2$ seed -e '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11477.4776318287
bash-4.2$ node -pe '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11624.520061481482
(Watch out when passing the month to the JavaScript Date object's constructor. Starts with 0.)
I frequently use SQL for date calculations. For example MySQL, PostgreSQL or SQLite:
bash-4.2$ mysql <<< "select datediff(current_date,'1980-06-14')"
datediff(current_date,'1980-06-14')
11477
bash-4.2$ psql <<< "select current_date-'1980-06-14'"
?column?
----------
11477
(1 row)
bash-4.2$ sqlite2 <<< "select julianday('now')-julianday('1980-06-14');"
11477.3524537035
Other times I just feel in mood for JavaScript. For example SpiderMonkey, WebKit, Seed or Node.js:
bash-4.2$ js -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.477526192131
bash-4.2$ jsc-1 -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.47757960648
bash-4.2$ seed -e '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11477.4776318287
bash-4.2$ node -pe '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11624.520061481482
(Watch out when passing the month to the JavaScript Date object's constructor. Starts with 0.)
edited Apr 11 '12 at 9:28
answered Nov 16 '11 at 8:35
manatwork
21.2k38284
21.2k38284
add a comment |Â
add a comment |Â
up vote
3
down vote
Another way to calculate the difference between two dates of the same calendar year you could use this:
date_difference.sh
1 #!/bin/bash
2 DATEfirstnum=`date -d "2014/5/14" +"%j"`
3 DATElastnum=`date -d "12/31/14" +"%j"`
4 DAYSdif=$(($DATElastnum - $DATEfirstnum))
5 echo "$DAYSdif"
- Line 1 declares to the shell which interpreter to use.
- Line 2 assigns the value from the out of
dateto the variable
DATEfirstnum. The-dflag displays the string in a time format in
this case May 14th 2014 and+"%j"tellsdateto format the output
to just the day of the year (1-365). - Line 3 is the same as Line 2 but with a different date and
different format for the string, December 31st, 2014. - Line 4 assigns the value
DAYSdifto the difference of the two
days. - Line 5 displays the value of
DAYSdif.
This works with the GNU version of date, but not on the PC-BSD/FreeBSD version. I installed coreutils from ports tree and used the command /usr/local/bin/gdate instead.
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:DATEfirstnum=$(date -d "$1" +%s)DATElastnum=$(date -d "$2" +%s)Also, this script will not be able to calculate the difference between two different years.+%jrefers to day of year (001..366) so./date_difference.sh 12/31/2001 12/30/2014outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.
â Six
Feb 28 '15 at 13:29
You don't need$inside arithmetic expression:$((DATElastnum - DATEfirstnum))will also work.
â Ruslan
Jun 2 at 14:26
add a comment |Â
up vote
3
down vote
Another way to calculate the difference between two dates of the same calendar year you could use this:
date_difference.sh
1 #!/bin/bash
2 DATEfirstnum=`date -d "2014/5/14" +"%j"`
3 DATElastnum=`date -d "12/31/14" +"%j"`
4 DAYSdif=$(($DATElastnum - $DATEfirstnum))
5 echo "$DAYSdif"
- Line 1 declares to the shell which interpreter to use.
- Line 2 assigns the value from the out of
dateto the variable
DATEfirstnum. The-dflag displays the string in a time format in
this case May 14th 2014 and+"%j"tellsdateto format the output
to just the day of the year (1-365). - Line 3 is the same as Line 2 but with a different date and
different format for the string, December 31st, 2014. - Line 4 assigns the value
DAYSdifto the difference of the two
days. - Line 5 displays the value of
DAYSdif.
This works with the GNU version of date, but not on the PC-BSD/FreeBSD version. I installed coreutils from ports tree and used the command /usr/local/bin/gdate instead.
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:DATEfirstnum=$(date -d "$1" +%s)DATElastnum=$(date -d "$2" +%s)Also, this script will not be able to calculate the difference between two different years.+%jrefers to day of year (001..366) so./date_difference.sh 12/31/2001 12/30/2014outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.
â Six
Feb 28 '15 at 13:29
You don't need$inside arithmetic expression:$((DATElastnum - DATEfirstnum))will also work.
â Ruslan
Jun 2 at 14:26
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Another way to calculate the difference between two dates of the same calendar year you could use this:
date_difference.sh
1 #!/bin/bash
2 DATEfirstnum=`date -d "2014/5/14" +"%j"`
3 DATElastnum=`date -d "12/31/14" +"%j"`
4 DAYSdif=$(($DATElastnum - $DATEfirstnum))
5 echo "$DAYSdif"
- Line 1 declares to the shell which interpreter to use.
- Line 2 assigns the value from the out of
dateto the variable
DATEfirstnum. The-dflag displays the string in a time format in
this case May 14th 2014 and+"%j"tellsdateto format the output
to just the day of the year (1-365). - Line 3 is the same as Line 2 but with a different date and
different format for the string, December 31st, 2014. - Line 4 assigns the value
DAYSdifto the difference of the two
days. - Line 5 displays the value of
DAYSdif.
This works with the GNU version of date, but not on the PC-BSD/FreeBSD version. I installed coreutils from ports tree and used the command /usr/local/bin/gdate instead.
Another way to calculate the difference between two dates of the same calendar year you could use this:
date_difference.sh
1 #!/bin/bash
2 DATEfirstnum=`date -d "2014/5/14" +"%j"`
3 DATElastnum=`date -d "12/31/14" +"%j"`
4 DAYSdif=$(($DATElastnum - $DATEfirstnum))
5 echo "$DAYSdif"
- Line 1 declares to the shell which interpreter to use.
- Line 2 assigns the value from the out of
dateto the variable
DATEfirstnum. The-dflag displays the string in a time format in
this case May 14th 2014 and+"%j"tellsdateto format the output
to just the day of the year (1-365). - Line 3 is the same as Line 2 but with a different date and
different format for the string, December 31st, 2014. - Line 4 assigns the value
DAYSdifto the difference of the two
days. - Line 5 displays the value of
DAYSdif.
This works with the GNU version of date, but not on the PC-BSD/FreeBSD version. I installed coreutils from ports tree and used the command /usr/local/bin/gdate instead.
edited Oct 18 '16 at 11:00
Stéphane Chazelas
289k54535874
289k54535874
answered Dec 31 '14 at 17:32
Justin Holcomb
311
311
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:DATEfirstnum=$(date -d "$1" +%s)DATElastnum=$(date -d "$2" +%s)Also, this script will not be able to calculate the difference between two different years.+%jrefers to day of year (001..366) so./date_difference.sh 12/31/2001 12/30/2014outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.
â Six
Feb 28 '15 at 13:29
You don't need$inside arithmetic expression:$((DATElastnum - DATEfirstnum))will also work.
â Ruslan
Jun 2 at 14:26
add a comment |Â
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:DATEfirstnum=$(date -d "$1" +%s)DATElastnum=$(date -d "$2" +%s)Also, this script will not be able to calculate the difference between two different years.+%jrefers to day of year (001..366) so./date_difference.sh 12/31/2001 12/30/2014outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.
â Six
Feb 28 '15 at 13:29
You don't need$inside arithmetic expression:$((DATElastnum - DATEfirstnum))will also work.
â Ruslan
Jun 2 at 14:26
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:
DATEfirstnum=$(date -d "$1" +%s) DATElastnum=$(date -d "$2" +%s) Also, this script will not be able to calculate the difference between two different years. +%j refers to day of year (001..366) so ./date_difference.sh 12/31/2001 12/30/2014 outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.â Six
Feb 28 '15 at 13:29
This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this:
DATEfirstnum=$(date -d "$1" +%s) DATElastnum=$(date -d "$2" +%s) Also, this script will not be able to calculate the difference between two different years. +%j refers to day of year (001..366) so ./date_difference.sh 12/31/2001 12/30/2014 outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC.â Six
Feb 28 '15 at 13:29
You don't need
$ inside arithmetic expression: $((DATElastnum - DATEfirstnum)) will also work.â Ruslan
Jun 2 at 14:26
You don't need
$ inside arithmetic expression: $((DATElastnum - DATEfirstnum)) will also work.â Ruslan
Jun 2 at 14:26
add a comment |Â
up vote
2
down vote
With the help of dannas solutions this can be done in one line with following code:
python -c "from datetime import date as d; print(d.today() - d(2016, 7, 26))"
(Works in both Python 2.x and Python 3.)
1
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
add a comment |Â
up vote
2
down vote
With the help of dannas solutions this can be done in one line with following code:
python -c "from datetime import date as d; print(d.today() - d(2016, 7, 26))"
(Works in both Python 2.x and Python 3.)
1
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With the help of dannas solutions this can be done in one line with following code:
python -c "from datetime import date as d; print(d.today() - d(2016, 7, 26))"
(Works in both Python 2.x and Python 3.)
With the help of dannas solutions this can be done in one line with following code:
python -c "from datetime import date as d; print(d.today() - d(2016, 7, 26))"
(Works in both Python 2.x and Python 3.)
edited Mar 17 '17 at 15:23
ilkkachu
52.9k680145
52.9k680145
answered Mar 17 '17 at 13:59
Kiran Telukunta
1263
1263
1
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
add a comment |Â
1
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
1
1
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
You can edit your post instead of commenting
â Stephen Rauch
Mar 17 '17 at 14:11
add a comment |Â
up vote
1
down vote
There's also GNU unit's time calculations combined with GNU date:
$ gunits $(gdate +%s)sec-$(gdate +%s -d -1234day)sec 'yr;mo;d;hr;min;s'
3 yr + 4 mo + 16 d + 12 hr + 37 min + 26.751072 s
$ gunits $(gdate +%s -d '2015-1-2 3:45:00')sec-$(gdate +%s -d '2013-5-6 7:43:21')sec 'yr;mo;d;hr;min;s'
1 yr + 7 mo + 27 d + 13 hr + 49 min + 26.206759 s
(gunits is units in Linux, gdate is date)
add a comment |Â
up vote
1
down vote
There's also GNU unit's time calculations combined with GNU date:
$ gunits $(gdate +%s)sec-$(gdate +%s -d -1234day)sec 'yr;mo;d;hr;min;s'
3 yr + 4 mo + 16 d + 12 hr + 37 min + 26.751072 s
$ gunits $(gdate +%s -d '2015-1-2 3:45:00')sec-$(gdate +%s -d '2013-5-6 7:43:21')sec 'yr;mo;d;hr;min;s'
1 yr + 7 mo + 27 d + 13 hr + 49 min + 26.206759 s
(gunits is units in Linux, gdate is date)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
There's also GNU unit's time calculations combined with GNU date:
$ gunits $(gdate +%s)sec-$(gdate +%s -d -1234day)sec 'yr;mo;d;hr;min;s'
3 yr + 4 mo + 16 d + 12 hr + 37 min + 26.751072 s
$ gunits $(gdate +%s -d '2015-1-2 3:45:00')sec-$(gdate +%s -d '2013-5-6 7:43:21')sec 'yr;mo;d;hr;min;s'
1 yr + 7 mo + 27 d + 13 hr + 49 min + 26.206759 s
(gunits is units in Linux, gdate is date)
There's also GNU unit's time calculations combined with GNU date:
$ gunits $(gdate +%s)sec-$(gdate +%s -d -1234day)sec 'yr;mo;d;hr;min;s'
3 yr + 4 mo + 16 d + 12 hr + 37 min + 26.751072 s
$ gunits $(gdate +%s -d '2015-1-2 3:45:00')sec-$(gdate +%s -d '2013-5-6 7:43:21')sec 'yr;mo;d;hr;min;s'
1 yr + 7 mo + 27 d + 13 hr + 49 min + 26.206759 s
(gunits is units in Linux, gdate is date)
answered Nov 14 '15 at 20:14
Larry
194
194
add a comment |Â
add a comment |Â
up vote
1
down vote
datediff.sh on github:gist
#!/bin/bash
#Simplest calculator two dates difference. By default in days
# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]
first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")
case "$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=$((60*60));;
"--days" | "-d" | "") period=$((60*60*24));;
esac
datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff
add a comment |Â
up vote
1
down vote
datediff.sh on github:gist
#!/bin/bash
#Simplest calculator two dates difference. By default in days
# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]
first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")
case "$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=$((60*60));;
"--days" | "-d" | "") period=$((60*60*24));;
esac
datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff
add a comment |Â
up vote
1
down vote
up vote
1
down vote
datediff.sh on github:gist
#!/bin/bash
#Simplest calculator two dates difference. By default in days
# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]
first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")
case "$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=$((60*60));;
"--days" | "-d" | "") period=$((60*60*24));;
esac
datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff
datediff.sh on github:gist
#!/bin/bash
#Simplest calculator two dates difference. By default in days
# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]
first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")
case "$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=$((60*60));;
"--days" | "-d" | "") period=$((60*60*24));;
esac
datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff
answered Sep 11 '16 at 13:16
user178063
114
114
add a comment |Â
add a comment |Â
up vote
1
down vote
date and bash can do date differences (OS X options shown). Place the latter date first.
echo $((($(date -jf%D "04/03/16" +%s) - $(date -jf%D "03/02/16" +%s)) / 86400))
# 31
add a comment |Â
up vote
1
down vote
date and bash can do date differences (OS X options shown). Place the latter date first.
echo $((($(date -jf%D "04/03/16" +%s) - $(date -jf%D "03/02/16" +%s)) / 86400))
# 31
add a comment |Â
up vote
1
down vote
up vote
1
down vote
date and bash can do date differences (OS X options shown). Place the latter date first.
echo $((($(date -jf%D "04/03/16" +%s) - $(date -jf%D "03/02/16" +%s)) / 86400))
# 31
date and bash can do date differences (OS X options shown). Place the latter date first.
echo $((($(date -jf%D "04/03/16" +%s) - $(date -jf%D "03/02/16" +%s)) / 86400))
# 31
answered Apr 21 '17 at 12:54
Mr. Dave
1336
1336
add a comment |Â
add a comment |Â
up vote
0
down vote
You can use the awk Velour library:
velour -n 'print t_secday(t_utc("2017-4-12") - t_utc("2017-4-5"))'
Or:
velour -n 'print t_secday(t_utc(ARGV[1]) - t_utc(ARGV[2]))' 2017-4-12 2017-4-5
Result:
7
add a comment |Â
up vote
0
down vote
You can use the awk Velour library:
velour -n 'print t_secday(t_utc("2017-4-12") - t_utc("2017-4-5"))'
Or:
velour -n 'print t_secday(t_utc(ARGV[1]) - t_utc(ARGV[2]))' 2017-4-12 2017-4-5
Result:
7
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use the awk Velour library:
velour -n 'print t_secday(t_utc("2017-4-12") - t_utc("2017-4-5"))'
Or:
velour -n 'print t_secday(t_utc(ARGV[1]) - t_utc(ARGV[2]))' 2017-4-12 2017-4-5
Result:
7
You can use the awk Velour library:
velour -n 'print t_secday(t_utc("2017-4-12") - t_utc("2017-4-5"))'
Or:
velour -n 'print t_secday(t_utc(ARGV[1]) - t_utc(ARGV[2]))' 2017-4-12 2017-4-5
Result:
7
edited Jun 8 at 15:06
answered Dec 28 '16 at 2:28
Steven Penny
2,39521635
2,39521635
add a comment |Â
add a comment |Â
up vote
0
down vote
Can someone explain those strange results ?????
$ date -d "10/30/2018 8:00:00 + 3 hours"
Tue Oct 30 07:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 + 0 hours + 2 hours"
Tue Oct 30 12:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 - 01:00"
Tue Oct 30 10:00:00 CET 2018
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
Can someone explain those strange results ?????
$ date -d "10/30/2018 8:00:00 + 3 hours"
Tue Oct 30 07:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 + 0 hours + 2 hours"
Tue Oct 30 12:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 - 01:00"
Tue Oct 30 10:00:00 CET 2018
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Can someone explain those strange results ?????
$ date -d "10/30/2018 8:00:00 + 3 hours"
Tue Oct 30 07:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 + 0 hours + 2 hours"
Tue Oct 30 12:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 - 01:00"
Tue Oct 30 10:00:00 CET 2018
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Can someone explain those strange results ?????
$ date -d "10/30/2018 8:00:00 + 3 hours"
Tue Oct 30 07:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 + 0 hours + 2 hours"
Tue Oct 30 12:00:00 CET 2018
$ date -d "10/30/2018 8:00:00 - 01:00"
Tue Oct 30 10:00:00 CET 2018
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 8 mins ago
liar666
1
1
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
liar666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f24626%2fquickly-calculate-date-differences%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
See also Tool in UNIX to subtract dates for when GNU date is not available.
â Gilles
Nov 15 '11 at 23:26