Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Working with date

by alk1000 (Initiate)
on Jun 04, 2003 at 01:56 UTC ( #262860=perlquestion: print w/replies, xml ) Need Help??
alk1000 has asked for the wisdom of the Perl Monks concerning the following question:

Holy Monks,

I've just 3 short questions about date:

1- if i've $newdate = `date`;
i'll get $newdate equal to "Wed Jun 4 11:30:16 EST 2003". What's the best way to change the $newdate to "04-06-2003 11:30:16". the way i figured out is pretty long.

2- Using localtime in perl, how can i print the date as dd-mm-yyyy (i.e. 04-06-2003). do i've to use printf or is there any other way?

2- How can i add 10 days to $newdate to get a new variable $deldate, with automatic month/year increment if the case calls.

Tanx in advance, AnA (newbie)

Replies are listed 'Best First'.
Re: Working with date
by edoc (Chaplain) on Jun 04, 2003 at 03:09 UTC

    I have to recommend looking at some date modules on cpan but there are a lot to choose from so if that is all you need to do, and all you'll ever need to do you could use something like this:

    #!/usr/bin/perl -w use strict; my $time = time; my $date = date_format($time); print "Now: $date\n"; my $tendays = date_format( $time + ( 10 * 24 * 60 * 60 ) ); print "10 days from now: $tendays\n"; my $thirtydays = date_format( $time + ( 30 * 24 * 60 * 60 ) ); print "30 days from now: $thirtydays\n"; sub date_format{ my $seconds = shift; my @bits = localtime($seconds); $bits[4]++; $bits[5] += 1900; return sprintf "%02.0f-%02.0f-%04.0f %02.0f:%02.0f:%02.0f", ($bits[3 +],$bits[4],$bits[5],$bits[2],$bits[1],$bits[0]); }

    my sprintf'n prolly isn't the best, but it seems to work..



      my sprintf'n prolly isn't the best, but it seems to work.

      Since you are printing integers, you should really use %d rather than %f. That and an array slice clean it up nicely...

      sprintf "%02d-%02d-%04d %02d:%02d:%02d", @bits[3,4,5,2,1,0];

      "My two cents aren't worth a dime.";

        ahh, much better! ++



      TANX mate, u're a star!
Re: Working with date
by crouchingpenguin (Priest) on Jun 04, 2003 at 11:51 UTC

    See this recent node: help on getting date

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Working with date
by jaa (Friar) on Jun 04, 2003 at 08:22 UTC

    using backticks to get date - not pretty at all - but you should try
      man date
    as *NIX date will format its output any which way you want

    Better yet search CPAN for a date module that supports your processing functionality.

Re: Working with date
by nite_man (Deacon) on Jun 04, 2003 at 06:40 UTC


    Try to look at my modules from 'Date' DateTime Project. Maybe it will be useful for you :-))
    SV* sv_bless(SV* sv, HV* stash);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://262860]
Approved by VSarkiss
and all is calm...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-02-17 20:43 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (249 votes). Check out past polls.