Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Dates

Status
Not open for further replies.

Newbee21369

Programmer
Oct 13, 2004
30
0
0
US
Hi,
How do I format $FILE_TS (01-JAN-05) to look like: 01/01/2005 13:36:27

Currently I tried: $FILE_TS = sprintf("%02d/%02d/%04d %02d:%02d:%02d",($tm->mon)+1,$tm->mday,($tm->year)+1900,$tm->hour,$tm->min,$tm->sec);

Printing: 05/17/2005 13:36:27
Format is what I want but it's giving today's date.
Thanks in Advance!!
 
I'd guess your $tm doesn't contain what you think it contains. How's it getting set? Can we see a bit more of your code, please?

 
Previously I had tm=localtime.
I tried this:
$Formatted_FILE_TS = sprintf("%02d/%02d/%04d %02d:%02d:%02d",($FILE_TS->mon)+1,$FILE_TS->mday,($FILE_TS->year)+1900,$FILE_TS->hour,$FILE_TS->min,$FILE_TS->sec);
But got the Error: Can't call method "mon" without a package or object reference

Any suggestions on what I need to change?

Thanks!!
 
It's not clear to me what you're trying to do. Are you trying to format a file's timestamp? (The name $FILE_TS suggests this.) You're still not showing enough of your code. Cut and paste it inside [ignore]
Code:
[/ignore] tags. If you don't know about [ignore]
Code:
[/ignore] tags, click on ProcessTGML below the box where you type your posts for an explanation.

 
I'm trying to take a non-formatted date ($FILE_TS):
01-JAN-05 and format it to look like ($Formatted_FILE_TS): 01/01/2005 13:36:27




 
What Mike is trying to convey here is that we need more background information before we can begin to help you resolve this issue. Please post the code you have currently so we can familiarize ourselves with what you're doing and then will be able to better assist you.

- Rieekan
 
One way is to use the Date::Calc module, e.g.
Code:
#!perl
use strict;
use warnings;
use Date::Calc qw(Decode_Date_EU);

my $file_ts = '01-JAN-2005';
my ($year, $month, $day) = Decode_Date_EU($file_ts);
my $out = sprintf '%02d/%02d/%04d', $month, $day, $year;
print $out, "\n";
How you expect to get the time 13:36:27 out of a string which contains only a date I don't know.




 
$FILE_TS is a date value that is returned from a stored proc. It looks like: 01-JAN-05

#!/usr/bin/perl

use Time::localtime;

sub mysub($)

{
my $FILE_TS=shift;

$Formatted_FILE_TS = sprintf("%02d/%02d/%04d %02d:%02d:%02d",($FILE_TS->mon)+1,$FILE_TS->mday,($FILE_TS->year)+1900,$FILE_TS->hour,$FILE_TS->min,$FILE_TS->sec);

print ("Formatted File_Ts: " . $Formatted_FILE_TS . "\n");

}
 
If it's just a scalar value, you can't call methods (like `mon', `mday' etc.) on it - it'd have to be an object for that. Something like this, maybe:

Code:
my %months = (
   JAN => 1,
   FEB => 2,
   MAR => 3,
   APR => 4,
   MAY => 5,
   JUN => 6,
   JUL => 7,
   AUG => 8,
   SEP => 9,
   OCT => 10,
   NOV => 11,
   DEC => 12
);
my ( $day, $month, $year ) = split '-', $FILE_TS;
$month = $months{$month};
$year += 2000;

my $formatted = sprintf "%02d/%02d/%04d", $month, $day, $year;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top