4. Example I: date & time

Below an example using the module DateTime.
4.1 Find the current date

use DateTime;

# Create a new DateTime object for the current date and time 
$dt = DateTime->now;

# Print the current date and time 
print("Current date and time: ", $dt->ymd, " ", $dt->hms, "\n");
Output: Current date and time: 2023-12-11 07:57:22
4.2 Add 3 days to the current date

use DateTime;

# Create a new DateTime object for the current date and time 
$dt = DateTime->now;

# Add 3 days to the current date 
$dt->add(days => 3);

# Print the updated date 
print("The date 3 days after current date: ", $dt->ymd, "\n");
Output: The date 3 days after current date: 2023-12-14
4.3 Calculate the date 249 days before the current date

use DateTime;

# Get the current date and time 
$current_date = DateTime->now;

# Calculate the date 249 days ago 
$date_249_days_ago = $current_date->clone->subtract(days => 249);

# Print the result 
print("The date 249 days before the current date is: ", $date_249_days_ago->ymd, "\n");
Output: The date 249 days before the current date is: 2023-04-06
4.4 Find the date and weeknumber of the first Monday in a specified month in a specified year

use DateTime;

# Specify the year 
$year = 2023;
$month = 12;

# Create a DateTime object for the first day of a specified year 
$dt = DateTime->new(year => $year, month => $month, day => 1);

# Find the first Monday by looping until Monday is reached 
while ($dt->day_of_week != 1) { # 1 corresponds to Monday 
    $dt->add(days => 1);
}

# Print the date of the first Monday 
print("The requested first Monday in month ", $month , " of year ", $year," is on: ", $dt->ymd, "\n");

print($dt->ymd, " is in week number ", $dt->week_number, ", in week ", $dt->week_of_month, " of the month.\n");
Output:
The requested first Monday in month 12 of year 2023 is on: 2023-12-04
2023-12-04 is in week number 49, in week 1 of the month.
4.5 The last Saturday of a specified date

use DateTime;

# Specify the date 
$year = 2023;
$month = 12;
$day = 31;

# Create a DateTime object for the specified date 
$current_date = DateTime->new(year => $year, month => $month, day => $day);

while ($current_date->day_of_week != 6) { # 6 corresponds to Saturday 
    $current_date->subtract(days => 1);
}

print("The last Saturday of the specified year $year is on: ", $current_date->ymd, "\n");
Output: The last Saturday of the specified year 2023 is on: 2023-12-30
4.6 Get the day name for a specified date

use DateTime;

# Specify the date 
$year = 2023;
$month = 4;
$day = 10;

# Create a DateTime object for the specified date 
$dt = DateTime->new(year => $year, month => $month, day => $day);

# Get the day name for the specified date 
$day_name = $dt->day_name;

# Print the result 
print("The day of the week for $year-$month-$day is: $day_name\n");
Output: The day of the week for 2023-4-10 is: Monday
4.7 Formatting output with strftime

use DateTime;

# Create a new DateTime object for the current date and time 
$dt = DateTime->now;

# Print the result 
print("Current date and time: " . $dt->strftime("%A %d %B %Y") . "\n");
Output: Current date and time: Monday 11 December 2023

Exit: Date::Tiny and DateTime::Tiny

The module DateTime loads slow and uses much memory. Date::Tiny and also DateTime::Tiny are fast alternatives. Their basic usage is similar. To show only the current date use Date::Tiny:

use Date::Tiny

$today = Date::Tiny->now;
print "Year : " . $today->year  . "\n";
print "Month: " . $today->month . "\n";
print "Day  : " . $today->day   . "\n";
DateTime::Tiny is useful when you need date and time:

use DateTime::Tiny

$now = DateTime::Tiny->now;
print "Year   : " . $now->year   . "\n";
print "Month  : " . $now->month  . "\n";
print "Day    : " . $now->day    . "\n";
print "Hour   : " . $now->hour   . "\n";
print "Minute : " . $now->minute . "\n";
print "Second : " . $now->second . "\n";
The ymdhms method returns the most common and accurate stringified date format, including the 'T' separator:

use DateTime::Tiny;

$now = DateTime::Tiny->now;
print "Stringified date format: " . $now->ymdhms . "\n"; # 2023-12-18T16:10:16