How to Find number of days in a month in mmddyyyy format?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Find number of days in a month in mmddyyyy format?
# 1  
Old 11-24-2011
How to Find number of days in a month in mmddyyyy format?

Hi Guru's,

I am working on a shell script from past a month and unable to get rid of automating while working with dates,here's what i have.

inital_date=11012011
final_date=11302011

expected_output= has to be in below format PFB
11012011
11022011
11032011
*
*
*
11102011
*
*
11152011
*
*
11252011
*
*
11302011

Please advice how to achieve this?
# 2  
Old 11-25-2011
Print the date by incrementing UNIX timestamp.

Code:
#!/bin/bash

function get_timestamp()
{
    m=${1:0:2}
    d=${1:2:2}
    y=${1:4:4}
    date -d "$m/$d/$y" +%s
}

inital_date="11012011"
final_date="11302011"

inital_ts=`get_timestamp $inital_date`
final_ts=`get_timestamp $final_date`

day_inc=$((60 * 60 * 24))
i=$((inital_ts))

while [ $i -le $final_ts ]; do
    date -d "@$i" +%m%d%Y
    i=$((i + day_inc))
done

# 3  
Old 11-25-2011
Quote:
Originally Posted by Gaurav198
...
I am working on a shell script from past a month and unable to get rid of automating while working with dates,here's what i have.

inital_date=11012011
final_date=11302011

expected_output= has to be in below format PFB
11012011
11022011
11032011
*
*
*
11102011
*
*
11152011
*
*
11252011
*
*
11302011

Please advice how to achieve this?
Code:
$
$
$ perl -le 'BEGIN {use POSIX; use Time::Local; $fixed=0}
            $begin = "11012011";
            $end   = "11302011";
            @x = unpack "A2 A2 A4", $begin;
            $start = timelocal 0,0,0, $x[1], $x[0]-1, $x[2]-1900;
            @y = unpack "A2 A2 A4", $end;
            $end = timelocal 0,0,0, $y[1], $y[0]-1, $y[2]-1900;
            do {
              print strftime "%m%d%Y", localtime $start;
              $olddst = (localtime $start)[8];
              $start += 24*60*60;
              $newdst = (localtime $start)[8];
              if ($newdst == 0 and $olddst != $newdst and !$fixed) {
                $start += 1*60*60; $fixed = 1
              } elsif ($newdst == 1 and $olddst != $newdst and !$fixed) {
                $start -= 1*60*60; $fixed = 1
              }
            } while $start <= $end'
11012011
11022011
11032011
11042011
11052011
11062011
11072011
11082011
11092011
11102011
11112011
11122011
11132011
11142011
11152011
11162011
11172011
11182011
11192011
11202011
11212011
11222011
11232011
11242011
11252011
11262011
11272011
11282011
11292011
11302011
$
$
$
$ # Testing for leap year...
$
$ perl -le 'BEGIN {use POSIX; use Time::Local; $fixed=0}
            $begin = "02012008";
            $end   = "03052008";
            @x = unpack "A2 A2 A4", $begin;
            $start = timelocal 0,0,0, $x[1], $x[0]-1, $x[2]-1900;
            @y = unpack "A2 A2 A4", $end;
            $end = timelocal 0,0,0, $y[1], $y[0]-1, $y[2]-1900;
            do {
              print strftime "%m%d%Y", localtime $start;
              $olddst = (localtime $start)[8];
              $start += 24*60*60;
              $newdst = (localtime $start)[8];
              if ($newdst == 0 and $olddst != $newdst and !$fixed) {
                $start += 1*60*60; $fixed = 1
              } elsif ($newdst == 1 and $olddst != $newdst and !$fixed) {
                $start -= 1*60*60; $fixed = 1
              }
            } while $start <= $end'
