Days of the month


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Days of the month
# 1  
Old 10-18-2004
Question Days of the month

Hello,
Please can someone help me in getitng last 7 days from the current date?

for eg:
input /default : today (10/18/2004)
required output:
10/17/2004
10/16/2004
10/15/2004
10/14/2004
10/13/2004
10/12/2004
10/11/2004

Thanks in advance.
Anamika
# 2  
Old 10-18-2004
Perderabo wrote a script called datecalc that can do virtually any kind of date arithmetic. datecalc is arguably the best solution for date manipulation. But there are several other solutions to consider, see the Frequetly Answered Questions section. Here is a link to the program.
# 3  
Old 10-19-2004
Code:
#!/usr/local/bin/perl

($day, $month, $year) = (localtime)[3,4,5];
$year += 1900;
$month += 1;
for($i=0;$i<7;$i++){
	$day--;
	print "$month/$day/$year \n";
}

# 4  
Old 10-19-2004
Bug Thanks

Google,
Thanks for the link. I am going thru the script written by Perderabo.

Photon,
Thanks for the code snippet. It looks like I need perl compiler/interpreter to be installed for running the code. Unfortunately, we don't use Perl.

Thanks again!!
Smilie
# 5  
Old 10-20-2004
Also, the Perl script is going to fall over if you run it on or before the 7th of the month. You'll end up with dates such as 10/-5/04, rather than 9/24/04 - i.e. it isn't going to drop back to the previous month.

Cheers
ZB
# 6  
Old 10-20-2004
Tks zazzybob, love those bugs.Smilie

How is this one:

Code:
#!/usr/local/bin/perl

use Time::Local;

($dd, $mm, $yyyy) = (localtime)[3,4,5];
$year += 1900;
$month += 1;

$ss = timelocal(0, 0, 0, $dd, $mm, $yyyy);

$tt = $ss;     
for($i=1;$i<=7;$i++){
	$interval = -$i  * 60 * 60 * 24;
	$then = $tt + $interval;
	($dd, $mm, $yyyy) = (localtime($then))[3,4,5];
	$yyyy += 1900;
	$mm += 1;
	print "$mm/$dd/$yyyy \n";
}


Last edited by photon; 10-20-2004 at 11:56 AM..
# 7  
Old 10-20-2004
I changed the for loop to iterate 365 days to test it, and it seems to work nicely. Smilie

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of days in current month

I have a homework assignment: ---------------------------------------- "Display" the number of days in the current month. For example: September 1996 has 30 days ---------------------------------------- I am trying to just display the head of cal to start the sentence. eg. cal | head ... (1 Reply)
Discussion started by: eaafuddy
1 Replies

2. Shell Programming and Scripting

Script to set columns to days in month

I am trying to figure out how to assign columns of a text file to the day of the month. The end result will be a way to determine when each day (column) is populated with data. The data file are in the format of: M1Y2012 x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x... (8 Replies)
Discussion started by: ncwxpanther
8 Replies

3. UNIX for Dummies Questions & Answers

Finding days in previous month

#!/bin/ksh day=1 month=1 year=2012 if then then prevmonth=31 elif then prevmonth=30 elif then then prevmonth=29 elif then prevmonth=29 else prevmonth=28 fi (4 Replies)
Discussion started by: vagar11
4 Replies

4. Shell Programming and Scripting

display number of days in current month

hi all searched google and here, cant find and am begining to suspect there is no options for this. shell = born with either the date or cal command I need to display the number of days in current month. can anyone point me in the right direction? (10 Replies)
Discussion started by: rontopia
10 Replies

5. Shell Programming and Scripting

Number of days in month from certain parameters

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (5 Replies)
Discussion started by: modn3
5 Replies

6. Shell Programming and Scripting

Number of days in the previous month

Hi all. I am scripting in a POSIX shell on HPUX. I am running a script that needs to determine the number of days in a month. I found this on the forum and it works great: X=`cal $(date +%m) $(date +%Y) | grep -v '' | wc -w` The issue is that I am running the script on the 7th day of... (11 Replies)
Discussion started by: lyoncc
11 Replies

7. Shell Programming and Scripting

Assigning number of days in the month to a variable

I am writing a script that requires the number of days in any given month. In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc... (3 Replies)
Discussion started by: skaptakalian
3 Replies

8. Shell Programming and Scripting

how to increment days according to year & month

Hiii i have a file with data as shown below: a.dat: RAO 1900 2 7 0 0 0.00 10.8000 76.8000 10.0 0 0.00 0 6.00 0.00 0.00 0 0.00 6.00 0 NULL LEE 1901 2 15 0 0 0.00 26.0000 100.0000 0.0 0 0.00 0 0.00 0.00 0.00 0 6.00 6.00 0 NULL RAO 1901 4... (3 Replies)
Discussion started by: reva
3 Replies

9. Shell Programming and Scripting

calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result (8 Replies)
Discussion started by: dunryc
8 Replies

10. Shell Programming and Scripting

Calc number of days in a month

Looking for some help on capturing the number of days in a month to set as a loop counter. Any ideas, please let me know. (3 Replies)
Discussion started by: flounder
3 Replies
Login or Register to Ask a Question