Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

formatting integers for printing

by newatperl (Acolyte)
on May 14, 2001 at 00:52 UTC ( [id://80099]=perlquestion: print w/replies, xml ) Need Help??

newatperl has asked for the wisdom of the Perl Monks concerning the following question:

I'm grabbing the date from localtime(time) and now I want to print it, however, I need all values of the dates to be 2 digits, for example I would like to print 04/09/01 instead of 4/9/1. Is there a simple elegant way to do this? I could do this using a hash, but, is there any other better way? Thanks.

Replies are listed 'Best First'.
Re (tilly) 1: formatting integers for printing
by tilly (Archbishop) on May 14, 2001 at 01:49 UTC
    Is that date supposed to be April 9, 2001 or September 4, 2001?

    If you look into it you will find that people differ on how they interpret that (largely depending on where they are from). In fact there are other combinations possible, I am not sure how many are in common use. The above two are just the ones that I have lived with.

    For that reason if at all possible I suggest going with the ISO standard 2001-04-09 (or 2001-09-04 if you meant the other thing).

    Wins. First of all nobody that I know of finds that at all confusing. Secondly when you come back wanting to sort these dates it will be easy because we will all tell you to sort, and alphabetical order will be right. Thirdly if you ever need to compare dates it will be trivial to tell what comes first.

    So, less work for you. Doesn't confuse anybody. And it is actually an ISO standard. What is not to like? :-)

Re: formatting integers for printing
by lemming (Priest) on May 14, 2001 at 01:00 UTC
    printf("%02d/%02d/%02d", $month, $day, $year);
    Assuming US centric there with variable names...

    You may want to check into sprintf as well.
      printf("%02d/%02d/%02d", $month, $day, $year); If you're setting those variable straight from localtime(), and want to avoid displaying "04/13/101" intsead of "05/13/01", best use   printf("%02d/%02d/%02d", $mon + 1, $mday, $year % 100);
(dkubb) Re: (2) formatting integers for printing
by dkubb (Deacon) on May 14, 2001 at 09:00 UTC

    ++tilly on the ISO date advocacy post.

    I'm not a big fan of using sprintf or other home-grown methods for formatting date strings. IMHO, it's much cleaner to use CPAN's Date::Format module. You get clearer code, and more consistent results:

    #!/usr/bin/perl -w use strict; use Date::Format; my $iso_date = time2str '%Y-%m-%d', time;

    This will return the date in ISO format: YYYY-MM-DD

Re: formatting integers for printing
by cLive ;-) (Prior) on May 14, 2001 at 08:27 UTC
    Here's what I'd use...
    my @date = localtime; $date[4]++; $date[5]%=100; printf("European: %02d/%02d/%02d\n",@date[3,4,5]); printf("US/Canada/???: %02d/%02d/%02d\n",@date[4,3,5]);
    cLive ;-)
Re: formatting integers for printing
by Anonymous Monk on May 14, 2001 at 19:06 UTC
    my $your_int = 3;
    printf ("%2d", $your_int);
    #
    # or if you want to make a string with 03;
    #
    my $string_03 = sprint ("%2d", $your_int);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found