in reply to Getting a date/time in a certain format in a Windows and Linux environment

Here you go. localtime will show you how I derived this solution. If the date parts didn't require manipulation you could make this a little more eloquent and use 2 joins and slices, then concatenate those two together. a join on / for the date and a join on : for the time... (which is doable but still requires modification of the approriate values prior to joining)

#!/Perl/bin/perl use strict; use warnings; my @time = (localtime); my $formatted_time = ($time[4] + 1)."/".$time[3]."/".($time[5]+1900)." + ".$time[2].":".$time[1].":".$time[0]; print $formatted_time;


Grygonos
  • Comment on Re: Getting a date/time in a certain format in a Windows and Linux environment
  • Download Code

Replies are listed 'Best First'.
Re^2: Getting a date/time in a certain format in a Windows and Linux environment
by VSarkiss (Monsignor) on Aug 16, 2004 at 20:51 UTC

    A word of caution: those won't necessarily give you mm/dd/yyyy. You'll need to use sprintf or printf to make sure you get 2 digits in the month and day. Similar considerations for the time portion:

    my $formatted_time = sprintf "%02d/%02d/%04d %02d:%02d:%02d", $time[4] + 1, $time[3], $time[5] + 1900, $time[2], $time[1], $time[0];

      excellent point. I need to brush up on my use of sprintf