The typical way to handle this kind of string formatting is with sprintf:
$date_string = sprintf( "%2.2d-%2.2d-%d %2.2d:%2.2d:2.2d", $month, $day, $year, $hour, $min, $sec );
(Your explanation didn't mention seconds, but your code did, so I wrote the expression to produce "MM-DD-YYYY HH:MI:SS")

I think there's another way to assure padding with leading zeros, but the "%N.Nd" is what I grew up with from using C in olden times... The first digit (before the decimal point) represents the field width, and the second digit (after the decimal) represents "precision" (i.e. number of visible digits to output, using leading zeroes as needed); the "d" means make the result look like an integer value (as opposed to "f" for floating point, where "precision" would refer to the number of digits to the right of the decimal point, and field width would usually be larger than precision).


In reply to Re: Zero-padding integers and the numbers-as-strings Red Flag by graff
in thread Zero-padding integers and the numbers-as-strings Red Flag by Ionizor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.