Output: Current date and time: 2023-12-11 07:57:22use 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: The date 3 days after current date: 2023-12-14use 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 249 days before the current date is: 2023-04-06use 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: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 last Saturday of the specified year 2023 is on: 2023-12-30use 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 day of the week for 2023-4-10 is: Mondayuse 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: Current date and time: Monday 11 December 2023use 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");
use Date::Tiny $today = Date::Tiny->now; print "Year : " . $today->year . "\n"; print "Month: " . $today->month . "\n"; print "Day : " . $today->day . "\n";
Theuse 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";
use DateTime::Tiny; $now = DateTime::Tiny->now; print "Stringified date format: " . $now->ymdhms . "\n"; # 2023-12-18T16:10:16