perl datetime strftime 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 set date

# For Perl only

# syntax
use Time::Piece;
Time::Piece->strptime('<Date-time-input>', '<format-of-input>');

# examples (for: 25 May 1994 13:31:18)

# OPTION A
use Time::Piece;
my $date_1 = Time::Piece->strptime('1994-05-25 13:31:18', '%Y-%m-%d %X');
print $date_1 . "\n";

#OPTION B
use Time::Piece;
my $date_2 = Time::Piece->strptime('13:31:18 25/05/1994', '%X %d/%m/%Y');
print $date_2 . "<---\n";

# For more details on formatting symbols (%...), please scroll to the 
# bottom of "https://www.tutorialspoint.com/perl/perl_date_time.htm"

Tags:

Perl Example