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!

taking year out of localtime function. 4

Status
Not open for further replies.

perlTest

Programmer
Nov 16, 2002
14
0
0
US
Hello,

I have this for my local time, which generated by perl: Sat Dec 21 17:38:24 2002. How do I take out the year and leave the rest? Thanks if you can help.
 
If you are willing to get what you want directly, you can call
for instance the Unix date system command with the right flags
e.g.
my $string= system("echo `date %Y`);

if you have the string you say above and just want the year


my $string='Sat Dec 21 17:38:24 2002';
die unless $string=~/(.*)\s(.*)$/;
my $year=$2;
print "year=$year\n";
 
Hi,

I want to take out the year and print just the date. Like for an example, I want to print following:

Sat Dec 21 17:38:24

Thank you for your reply!
 
You could try something like splitting the date string on the space character, then pop the last element off the existing array, then join the array back together with spaces, then print the string. It's a little messy, but it works.

============
$thetime = scalar localtime;
@timearray = split / /, $thetime;
pop @timearray;
$thetime = join ' ', @timearray;
print "$thetime\n";
=============
 
$string=~ s/(.*\s)\d{4}$/$1/s; 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Localtime has an array output too!

Code:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
print qq~$wday $mon $mday $hour:$min:$sec~;

Then, you could also easily rearrange any of the items you want. Go to and search for "localtime" for more info about this function.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top