Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the next date of given date using perl?

Tags:

perl

I need to find the next date of any date.

use strict;

my $entered_date="2011-11-30";

In the above code I want to find the next date of the date, which stored in $entered_date variable... Please share your solutions....

Thanks in Advance...

like image 638
Madhan Avatar asked Feb 15 '12 12:02

Madhan


People also ask

How do I get the difference between two dates in Perl?

$bree = 361535725; # 16 Jun 1981, 4:35:25 $nat = 96201950; # 18 Jan 1973, 3:45:50 $difference = $bree - $nat; print "There were $difference seconds between Nat and Bree\n"; There were 265333775 seconds between Nat and Bree $seconds = $difference % 60; $difference = ($difference - $seconds) / 60; $minutes = $difference ...

How do I get yesterday's date in Perl?

If you want yesterday's date: 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'); For example, in New York on 2019-05-14 at 01:00:00, it would have returned 2019-05-13.

What is the purpose of time () in Perl?

localtime() function in Perl returns the current date and time of the system, if called without passing any argument.

How do I print a date in Perl?

Perl POSIX Specifiers The formatted date and time values can be printed by using the strftime() function of PERL by using different types of specifiers preceded with the (%) sign. Two types of specifiers are used in PERL.


2 Answers

use Time::Piece;
use Time::Seconds;
my $date = Time::Piece->strptime($entered_date, "%Y-%m-%d");
$date += ONE_DAY;
return $date->strftime("%Y-%m-%d");

[ edit: changed %F to %Y-%m-%d for Wintendo compatibility. Thanks, @bvr ]

like image 155
al. Avatar answered Sep 20 '22 16:09

al.


perl -MDate::Calc -E ' use Date::Calc qw(Add_Delta_Days);say join "-", Add_Delta_Days(2011,11,30,1);'

2011-12-1

In a script (not very safe)

use Date::Calc qw(Add_Delta_Days);
my $entered_date = "2011-12-31";
print join "-", Add_Delta_Days(split(/-/,$entered_date),1);'

2012-1-1

like image 37
knb Avatar answered Sep 18 '22 16:09

knb