Date not displaying correctly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date not displaying correctly
# 8  
Old 01-05-2012
Solution to post #5.
bash code:
  1. #!/bin/bash
  2.  
  3. st_date=`date -d "$1" +%s`
  4. nd_date=`date -d "$2" +%s`
  5.  
  6. num_days=$((($nd_date - $st_date) / 86400))
  7. for (( c=0; c<=$num_days; c++ ))
  8. do
  9.     date -d "$1 +$c days" +%Y-%m-%d
  10. done

Code:
$ ./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.
# 9  
Old 01-05-2012
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.
# 10  
Old 01-05-2012
Im loosing my mind here...no matter what I do... it is still showing incorrect format. Consitontly showing errors no matter what I do.
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]
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

Last edited by Franklin52; 01-05-2012 at 04:19 AM.. Reason: Please use code tags for code and data samples, thank you
# 11  
Old 01-05-2012
Try perl or search this site for date math tutorials. Plenty of them...
# 12  
Old 01-05-2012
perl code:
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Time::Local;
  4. use POSIX;
  5.  
  6. my @st_date = split/-/, $ARGV&#91;0];
  7. my @nd_date = split/-/, $ARGV&#91;1];
  8.  
  9. my $st_dt   = timelocal (0, 0, 0, $st_date&#91;2], $st_date[1] - 1, $st_date[0]);
  10. my $nd_dt   = timelocal (0, 0, 0, $nd_date&#91;2], $nd_date[1] - 1, $nd_date[0]);
  11.  
  12. my $s_dt_fmt = POSIX::strftime ("%Y-%m-%d", localtime($st_dt));
  13. print "$s_dt_fmt\n";
  14.  
  15. my $num_days = ($nd_dt - $st_dt) / 86400;
  16.  
  17. for (my $i = 1; $i <= $num_days; $i++) {
  18.     my $nxt_dt = $st_dt + ($i * 86400);
  19.     my $n_dt_fmt = POSIX::strftime ("%Y-%m-%d", localtime($nxt_dt));
  20.     print "$n_dt_fmt\n";
  21. }

Code:
$ ./test.pl 2012-01-01 2012-01-03
2012-01-01
2012-01-02
2012-01-03

This User Gave Thanks to balajesuri For This Post:
# 13  
Old 01-05-2012
balajesuri,

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

regards,
Pgonzalez.
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