Date not displaying correctly

Hi Experts,

I tried to stay away from posting stuff here and asking for help.

I want to print date valeu for a given variable and that is not working.

#!/bin/bash
START=`echo $1 | tr -d _`;
FV=`echo $2`
for (( c = 0 ; c < $FV ; c++ ))

do
#    echo -n "`date --date="$START +$c day" +%Y_%m_%d` ";
        date -d "$START + $c day" "+%Y_%m_%d"
done

No when I run this, it displays the following ....telling me that im wrong in asking the bash to display the date format.

Can u experts plz correct me in here please ?

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Thanks you in advance.
PGonzlez

What is your input? What are you passing as arguments to this script?
What is your desired output?

Hi Mirni,

I want it to display the next few days untill it reaches the value of c. To explain more clear,

Lets say if we pass a date of '2011-11-20' as argument then we want to see the next six days(which is value of C passed as argument again) as below

2011-11-20
2011-11-21
2011-11-22
2011-11-23
2011-11-24
2011-11-25
./DJ 2011_12_30 6

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

All of this trouble is to display all date values between two input dates. So, what Im planning doing is compare the values of each output displayed from the above lines and then parse that output using cut or awk and then compare that with the final value that user enters it and display it.

regards,
PGonzalez.

If I could ask: does your innermost loop command actually work if given your formatted args? For example, what happens if you input:

date -d "2012-01-04 + 6 day" "+%Y_%m_%d"

I get invalid date...

Yes, when I run this, I get this error.

date -d "2012-01-04 + 6 day" "+%Y_%m_%d"


usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Getting back to the basic question, how do I display all dates between two dates in the YYYYMMDD format.

For example,

if I give 2 inputs, 2010-12-28 and 2011-01-02 then I should follow -

2010-12-28
2010-12-29
2010-12-30
2010-12-31
2011-01-01
2011-01-02

Solution for post #1.

[highlight=bash]#!/bin/bash

START=`echo $1 | tr _ -`
for (( c = 0 ; c < $2 ; c++ ))
do
date -d "$START +$c days" "+%Y_%m_%d"
done[/highlight]

$ ./test.sh 2012_01_03 6
2012_01_03
2012_01_04
2012_01_05
2012_01_06
2012_01_07
2012_01_08

Im afraid it is still not working for me.

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Solution to post #5.
[highlight=bash]#!/bin/bash

st_date=`date -d "$1" +%s`
nd_date=`date -d "$2" +%s`

num_days=$((($nd_date - $st_date) / 86400))
for (( c=0; c<=$num_days; c++ ))
do
date -d "$1 +$c days" +%Y-%m-%d
done[/highlight]

$ ./test.sh 2012-01-01 2012-01-03
2012-01-01
2012-01-02
2012-01-03

Works for me. I'm using GNU bash, version 3.1.17.

This looks like a BSD legacy data command (probably Mac OSX ?).

The -d (or --date) parameter isn't supported. You will have to roll your own date convert routing using gawk or perl, or install the GNU date command.

Im loosing my mind here...no matter what I do... it is still showing incorrect format. Consitontly showing errors no matter what I do.

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
bash: ( - ) / 86400: syntax error: operand expected (error token is ") / 86400")
bash: ((: c<=: syntax error: operand expected (error token is "=")

Im running GNU bash, version 3.2.48(1). I have tried so many variants of this date format and nothing is working.

I guess I have to drop this script.

Thanks for your help.
PGonzalez

Try perl or search this site for date math tutorials. Plenty of them...

[highlight=perl]#!/usr/bin/perl -w
use strict;
use Time::Local;
use POSIX;

my @st_date = split/-/, $ARGV[0];
my @nd_date = split/-/, $ARGV[1];

my $st_dt = timelocal (0, 0, 0, $st_date[2], $st_date[1] - 1, $st_date[0]);
my $nd_dt = timelocal (0, 0, 0, $nd_date[2], $nd_date[1] - 1, $nd_date[0]);

my $s_dt_fmt = POSIX::strftime ("%Y-%m-%d", localtime($st_dt));
print "$s_dt_fmt\n";

my $num_days = ($nd_dt - $st_dt) / 86400;

for (my $i = 1; $i <= $num_days; $i++) {
my $nxt_dt = $st_dt + ($i * 86400);
my $n_dt_fmt = POSIX::strftime ("%Y-%m-%d", localtime($nxt_dt));
print "$n_dt_fmt\n";
}[/highlight]

$ ./test.pl 2012-01-01 2012-01-03
2012-01-01
2012-01-02
2012-01-03
1 Like

balajesuri,

thank you for showing me that this is also be done using perl.

regards,
Pgonzalez.