We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Date Format Conversion

Purdue_Pete
Purdue_Pete asked
on
Medium Priority
819 Views
Last Modified: 2013-12-25
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.
Comment
Watch Question

#!/usr/bin/perl

use date::Format;

$string = "10 Feb 2009 19:13";
$time = str2time($string);
print time2str("%Y/%m/%d", time), "\n";
typo:please read below
print time2str("%Y/%m/%d", time), "\n";
to
print time2str("%Y/%m/%d", $time), "\n";

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
The solution above is perfect but this works on my machine:

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

print time2str("%Y-%m-%d", $time), "\n";
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#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;
Commented:
($day, $month, $year, $time) = split(/\s/, $currentdate);
$date = join('-', $year,$month,$day);

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.