Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How can I format the output of localtime?

( #16212=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 03, 2000 at 22:45 UTC
Q&A  > dates and times


Description:

When every I use stat() to get the date a file was last modified I get a weird number such as 959907928. Is there a way to format this into a normal date such as 6/3/00 without using Date::Manip?

Answer: How can I format the output of localtime?
contributed by Zaxo

The most convenient formatter for localtime output is &POSIX::strftime. It is similar to an sprintf call, taking a format string as its first argument, and a localtime compatible list as the remaining arguments. The formats are specialized for time units, and extract the strings without need for displacements in the data. The complete list of formats is given in man 3 strftime. A locale-sensitive date can be formatted with the "%x" format string:

use POSIX 'strftime'; my $datestring = strftime 'Today is %x.', localtime; print $datestring, $/;
The format inquired of is given (with leading zeros) by the "%D" format string. Full names of months and days in the language of the current locale are given by the "%B" and "%A" formats, and their abbreviations by "%b" and "%a".

Answer: How can I format the output of localtime?
contributed by KM

Look at the POSIX module as well, it comes with Perl. Look at the POSIX::strftime method.

Cheers,
KM

Answer: How can I format the output of localtime?
contributed by reptile

localtime takes those big weird numbers (which btw are the number of seconds since the epoch, which is normally Jan 1. 1970) and returns either the ctime(3) string in scalar context, or an array of values describing the date. Look on the localtime manpage for more.

You can also look at Date::Format on CPAN. With that, something like:

time2str("%D", $time);

will format $time to the "MM/DD/YY" format you were looking for. See Date::Format's documentation for all the format specifiers.

Answer: How can I format the output of localtime?
contributed by Sol-Invictus

at it's simplest:

print scalar(localtime(time)),"\n";
Answer: How can I format the output of localtime?
contributed by Roy Johnson

If you want to do it without using any modules, and you want the m/d/y (or d/m/y, season to taste) format,

# Assuming you've got $timeval from a stat() call or some such my ($d,$m,$y) = (localtime($timeval))[3,4,5]; my $mdy = sprintf '%d/%d/%d', $m+1, $d, $y+1900;
Answer: How can I format the output of localtime?
contributed by jacques

This is an update of Roy Johnson's great answer, for those who just want the current time formatted in m/d/y.

my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy = sprintf '%d/%d/%d', $m+1, $d, $y+1900;
Or if month and day should always be two digits:
my $mdy = sprintf '%02d/%02d/%04d', $m+1, $d, $y+1900;
Answer: How can I format the output of localtime?
contributed by Mago

sub TimeStamp { my($format) = $_[0]; my($return); (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday +, my $yday, my $isdst) = localtime(); $year = $year + 1900; $mon = $mon + 1; if (length($mon) == 1) {$mon = "0$mon";} if (length($mday) == 1) {$mday = "0$mday";} if (length($hour) == 1) {$hour = "0$hour";} if (length($min) == 1) {$min = "0$min";} if (length($sec) == 1) {$sec = "0$sec";} if ($format == 1) {$return = "$year\-$mon\-$mday $hour\:$min +\:$sec";} if ($format == 2) {$return = $mon . $mday . $year;} if ($format == 3) {$return = substr($year,2,2) . $mon . $mda +y;} if ($format == 4) {$return = $mon . $mday . substr($year,2,2 +);} if ($format == 5) {$return = $year . $mon . $mday . $hour . +$min . $sec;} if ($format == 6) {$return = $year . $mon . $mday;} if ($format == 7) {$return = $mday .'/'. $mon .'/'. $year .' + '. $hour .':'. $min .':'. $sec;} if ($format == 8) {$return = $year . $mon . $mday . $hour . +$min;} if ($format == 9) {$return = $mday . '/' . $mon . '/' . $yea +r;} return $return; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2014-07-11 05:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (219 votes), past polls