Date not displaying correctly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date not displaying correctly
# 1  
Old 01-04-2012
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.

Code:
#!/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 ?
Code:
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

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-05-2012 at 04:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-04-2012
What is your input? What are you passing as arguments to this script?
What is your desired output?
# 3  
Old 01-05-2012
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
Code:
2011-11-20
2011-11-21
2011-11-22
2011-11-23
2011-11-24
2011-11-25

Code:
./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.

Last edited by Franklin52; 01-05-2012 at 04:17 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 01-05-2012
If I could ask: does your innermost loop command actually work if given your formatted args? For example, what happens if you input:

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

I get invalid date...
# 5  
Old 01-05-2012
Yes, when I run this, I get this error.
Code:
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 -
Code:
2010-12-28
2010-12-29
2010-12-30
2010-12-31
2011-01-01
2011-01-02


Last edited by Franklin52; 01-05-2012 at 04:18 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 01-05-2012
Solution for post #1.

bash code:
  1. #!/bin/bash
  2.  
  3. START=`echo $1 | tr _ -`
  4. for (( c = 0 ; c < $2 ; c++ ))
  5. do
  6.     date -d "$START +$c days" "+%Y_%m_%d"
  7. done

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

# 7  
Old 01-05-2012
Im afraid it is still not working for me.
Code:
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]


Last edited by Franklin52; 01-05-2012 at 04:18 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Displaying every date since 2007

Hi, I need to write a script that displays every date from 01/01/2007 to 03/31/2010 in the format mm/dd/yyyy. There doesn't seem to be a "datecalc" command on my system. Any inputs? Thanks, (4 Replies)
Discussion started by: 17amrut29
4 Replies

2. UNIX for Dummies Questions & Answers

Date command to obtain the last month is not working correctly..

Hello, I could not find the exactly same post here.. so I will explain what I did to get the last month using date command. I used date +%Y-%m -d "-1 months" to get the last month. However, the returned value of above command on 2009/10/31 was 2009 10 and not 2009 09.. and the... (9 Replies)
Discussion started by: tigersk
9 Replies

3. Shell Programming and Scripting

script not displaying output correctly

Hi, I am having an issue with my script, ofcourse... I am trying to run commands against a remote server, I am pulling the hostnames or IPs from a file list, then looping thru and running the date cmd. I will be running different cmds just trying to get it working first. The ouput isn't... (2 Replies)
Discussion started by: dfezz1
2 Replies

4. UNIX for Dummies Questions & Answers

How do I grep a Date correctly

I am still a novice at this stuff, but I have searched everywhere and I cant seem to get this working. I am using a database program that I need to pull information from. The command I am using is the following. search /project | grep "date -v -1m "+%Y-%m"" This returns no results, however... (1 Reply)
Discussion started by: trezero
1 Replies

5. Shell Programming and Scripting

problem with displaying date and adding time

Hi, I have a log file with contents like 81.49.74.131 - - 81.49.74.131 - - 116.112.52.31 - - 116.112.52.31 - - I need an output like this 81.49.74.131 14/Sep/2008 Time duration: 00:06:00 116.112.52.31 15/Sep/2008 Time duration: 00:00:01 Please anyone suggest a script for this.... (1 Reply)
Discussion started by: FuncMx
1 Replies

6. Shell Programming and Scripting

Displaying Date

Hi, Am new to unix..i want some help.. when am using ls command like ls-ltr it displaying output like this: rw-r--r-- 1 infauser dba 36 Jun 16 12:36 s1_midify -rw-r--r-- 1 infauser dba 66 Jun 16 12:42 sample_one -rw-r--r-- 1 infauser dba 77 Jun 16 13:05... (3 Replies)
Discussion started by: sujana
3 Replies

7. Shell Programming and Scripting

Collecting & Displaying of Last User ID and Date of Last Login

Hi, I needed to write a script to "Collect and Display the Last User ID and Date of Last Login". The requirement is: When a workstation stops reporting it could be for many reasons. If we know the last person who logged in before the workstation came down, then we can contact the last... (1 Reply)
Discussion started by: amittal
1 Replies

8. UNIX for Dummies Questions & Answers

Collecting & Displaying of Last User ID and Date of Last Login

Hi, I needed to write a script to "Collect and Display the Last User ID and Date of Last Login". The requirement is: When a workstation stops reporting it could be for many reasons. If we know the last person who logged in before the workstation came down, then we can contact the last... (1 Reply)
Discussion started by: amittal
1 Replies

9. UNIX for Dummies Questions & Answers

Displaying file names before a particular creation date

Hi!! I wanna display file names which are created before/after a particular date. I wud be glad if anybody can help me out in that. Thanx Dhruv (1 Reply)
Discussion started by: dhruv_saksena
1 Replies

10. Shell Programming and Scripting

displaying date

Hi All, When I type date..I get the date, time ..etc displayed ...but can someone help me to display yesterdays date... some script to display back dates. Thanks in advance Minaz (7 Replies)
Discussion started by: minazk
7 Replies
Login or Register to Ask a Question