By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

Date Manipulation

P: 37
Is there a way of getting the previous date when we enter a specific date?

e.g if we enter ... 20090804, it should return 20090803, or even if we enter a future date...20090910, it should return 20090909.
Aug 4 '09 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
@joeferns79
You can use one of the Date modules listed on CPAN or you can write your own code to split the date into tokens (year, month, day) and feed them to Time::Local ( that comes with perl) to convert them into epoch seconds and subtract 24 hours of seconds from the epoch time.
Proof of concept:

Expand|Select|Wrap|Line Numbers
  1. use Time::Local;
  2.  
  3. my $date = '20090910';
  4. my ($y,$m,$d) = unpack("A4A2A2",$date);
  5. my $epoch = timelocal(0,0,0, $d, $m-1, $y) - (60*60*24);
  6. print scalar localtime($epoch);
  7.  
assumes the date is always in YYYYMMDD format, if not you will have to change unpack() to a regexp and do some checking. You can use POSIX to reformat $epoch back into YYYYMMDD. I leave that part up to you. Or as mentioned, look into a Date module.
Aug 4 '09 #2

P: 37
Thanks a lot, that worked.
Aug 5 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.