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.
3 Answers
you can test :
DATE="20120803" date -d @$(( `date -d "$DATE" +%s` - (3*24*60*60) )) 2 Comments
canredondocity
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]
scai
@canredondocity you shoudn't use the linux tag if your question is not about Linux.
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
canredondocity
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 :(
scai
Works here. Please provide meaningful error descriptions including your input.
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
canredondocity
use: no encontrado my: no encontrado
scai
@canredondocity This is perl, not a shell script. And you need the DateTime.pm module.