date time perl code example

Example 1: perl set a specific datetime

# For Perl only

# syntax
use DateTime;
my $YourDate = DateTime->new(
    year       => <your-year>,
    month      => <your-month>,
    day        => <your-day>,
    hour       => <your-hour>,
    minute     => <your-minute>,
    second     => <your-second>,
    nanosecond => <your-nanosecond>,
);

# example: 
use DateTime; 
my $YourDate = DateTime->new(
    year       => 1994,
    month      => 05,
    day        => 25,
    hour       => 13,
    minute     => 31,
    second     => 18,
    nanosecond => 0,
);

Example 2: perl get date

# For Perl only

# syntax
use POSIX;
use Time::Piece;
my $CurTime = localtime(); # <-- Format: Day Month  Date HH:MM:SS (ei: Thu May  7 08:45:52 2020)
my $FormattedTime = $local_time->strftime('<FormatOfDateTime>');

# example 
use POSIX;
use Time::Piece;
my $CurTime = localtime();
print "[CurTime:$CurTime]\n";

# Note: for formatting, search Perl DateTime Formatting

Tags:

Perl Example