0

I need help. I made a shell script that you pass a date earlier than 3 days in YYYYMMDD format and tell me if it is correct or not. My question is. Can i subtract the date command 3 days? thanks.

1
  • do you want to use the date command to substract 3 days from the given days? Commented Aug 3, 2012 at 10:10

3 Answers 3

2

you can test :

 DATE="20120803" date -d @$(( `date -d "$DATE" +%s` - (3*24*60*60) )) 
Sign up to request clarification or add additional context in comments.

2 Comments

sorry but ... date: opción no permitida -- d sintaxis: date [-u] ddmmHHMM[[cc]aa][.SS] date [-u] [+format] date -a [-]sss[.fff] date: opción no permitida -- d sintaxis: date [-u] ddmmHHMM[[cc]aa][.SS] date [-u] [+format] date -a [-]sss[.fff]
@canredondocity you shoudn't use the linux tag if your question is not about Linux.
1

for the fancy solution:

INPUT="20120803" INPUT_SECONDS=$(date -d "$INPUT" +%s) THREEDAYSAGO_SECONDS=$(date -d "3 days ago" "+%s") if [ $INPUT_SECONDS -lt $THREEDAYSAGO_SECONDS ]; then echo "too early :(" fi 

2 Comments

date: opción no permitida -- d sintaxis: date [-u] ddmmHHMM[[cc]aa][.SS] date [-u] [+format] date -a [-]sss[.fff] date: opción no permitida -- d sintaxis: date [-u] ddmmHHMM[[cc]aa][.SS] date [-u] [+format] date -a [-]sss[.fff] too early :(
Works here. Please provide meaningful error descriptions including your input.
0

Although you can use the date command to do this (see Guillame's excellent answer) it may be worth considering a scripting language such as Perl to do more complex stuff more efficiently.

e.g. see this SO answer, using Perl and the DateTime.pm module:

use DateTime; my $date = DateTime->now; $date->subtract(days => 3); print $date->ymd; 

2 Comments

use: no encontrado my: no encontrado
@canredondocity This is perl, not a shell script. And you need the DateTime.pm module.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.