Quickly calculate date differences

I usually intend to make some fast date estimations, such as:

  • What is the distinction in between these 2 days?
  • What is the date n weeks hereafter various other date?

I generally open a schedule and also count the days, yet I assume there need to be a program/script that I can make use of to do these sort of estimations. Any kind of pointers?

126
2022-06-05 17:01:35
Source Share
Answers: 6

If a visual device is alright for you, I exuberantly advise qalculate (a calculator with a focus on device conversions, it features a GTK and also KDE user interface, IIRC). There you can claim as an example

days(1900-05-21, 1900-01-01)

to get the variety of days (140, given that 1900 was not a jump year) in between the days, yet certainly you can additionally do the very same for times:

17:12:45 − 08:45:12

returns 8.4591667 hrs or, if you set the result to time format, 8:27:33.

8
2022-07-22 17:41:04
Source

For a set of mobile devices attempt my really own dateutils. Your 2 instances would certainly come down to one - linings:

ddiff 2011-11-15 2012-04-11
=>
  148

or in weeks and also days:

ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
  21 1

and also

dadd 2011-11-15 21w
=>
  2012-04-10

Important note pertaining to Ubuntu installment:

These similar dateutils are readily available as a Ubuntu package, and also therefore installable via sudo apt install dateutils.

Nonetheless, regulates demand to be come before by a dateutils. prefix, as in dateutils.ddiff 2019-03-28 2019-05-16

They are additionally readily available in Fedora and also in Fedora EPEL for RHEL or CentOS with sudo dnf install dateutils. In Fedora, these plans do not call for a prefix yet make use of the lengthy names bdsh e.g, datediff and also dateadd as opposed to ddiff and also dadd.

For Arch Linux, install the plan with sudo pacman -Sy dateutils. The binaries are called datediff and also dateadd, similar to in Fedora.

61
2022-06-06 23:48:01
Source

A python instance for computing the variety of days I've strolled the earth:

$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
38
2022-06-05 17:34:28
Source

I generally favor having the time/date in unix utime layout (variety of secs given that the date, when the seventies started, UTC). In this way it constantly comes down to simple reduction or enhancement of secs.

The trouble the generally comes to be changing a date/time right into this layout.

In a shell environment/script you can get it with date '+%s' At the time of creating, the existing time is 1321358027.

To compare to 2011 - 11 - 04 (my birthday celebration), date '+%s' -d 2011-11-04, generating 1320361200. Subtract: expr 1321358027 - 1320361200 offers 996827 secs, which is expr 996827 / 86400 = 11 days earlier.

Transforming from utime (1320361200 layout) right into a date is really straightforward to do in as an example C, PHP or perl, making use of typical collections. With GNU date, the -d argument can be prepended with @ to show "Seconds given that the Epoch" layout.

16
2022-06-05 17:32:52
Source

The "n weeks after a date" is very 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 do not recognize of a straightforward means to compute the distinction in between 2 days, yet you can cover a little reasoning 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'  # Note: answer should be 92 days but in my timezone, DST starts between the dates.
91 days

Swap d1 and also d2 if you desire the date estimation the various other means, or get a little bit fancier to make it not matter. In addition, in instance there is a non - DST to DST change in the period, among the days will certainly be just 23 hrs long ; you can make up by including 1/2 day to the amount.

echo $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days
152
2022-06-05 17:32:13
Source

I regularly make use of SQL for date estimations. As an 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 simply really feel in state of mind for JavaScript. As an 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 is erector. Begins with 0.)

3
2022-06-05 17:31:56
Source