02012008
02022008
02032008
02042008
02052008
02062008
02072008
02082008
02092008
02102008
02112008
02122008
02132008
02142008
02152008
02162008
02172008
02182008
02192008
02202008
02212008
02222008
02232008
02242008
02252008
02262008
02272008
02282008
02292008
03012008
03022008
03032008
03042008
03052008
$
$
$
$ # Testing for month and year end...
$
$ perl -le 'BEGIN {use POSIX; use Time::Local; $fixed=0}
            $begin = "12012011";
            $end   = "01052012";
            @x = unpack "A2 A2 A4", $begin;
            $start = timelocal 0,0,0, $x[1], $x[0]-1, $x[2]-1900;
            @y = unpack "A2 A2 A4", $end;
            $end = timelocal 0,0,0, $y[1], $y[0]-1, $y[2]-1900;
            do {
              print strftime "%m%d%Y", localtime $start;
              $olddst = (localtime $start)[8];
              $start += 24*60*60;
              $newdst = (localtime $start)[8];
              if ($newdst == 0 and $olddst != $newdst and !$fixed) {
                $start += 1*60*60; $fixed = 1
              } elsif ($newdst == 1 and $olddst != $newdst and !$fixed) {
                $start -= 1*60*60; $fixed = 1
              }
            } while $start <= $end'
12012011
12022011
12032011
12042011
12052011
12062011
12072011
12082011
12092011
12102011
12112011
12122011
12132011
12142011
12152011
12162011
12172011
12182011
12192011
12202011
12212011
12222011
12232011
12242011
12252011
12262011
12272011
12282011
12292011
12302011
12312011
01012012
01022012
01032012
01042012
01052012
$
$

tyler_durden
# 4  
Old 11-25-2011
Thanks Tyler and MacMonmster...for the response.
I am on sunos and dont have perl.
Bash script is fine, but the -d optionwith date wont work.Macmonster is there any other way to find solution?
# 5  
Old 11-25-2011
# 6  
Old 11-25-2011
Hi Felipe....did check with this long back...support diffrent format..
I had this one , but works for on year difrrence..i want to work in month diffrence format...

Code:
#! /usr/bin/ksh
#              ja fe ma ap ma ju ju ag se oc no de
set -A lasts 0 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 dmonth dday
year=2011
while ((year<2015)) ; do
        leap=0
        if ((!(year%100))); then
                ((!(year%400))) && leap=1
        else
                ((!(year%4))) && leap=1
        fi
        lasts[2]=28
        ((leap)) && lasts[2]=29
        month=1
        while ((month<13)); do
                day=1
                while((day<(lasts[month])+1)) ; do
                        dday=$day 
                        dmonth=$month
                        echo ${dmonth}${dday}${year}
                       ((day=day+1))
                done
                 ((month=month+1))
        done
        ((year=year+1))
done
exit 0

the above one works fine...but for years i have to narrow down to months

Last edited by fpmurphy; 11-26-2011 at 08:59 AM.. Reason: code tags please!
# 7  
Old 11-25-2011
Quote:
Originally Posted by Gaurav198
...I am on sunos and dont have perl.
...
Just in case you didn't know, Perl is available for every major OS out there.
Besides, if you have a C compiler in your SunOS, you can build perl.

tyler_durden
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

find file older than one month not by x days olds

Hi, I would like to ask about some question on the -mtime option in the find command. I want to move a log files older than one month and planning to used the find -mtime +30 but i have some clarrification does -mtime +30 or -30 refer to x days beyond or between so how about the month. suppose... (2 Replies)
Discussion started by: jao_madn
2 Replies

3. 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

4. Programming

Number of days in month from certain parameters, c programming request.

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... (2 Replies)
Discussion started by: modn3
2 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

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

9. Shell Programming and Scripting

Day of month and year to mmddyyyy

I have a date that looks like this: 2008/100:18:40:47.040 I need it to look like this: 2008 04 09 18 40 47 040 I have looked at datecalc and it doesn't seem like it takes the day of year for this year (or whatever year is input) and converts it into month and day. It also has to account... (2 Replies)
Discussion started by: ajgwin
2 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