Date and Time in PERL

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date and Time in PERL
# 1  
Old 06-23-2010
Date and Time in PERL


I want to get the current date and time and subtract 1 day to get to the
previous day?

I see timelocal( ) and (time) etc.

How do I code this in PERL to get the previous day?

# 2  
Old 06-23-2010
my $minus_one_day = time - 86400;
my $yesterday = localtime $minus_one_day;

print("yesterday: $yesterday \n");

# 3  
Old 06-23-2010
  # usage: days_ago n
  # where n = number of days before today
  perl  -e   '
        # take 86400 * # of days from right now in epoch seconds
                 $yestertime = time - (86400 * $ARGV[0]);
                 $month = (localtime $yestertime)[4] + 1;
        # day of the month
                 $day = (localtime $yestertime)[3];
        # year
                 $year = (localtime $yestertime)[5] + 1900;
        # format mm/dd/yyyy
                 printf( "%02d/%02d/%d\n", $month, $day, $year);  '  $1

days_ago 1

This is a shell example, steal what you need for a perl script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl:Script to append date and time stamp

Help with Perl script : I have a web.xml file with a line <display-name>some_text_here</display-name> Need to append the current date and time stamp to the string and save the XML file Something like <display-name>some_text_here._01_23_2014_03_56_33</display-name> -->Finally want... (5 Replies)
Discussion started by: gaurav99
5 Replies

2. Shell Programming and Scripting

Perl : Module to get the last week date and time

Hello folks, I am looking for a Perl module or a program logic that gives the startdate and enddate of the last week.. Suppose say, assuming this week starts from Sunday to Saturday If I execute the script in this week. I need to get the last week sunday's date. Could anyone please... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

PERL, Date & Time issues

Hello All, This is my first script in PERL. Hence require your help in moving further. I have a script which should populate the values for Today, Yesterday output. For which I use timeFrame as a variable to obtain the time in hrs:mm as 10:00. All I want is, I want my timeFrame to start... (4 Replies)
Discussion started by: sathyaonnuix
4 Replies

4. Shell Programming and Scripting

perl : searching for month and storing the date and time in an array

I am writing the code in perl. I have an array in perl and each variable in the array contains the data in the below format Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug'... (5 Replies)
Discussion started by: giridhar276
5 Replies

5. Shell Programming and Scripting

How to find last updated date and time of a folder in Perl?

Hi All, I have a process which after some time continues move a files to some folder(say the name of the folder is logdir) What i am trying to do is as the files are coming to the logdir folder, I want the latest updated time and date of the folder in PERL. (1 Reply)
Discussion started by: parthmittal2007
1 Replies

6. Shell Programming and Scripting

Creating a date (without time) in perl

I have a perl script that automatically runs on Mondays. I need to have it create a variable for last Monday's date thru that Sunday's date. example: 04-01-2011 thru 04-08-2011 Its reporting numbers for the previous week beginning with Monday and ending on Sunday. So i dont have to go in... (7 Replies)
Discussion started by: bbraml
7 Replies

7. UNIX for Dummies Questions & Answers

Formatting date time in unix using perl breaks

while read l do vTimeCreated=`perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' ${l}` echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt done < servername.txt Using the above code to format date time for each of... (5 Replies)
Discussion started by: HeadBang
5 Replies

8. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

9. Shell Programming and Scripting

perl DBI inserting date\time

hi there, my mysql database has a date/time field using the standard mysql date|time format of 2009-08-31 00:16:44 when inserting into this field using perl DBI, is there is an easy way to insert the current date/time in without having to preformat the date/time string in perl before... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

10. Shell Programming and Scripting

Perl - naming a file after the date/time

I'm making log-files for 4different hp-ux's and I need to name the files after the date so I can keep the files. The way I want the filename to look is like this: yymmdd.log ( 020522<CompName>.log ). Can anyone please give me a helping hand. :confused: I have made the log-script and... (1 Reply)
Discussion started by: tgreig
1 Replies
Login or Register to Ask a Question

Featured Tech Videos