user7422128
user7422128

Reputation: 932

Date conversion and subtraction in shell script

I have my date in the below format:datetime=date +%Y%m%d%H%M%SEchoing it gives me something like 20180123143852.

Now i want the difference of two days from the date and i'm trying like this

dby_date=`date -d "$datetime - $date_diff " +%Y-%m-%d`

Error Output:

   date: invalid date â20180123143852 - 2 â

Any suggestion how to get it in the format i want

Upvotes: 1

Views: 368

Answers (2)

kvantour
kvantour

Reputation: 26481

If you want to have the difference between two dates, it might be best to convert them in a format that date knows.

man date: The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.

So I would suggest to format them accordingly (date "+%F %T" should do the trick)

Furthermore, a difference in dates cannot be converted in a date format, so you need to do some manual work.

Lets say you have two dates in this format $date1 and $date2, the difference can now be obtained as :

date1="2018-01-23 16:54:16"
date2="2004-02-29 16:21:42"
# Compute difference in seconds
diff=$(( $(date --utc -d "$date1" "+%s") - $(date --utc -d "$date2" "+%s") ))
# compute the separate parts (integer arithmetic)
days=$(( diff/86400 ))
hours=$(( (diff - days*86400)/3600 ))
minutes=$(( (diff - days*86400 - hours*3600)/60 ))
seconds=$(( diff - days*86400 - hours*3600 - minutes*60 ))
# print result
printf "%0.3d days %0.2d hours %0.2d min %0.2d sec\n" $days $hours $minutes $seconds

5077 days 00 hours 32 min 34 sec

Or you can just do

date1="2018-01-23 16:54:16"
date2="2004-02-29 16:21:42"
# Compute difference in seconds
diff=$(( $(date --utc -d "$date1" "+%s") - $(date --utc -d "$date2" "+%s") ))
echo $(( $diff/86400 ))":"$(date -d @$diff "+%T")
5077:00:32:34

Bare in mind the following here :

  • The dates are converted to unix time in UTC. Not strictly needed if both dates are in the same time-zone.
  • The computation does not account for leap-seconds. When a leap second occurs, unix time is reset by one and is therefor unaware of the leap second.

Upvotes: 0

shellter
shellter

Reputation: 37268

A quick solution :

datetime=20180123
date -d "$datetime - $date_diff days" +%Y-%m-%d
# -----------------------------^^^^^^

returns

2018-01-21

Note that I had to eliminate the time portion of your time-stamp and used -2 days for the subtraction.

If you need the time portion, I would recommend saving that to a separate variable then appending that value back onto your output. But as you've changed your output format, I guess the time isn't that important?

IHTH

Upvotes: 1

Related Questions