How can I elegantly print the date in RFC822 format in Perl?
say $datetime->month; say $datetime->day; say $datetime->strftime( '%Y-%m-%d-%H-%M-%S' );
You can use the POSIX function strftime() in Perl to format the date and time with the help of the following table. Please note that the specifiers marked with an asterisk (*) are locale-dependent. Specifier. Replaced by.
To get the day, month, and year, you should use localtime : my($day, $month, $year)=(localtime)[3,4,5]; To then get the last day of the month, use your code from above: $av_tmp_TODAY = DateTime->last_day_of_month({ year => $year + 1900, month => $month +1 })->ymd('-');
use DateTime qw( ); my $yday_date = DateTime ->now( time_zone => 'local' ) ->set_time_zone('floating') ->truncate( to => 'day' ) ->subtract( days => 1 ) ->strftime('%Y-%m-%d');
use POSIX qw(strftime);
print strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())) . "\n";
The DateTime suite gives you a number of different ways, e.g.:
use DateTime;
print DateTime->now()->strftime("%a, %d %b %Y %H:%M:%S %z");
use DateTime::Format::Mail;
print DateTime::Format::Mail->format_datetime( DateTime->now() );
print DateTime->now( formatter => DateTime::Format::Mail->new() );
Update: to give time for some particular timezone, add a time_zone argument to now():
DateTime->now( time_zone => $ENV{'TZ'}, ... )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With