crazyim5
crazyim5

Reputation: 714

Perl Date in this format

How do I get date & time in this format in perl? Y-m-d\TH:i:s\Z T represents the time part and Z represents the time zone.

For example, 2012-11-28T17:21:11+0100

I think in php you can do using gmdate("Y-m-d\TH:i:s\Z")

Upvotes: 0

Views: 166

Answers (3)

clt60
clt60

Reputation: 63902

one of possible solutions:

use 5.010;
use warnings;
use Time::Piece;
say localtime->strftime("%Y-%m-%dT%T%z");

or oneliner

perl -MTime::Piece -E 'say localtime->strftime("%Y-%m-%dT%T%z");'

prints

2014-09-06T01:36:39+0200

Upvotes: 2

Brian Ridgeway
Brian Ridgeway

Reputation: 255

TIMTOWTDI

Using POSIX strftime

use strict;
use warnings;
use 5.010;
use POSIX qw/ strftime /;

say strftime( "%Y-%m-%dT%T%z", localtime );

One Liner:

perl -MPOSIX -E 'say strftime("%Y-%m-%dT%T%z", localtime);'

Outputs:

2014-09-05T20:11:34-0400

Upvotes: 3

Miller
Miller

Reputation: 35198

Use Time::Piece:

use strict;
use warnings;

use Time::Piece;

print localtime->strftime('%Y-%m-%dT%H:%M:%S%z'), "\n";

Outputs:

2014-09-05T16:39:54-0700

Upvotes: 3

Related Questions