Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: help on getting date

by crouchingpenguin (Priest)
on Jun 02, 2003 at 18:23 UTC ( [id://262449]=note: print w/replies, xml ) Need Help??


in reply to help on getting date

1. Is there a way to get the system date using perl in the following format mm-dd-yyyy?

Yes, using POSIX::strftime:

print POSIX::strftime("%m-%d-%Y",localtime()),"\n";

2. is there a way to compare the dates to see which is greater or lesser then a given date which is in mm-dd-yyyy format?

Yes, using POSIX::mktime() and optionally POSIX::difftime():

my $start_date_string = '05-03-2003'; my @date_parts = split('-',$start_date_string); # build time my $then_time = POSIX::mktime( 0,0,0, $date_parts[1], ($date_parts[0] -1), ($date_parts[2] - 1900) ); ### or could be mktime() from another split string here my $now_time = time(); #could also use POSIX::difftime: #"the time difference (in seconds) between two times #(as returned by 'time()')" if( $then_time < $now_time ){ print POSIX::ctime($then_time) . " was before " . POSIX::ctime($now_time) . "\n"; }else{ print POSIX::ctime($then_time) . " was after " . POSIX::ctime($now_time) . "\n"; }

All that fun and POSIX comes with core perl.

Update:Oops, thanks rob_au for catching my typo.


cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."

Replies are listed 'Best First'.
Re: Re: help on getting date
by rob_au (Abbot) on Jun 03, 2003 at 06:12 UTC
    Excellent post. Just one point which I would pick up on, is that values for the month, weekday and day of the year passed to POSIX::mktime begin at zero. As such, the determination of $then_time in your post should alternatively read:

    my $then_time = POSIX::mktime ( 0, # Seconds 0, # Minutes 0, # Hours $date_parts[1], # Day (of month) $date_parts[0] - 1, # Month $date_parts[2] - 1900 # Year );

    This behaviour is documented on the POSIX module man page.

     

    perl -le 'print+unpack"N",pack"B32","00000000000000000000001001100100"'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://262449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found