user3185596
user3185596

Reputation: 79

Perl script to convert unix month to number

I'm looking for a quick script or time/date module to convert the below Unix format

[randy@go1server03 /ftp/device]$ date.

Thu Apr 24 13:48:28 UTC 2014

to something like only 04-24 , where 04 is month April and 24 is the date. Is their any quick and dirty way to do this?

Thanks in advance

Upvotes: 1

Views: 441

Answers (4)

Oesor
Oesor

Reputation: 6642

perl -MDateTime -E'say DateTime->now->format_cldr("MM-dd")'

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98921

You can use this:

#!/usr/local/bin/perl
use strict;
use warnings;
use Time::Piece;

my $date = localtime->strftime('%m-%d');
print $date;

DEMO: http://ideone.com/K1hVy8

Upvotes: 5

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24063

I recommend using the core module Time::Piece for date manipulation in Perl.

Here's a one-liner (using the current time):

perl -MTime::Piece -wE '$t = Time::Piece->new; say $t->strftime( q{%m-%d} );'

Or to do this in a script:

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $t = Time::Piece->new;
say $t->strftime('%m-%d');

Upvotes: 3

William Pursell
William Pursell

Reputation: 212248

Why do you want perl? Try;

date +%m-%d

Upvotes: 3

Related Questions