Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to calculate eg "date - 5 days"

479 views
Skip to first unread message

Roland

unread,
Mar 29, 2007, 4:05:28 AM3/29/07
to
Hi all

I need to calc eg "today-x_days" or "today+x_days)".

I may not use perl, phyton etc., I have only ksh or bash available.

OS: Sparc Solaris 8, 9 and 10.

I assume that this is not trivial, becase of eg. leap years.

Example, for calculation of "yesterday":
yesterday=today - 1_day

If today is 1st of March, yesterday may be 28th or 29th of Feb.

This should be treated correctly at leap years.

I really would appreciate any help!

Regards, Roland

Bill Pursell

unread,
Mar 29, 2007, 2:10:50 PM3/29/07
to
On 29 Mar, 09:05, "Roland" <Roland.R...@gmail.com> wrote:

> I need to calc eg "today-x_days" or "today+x_days)".


[misc]$ date
Thu Mar 29 19:17:37 BST 2007
[misc]$ date --date="28 days ago"
Thu Mar 1 19:17:47 GMT 2007
[misc]$ date --date="30 days ago"
Tue Feb 27 19:18:00 GMT 2007

Note that this isn't quite correct, since it doesn't take into
account the hour lost over daylight savings time...

Chris F.A. Johnson

unread,
Mar 29, 2007, 4:52:22 PM3/29/07
to

That requires GNU date. On some systems it may be installed as
gdate.

If you don't have it, and cannot install it, there are a number of
shell functions which manipulate dates in Chapter 8 of my book.
That chapter is on-line at:
<http://cfaj.freeshell.org/shell/ssr/08-The-Dating-Game.shtml>.


--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========= Do not reply to the From: address; use Reply-To: ========
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

0 new messages