fetch dates for last 36 days in format yyyy-mm-dd


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users fetch dates for last 36 days in format yyyy-mm-dd
# 1  
Old 10-01-2009
fetch dates for last 36 days in format yyyy-mm-dd

can anyone please suggest me some ideas for writing a korn shell script which will go back to 36 days from current day and print each day (incremented by 1) in the format yyyy-mm-dd until the current day.

Thanks
Mark
# 2  
Old 10-01-2009
There are a number of articles regarding date arithmetic on the site:

try https://www.unix.com/answers-frequent...rithmetic.html

from the FAQs - you may find your answer (or something close) in there.

What have you tried so far ? Post any code you may have and you're likely to get some good assistance.

Cheers
# 3  
Old 10-01-2009
fetch dates for last 36 days in format yyyy-mm-dd

This is the code i have so far. for now the loop is for 5 days. later i wish to update it to 36 days.

Code:
#!/bin/ksh

set -x
i=1
 while [ $i -le 5 ]; do
_day=`date +%Y%m%d`
newday=`expr $_day - $i`
echo $newday
echo "================================="
i=`expr $i + 1`
echo $i
done

but i have a problem, expr 20091001 - 2 then i see this newday=20090999

Can anyone please suggest me.

Thanks
Mark

Last edited by vgersh99; 10-01-2009 at 01:02 PM.. Reason: code tags, PLEASE!
# 4  
Old 10-01-2009
The previously suggested FAQ link and the forum 'Search' capability should give you a head start.
Have you looked into those?

---------- Post updated at 12:05 PM ---------- Previous update was at 12:05 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 5  
Old 10-01-2009
heres a simple way:-
Code:
#!/bin/ksh
set -x
i=1
 while [ $i -le 5 ]; do
date -d "`date +%Y`-01-01 +$(( `date +"%j"` - $i ))days" +%Y-%m-%d
i=`expr $i + 1`
echo $i
done

i have used a simple trick to convet the current calender date into julian date, substract it by 1, and convert back it to the calendar date Smilie
# 6  
Old 10-02-2009
one another way,

Code:
date --date="36 days ago"  +"%Y-%m-%d"

Use a variable and reduce the 36 upto required num, and proceed.


As, I have no experience in writing korn shell script !!! Write the above suggested by your own.
# 7  
Old 10-02-2009
the geek -

Your method is good as long as the OP has GNU date installed on his machine.

You can also try perl.
Code:
#!/bin/ksh
# will run on Solaris with the date/time format %s problem.

end=$( perl -e ' print time;' )
start=$(( end - ( 86400 * 36) ))  # 36 days in the past
while [[  $end -ge $start  ]]
do
     perl -e 'use POSIX qw(strftime);
              $mt = strftime "%a %b %e %H:%M:%S %Y", localtime($ARGV[0]); 
              print $mt,"\n";' $start
     start=$(( $start + 86400 ))
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date format YYYY/MM/DD to DD/MM/YYYY

I am getting output of YYYY-MM-DD and want to change this to DD/MM/YYYY. When am running the query in 'Todd' to_date(column_name,'DD/MM/YYYY') am getting the required o/p of DD/MM/YYYY, But when am executing the same query(Netezza) in linux server(bash) am getting the output of YYYY-MM-DD file... (3 Replies)
Discussion started by: Roozo
3 Replies

2. Shell Programming and Scripting

Fetch the last two days directory names and rename them

Hi All, I have below directory structure and from these directories, I would like to fetch the last two days list of directories and append a '0' (zero) to those directories. bash-4.1$ ls -lrt total 32 drwxr-xr-x+ 6 root root 9 Sep 5 01:05 tested-597 drwxr-xr-x+ 6 root root 9 Sep 9... (3 Replies)
Discussion started by: ibad_urs
3 Replies

3. Shell Programming and Scripting

Date Format MM/DD/YYYY

I am changing epoch times to dates. I was able to do the following: echo "$varx" | gawk '{print strftime("%c", $0)}' Mon Dec 31 16:26:40 2012 This changes the epoch date (which is what varx is) into localtime. However, my problem is that I only want 12/31/2012 and not the Mon Dec 31... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Fetch data between two dates from a file

Hi All, I m new to this forum & UNix too. currently i have a requirement which can fetch data from a logfile between two dates or timestamp. for example: 1. data from 2012 Jun to 2012 Jul 2. data from 2012 Jun to 2012 Jul 07 3. data from 2012 Jun 16 10:20 to 2012 Jul 03 10:10 Please... (7 Replies)
Discussion started by: KDMishra
7 Replies

5. Shell Programming and Scripting

age in months for yyyy-mm-dd format

Hi, I am trying to write a sh script which will give difference in given time to present time in months. I get date from a script like infoscript.sh | awk '{ print $3}' where infoscript is a custom script and gives date in yyyy-mm-dd format ex: 2010-04-12 Now, need to do... (8 Replies)
Discussion started by: rider29
8 Replies

6. Shell Programming and Scripting

Need date in the format [mm dd yyyy hh AM/PM]

could you please help be on the below code .. Requirement is when i pass the parameter(for below 2) i should get current time -2 hours in the format :wall:.. cur_dt=`$ICEBIN/sqsh -S$DSQUERY -U $BATCHID -P $PASSWD -h -C"select getdate()" | sed '2d'` pr_dt="`$ICEBIN/sqsh -S$DSQUERY -U $BATCHID... (2 Replies)
Discussion started by: karthicss
2 Replies

7. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

8. Shell Programming and Scripting

Sed: zero-padding dates (or: convert d/m/yyyy to dd/mm/yyyy)

Hi all I have some pipe-separated data in the form: 5/12/2008 00:00:00|31/1/2009 00:00:00|SOMESTUFF|OTHERSTUFF 12/31/2008 00:00:00|15/1/2009 00:00:00|MORESTUFF|REMAININGSTUFF 1/1/1023 00:00:00|16/5/2047 00:00:00|THEREST|YETMORE I need to zero-pad the single-digit days and months, using... (3 Replies)
Discussion started by: jgrogan
3 Replies

9. Shell Programming and Scripting

Date difference between 2 dates in 'yyyy-mm-dd hh:mm:ss' format

Hi all, I know this may have already been asked but hey ho... i have two dates in the 'yyyy-mm-dd hh:mm:ss' format. '2009-01-03 01:00:00' '2009-04-05 00:00:00' How can i, in shell script determine their differences? Please note, the time may not be available, so please suggest both... (4 Replies)
Discussion started by: muay_tb
4 Replies

10. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies
Login or Register to Ask a Question