Solved

date format  in perl

Posted on 2011-09-30
7
265 Views
Last Modified: 2012-05-12
Hi,
I am searching for data based on date. so what format do I need to use for the below example

Wed Sep  7 02:33:14 EDT 2011

$date= ???
0
Comment
Question by:new_perl_user
7 Comments
 
LVL 7

Accepted Solution

by:
whosbetterthanme earned 500 total points
Comment Utility
my $date = scalar localtime(time());

# this will be close.
0
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
Nice one!
I've been using Perl for 15 years and it's the first time I run into this wicked trick.
Thank you for sharing whosbetterthanme.
You are starting to deserve your nickname... :-)
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
since assigning to a scalar implies scalar context, you don't need the explicit scalar function,
and since localtime defaults to using time, you don't need the explicit time function either.

to get exactly the format you requested, you can
use POSIX qw(strftime);  
my $date = strftime "%a %b %e %H:%M:%S %Z %Y", localtime;  
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:parparov
Comment Utility
You can do without time() since it's default argument of localtime.
However the asker seems to be looking for a date that is not now.

Then my $date = scalar localtime($timestamp_you_have); will do the trick.
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Slightly shorter than ozo's version

use POSIX qw(strftime);  
my $date = strftime "%a %b %e %T %Z %Y", localtime;  

Open in new window



jermonee, are you being serious?  In 15 years, you've never used
$date=localtime();

Open in new window

0
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
Nope.
Always used strftime or sprintf.
Like Larry Wall would say: http://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it
0
 
LVL 7

Expert Comment

by:whosbetterthanme
Comment Utility
Well, even though new perl user hasn't responded yet, I suppose it's a matter of old school and using what works!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now