Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get various dates

Status
Not open for further replies.

SkyHigh

Technical User
May 30, 2002
309
0
0
CA
Hi Folks

I need some routines to get the following dates from any given day of the month :

Last day of the previous month
Last day of the month before previous month

I am using the following routines which breaks for leap year

($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime();
$month += 1;
$year += 1900;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon = $mon + 1; $year = $year + 1900;

my $last_day;
my $previous_month = $month - 1;

if ($previous_month == 0){$previous_month = 12; $year = $year - 1; $last_day = 31;}
elsif ($previous_month == 2) {$last_day = 28;}
elsif ($previous_month == 2 and $day == 29) {$last_day = 29;}
elsif ($previous_month == 4) {$last_day = 30;}
elsif ($previous_month == 6) {$last_day = 30;}
elsif ($previous_month == 9) {$last_day = 30;}
elsif ($previous_month == 11) {$last_day = 30;}
else {$last_day = 31;}


Thanks for you help
Brenda
 
Use an array with the number of days in each month and use $previous_month to index into the array and retrieve last day in that month.

This example takes number of months ago from STDIN and loops 'til you enter 'quit'. (For testing purposes.) Sub isleap determines whether $year is a leap year.
Code:
#!perl
use strict;
use warnings;

[b]my @daysmths = (undef, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);[/b]

# Get number of months ago
print "\nEnter number of months ago or 'quit': ";
my $months_ago = <STDIN>;

until ($months_ago =~ /^q(uit)?$/i) {
    unless (defined($months_ago) && $months_ago =~ /^\d+$/) {
        warn qq(Non-negative integer required!\n);
        next;
    }
    chomp $months_ago;

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $mon = $mon + 1; $year = $year + 1900;
    my $previous_month = $mon - $months_ago;

[b]    while ($previous_month < 1) {
        $previous_month += 12;
        $year -= 1;
    }
    my $last_day = $daysmths[$previous_month];
    $last_day += isleap($year) if $previous_month == 2;[/b]

    print "previous_month: $previous_month\n";
    print "last day: $last_day\n";

} continue {
    print "\nEnter number of months ago or 'quit': ";
    $months_ago = <STDIN>;
}

[b]sub isleap {
    # Return true if year is a leap year, false otherwise.
    my $year = shift;
    ($year % 4 == 0) && ($year % 100 != 0) || ($year % 400 == 0);
}[/b]

 
This is great thanks, I will really appreciate if you can make it a function so I can imbed it in my existing script to set various dates for some variables like :

my $forward_bal_date = your_function(2);
my $open_bal_date = your_function(1);

which should return date in yyyy-mm-dd format

Thanks a bunch
Brenda

 
Date::Manip can do nearly anything, but be sure to read the "Should I use Date::Manip" section, as it suggests faster modules like Date::Calc instead. Surely what you're asking can be done with one of these.

________________________________________
Andrew

I work for a gift card company!
 
My favorite module for dealing with dates or times is the *drum roll please* DateTime module. It is sweet.

To get the last day of any given month in time:

Code:
use DateTime;

$date1 = DateTime->last_day_of_month( 
    year  => DateTime->now()->subtract( months => 1 )->year, 
    month => DateTime->now()->subtract( months => 1 )->month 
)->ymd("/");

$date2 = DateTime->last_day_of_month( 
    year  => DateTime->now()->subtract( months => 2 )->year, 
    month => DateTime->now()->subtract( months => 2 )->month 
)->ymd("/");

print "Last of of last month: $date1\nand of month before that: $date2\n";

--jim
 
Thanks Jim for your help, I would also need to get first day of the current month and first day of the last month, also is there a way to get the output in YYYY-MM-DD format.

Thanks much
Brenda
 
Ok I figured out how to get the date I asked above with the following routines :

$date3 = DateTime->now->truncate( to => 'month' )->ymd; #EndDate
$date4 = DateTime->now->truncate( to => 'month' )->subtract( months => 1)->ymd; #StartDate

Now I need to just year and month for $date4 in MMYYYY


Can someone please help me really quick.
Thanks a bunch.
 
SkyHigh,

This bit of code from mikevh (from earlier) explains how to split any date up into its component parts.

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon = $mon + 1; $year = $year + 1900;

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
some "times" there's no need for modules

That's just EEVILL hnnhh hnnh (read maniacal laugh- ha hah hah)

Brenda, if you want to learn enough perl to deal with dates, see Camel::Eat on
Dunno if it exists, but its defintely worth "your" while to understand what's been put in front of you.

Anything you need/want to be done, probably already has, (in coding terms anyway-;?), SEEK AND YE SHALL FIND - gwb re: WMD

FUNNY INNIT ? ????? ?????? so ...

--Paul

seriously, it's easier to find weapons of mass destruction in Manhattan than it is anywhere else. They don't have to be in Manhattan, they're just easier to find ,,, HA HAH, while you're in Manhattan, and the WMD are somewhere else.

==nix

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top