1848680 Members
7490 Online
104035 Solutions
New Discussion

date minus some number

 
Sid Shapiro
Occasional Advisor

date minus some number

how to substract some number (let say 2 ) from currect date and get the date of 2 days back.

let say today is 05/02/01
substract = 2

should show me 30 (ie 30th,april)



Deepak Seth
7 REPLIES 7
Carsten Krege
Honored Contributor

Re: date minus some number

You could do it with adb if you know how to express the current date in seconds since 1970 (should be possible to get with a short C program that uses gettimeofday() ).

# echo "0d919818576=Y" | adb
1999 Feb 24 02:09:36

Here 919818576 is the number of seconds since 1970 on Feb 24 1999.

If you want to get the day before, let adb subtract 86400 seconds (=1 day):

# echo "0d919818576-0d86400=Y" |adb
1999 Feb 23 02:09:36

Putting this into a small script that, shouldn't be hard, e.g. to get the number of seconds for in four days is simply:

# echo "4 * 86400" |bc
345600

Probably there is also an easier way to do this.

Carsten
-------------------------------------------------------------------------------------------------
In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. -- HhGttG
Tom Danzig
Honored Contributor

Re: date minus some number

I wrote a script a while back to do this (attached). It will give you the date any given number of days ago (within reason).
Juan González
Trusted Contributor

Re: date minus some number

Hi Tom,
to complete what Carsten do:

Put in a file (minus.c)
#include
#include

int main(argc, argv)
int argc;
char *argv[];
{
struct timeval time;
if (argc!=2) {
printf("Error\n");
exit(0);
}
gettimeofday(&time,NULL);
printf("0d%d=Y",time.tv_sec - atoi(argv[1])*86400);
}

Compile it:
#cc minus.c -o minus

And then (if you want to know the date 2 days ago)
#echo `./minus 2`|adb

Best regards
Mladen Despic
Honored Contributor

Re: date minus some number

Tom,

The attached script does the job by using 'cal' to quickly determine the number of days in the previous month.
You can probably make it shorter, especially if you do not require a particular date format in the output.

Mladen
Sid Shapiro
Occasional Advisor

Re: date minus some number

thanx Juan Gonz?lez . your script script seems to be small and easy to implement .


Deepak Seth
Ralph Grothe
Honored Contributor

Re: date minus some number

Here's a Perl oneliner:

perl -e 'print scalar localtime(time-2*86400),"\n"'

If you need fancier formatting check the list entries returned from localtime in an array/list l-value context:

perldoc -f localtime

or

perldoc perlfunc
Madness, thy name is system administration
Davide Depaoli
Occasional Advisor

Re: date minus some number

Try the attached script. This script come back 1 day (yesterday), but you may customize it for your porpouse.

Hope this helps
regards Davide