Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

DateTime formatting problem

by PerlSufi (Friar)
on Apr 30, 2013 at 17:45 UTC ( #1031439=perlquestion: print w/replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have a problem formatting my dates with DateTime module. This code:
use DateTime; use Date::Format; my $one_month_ago = DateTime->today->subtract(months => 1); print $one_month_ago = DateTime->last_day_of_month( month => $one_month_ago->month, year => $one_month_ago->year, )->strftime("%m%d%y"); my $start_date = DateTime->today->subtract( months => 1); print $start_date->strftime("%m%d%y");
returns the date in the formats that I want. (MMDDYY)
This code that I have with mechanize entering the information in fields on a website, however, gives me the start date as MMDDYYT00:00:00.
my $start_date = DateTime->today->subtract( months => 1); $start_date->strftime("%m%d%y"); print $start_date; my $end_date = DateTime->today->subtract(months => 1); $end_date = DateTime->last_day_of_month( month => $end_date->month, year => $end_date->year, )->strftime("%m%d%y"); print $end_date; #This block of code is intended to fill in the required forms $mech->get(""); my $usr = "asdf"; my $pw = "asdf"; $mech->form_number(1); $mech->field( "capsn", $usr); $mech->form_number(2); $mech->field("capsp", $pw); $mech->form_number(3); $mech->field( "startdate", $start_date); $mech->form_number(4); $mech->field( "enddate", $end_date); $mech->form_number(5); $mech->select("format", "2"); $mech->click();
Why is this happening? Thanks in advance for your input..

Replies are listed 'Best First'.
Re: DateTime formatting problem
by moritz (Cardinal) on Apr 30, 2013 at 17:59 UTC
    $start_date->strftime("%m%d%y"); print $start_date;

    strftime does not modify the invocant ($start_date) in any way. Rather it returns a string that is the result of formatting the invocant.

    Since you throw away the result of calling ->strftime, and then print $start_date again in the next line, I guess you haven't fully understood this principle.

    So the answer is to work with the return value of the ->strftime call, not with the original object.

      Thanks. So on the first (tomorrow) it should return my start date as 040113 and my end date as 043013, right?
Re: DateTime formatting problem
by PerlSufi (Friar) on Apr 30, 2013 at 17:54 UTC
    I found that changing
    my $start_date = DateTime->today->subtract( months => 1); $start_date->strftime("%m%d%y");

    my $start_date = DateTime->today->subtract( months => 1)->strftime("%m +%d%y");


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1031439]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-03-20 22:23 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (260 votes). Check out past polls.