[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

Date Format Conversion

Hi,
I would like to convert in perl the following date format:
"10 Feb 2009 19:13"

to the following:
"2009-02-10"

Thank you.
0
Purdue_Pete
Asked:
Purdue_Pete
2 Solutions
 
SimonHowaldCommented:
#!/usr/bin/perl

use date::Format;

$string = "10 Feb 2009 19:13";
$time = str2time($string);
print time2str("%Y/%m/%d", time), "\n";
0
 
SimonHowaldCommented:
typo:please read below
print time2str("%Y/%m/%d", time), "\n";
to
print time2str("%Y/%m/%d", $time), "\n";
0
 
Fero45Commented:
The solution above is perfect but this works on my machine:

use Date::Format;
use Date::Parse;

print time2str("%Y-%m-%d", $time), "\n";
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
#If you want to do it wthout modules
@mon{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = ('01'..'12');
$_ = "10 Feb 2009 19:13";
s#(\d+)\s*(\w+)\s*(\d+).*#$3-$mon{$2}-$1#;
print;
0
 
IceCrackCommented:
($day, $month, $year, $time) = split(/\s/, $currentdate);
$date = join('-', $year,$month,$day);

0
 
Fero45Commented:
With modules you can convert dates in different formats, e.g.

22 Feb 09 7:30:44
23/Feb/09 7:30:44
13-Feb-08 7:30:44
Jan 25 09 7:30

with or without hours, minutes and seconds
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now