Products
Services
Contact Us

Scripts: Perl :: Date and time :: Library Article #5

Developer's Section

Computing the Current Date and extracting its individual values in Perl
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (3536)
Favorites:

This article describes how to compute the current date in Perl and teaches you how to extract its individual values.

In is very easy to extract common values from a Date Object into an array such as the year, month, day, hour, minutes, and seconds. All this requires is declaring the array with the individual variables passed from the function call localtime(time).

Example:

 Code Snippet 1

#!/usr/bin/perl -w

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

use Time::Local;

print "Content-type: text/plain\n\n";

#declare array returned from localtime
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);

#print the date using newly extracted values
printf "%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900,$mon+1,$mday,$hour,$min,$sec;

#calculate system time again from the extracted values
my $time2 = timelocal($sec,$min,$hour,$mday,$mon,$year);

#calculate the difference (in seconds) of two system times
$difference = time - $time2;

exit 0; 




See other Scripts in Date and time

Submit Your Scripts:


System Message:
  • Your Perl script has been sent. Please allow 48 hours for processing.


If you would like to have your Perl scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]