Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Want to format date and time

by joyeux (Initiate)
on Nov 17, 2012 at 16:45 UTC ( #1004324=perlquestion: print w/ replies, xml ) Need Help??
joyeux has asked for the wisdom of the Perl Monks concerning the following question:

i want to get the current date and time..and format that time to given format(DD\MM\YYYY or DD-MM-YYYY or something else)..The format is given by user..im new to perl..could u plz help me..

Comment on Want to format date and time
Re: Want to format date and time
by jonnyfolk (Vicar) on Nov 17, 2012 at 18:06 UTC
    This will give you most of what you need. Please let us know if you're missing anything else...
Re: Want to format date and time
by 2teez (Priest) on Nov 17, 2012 at 18:49 UTC

    Hi Joyeux,
    If I get you right, you probabily want to display your current date, in some format. If so, you can do like so:

    use warnings; use strict; my ( $day, $month, $year ) = (localtime)[ 3 .. 5 ]; print sprintf "%s : %s : %s\n", $day, $month + 1, $year + 1900, $/; #OR use POSIX qw(strftime); print strftime "%d - %m - %Y\n", localtime; #OR use Time::localtime; my $tm = localtime; my $day = $tm->mday(); my $month = $tm->mon() + 1; my $year = $tm->year() + 1900; print sprintf "%s \\ %s \\ %s", $day, $month, $year;
    You might also like to check this module on CPAN Date::Calc, Date::Manip

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Want to format date and time
by space_monk (Chaplain) on Nov 17, 2012 at 22:50 UTC

    Well the current local time is returned by localtime

    Formatting that time can be done in many different ways in Perl. The quickest way is probably to use the Posix strftime function.

    Putting the two together gives:

    use POSIX; print strftime( "%d/%y/%Y", localtime());
    A Monk aims to give answers to those who have none, and to learn from those who know more.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004324]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-07-30 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls