user137369
user137369

Reputation: 5676

Amount of X day increments from date

I need to find, in OSX (I mention this since date has different options from most Linux distros), how many increments of X days have passed since a certain date.

Let’s say the date is July 31, 2013, and I need to find how many 10 day increments have passed. Running the command today (August 29, 2013) — the result would be 2, and tomorrow (August 30, 2013), the result would be 3.

Upvotes: 0

Views: 68

Answers (2)

user137369
user137369

Reputation: 5676

Based on @konsolebox’s answer, here’s how to do it on OSX (see his answer for a working solution on Linux).

echo "$(( ($(date '+%s') - $(date -j -f '%Y-%m-%d' '2013-07-31' '+%s')) / (10 * 24 * 3600) ))"

or (broken down)

now=$(date '+%s')
other=$(date -j -f '%Y-%m-%d' '2013-07-31' '+%s')
echo "$(( (now - other) / (10 * 24 * 3600) ))"

Upvotes: 1

konsolebox
konsolebox

Reputation: 75478

Get the timestamps then calculate it:

echo "$(( ($(date -d 'now' '+%s') - $(date -d 'July 31, 2013' '+%s')) / (10 * 24 * 3600) ))"

More readable form:

now=$(date -d 'now' '+%s')
other=$(date -d 'July 31, 2013' '+%s')
echo "$(( (now - other) / (10 * 24 * 3600) ))"

Timestamps are in seconds since epoch (1970-01-01 UTC).

Upvotes: 1

Related Questions