Offsetting dates from the past in Date::Manip

P

pwaring

I'm trying to use Date::Manip to resolve strings which represent dates
according to a known reference point - e.g. given a known date, what
does "Friday" represent?. If I do something like this:

my $date = ParseDate("Friday");
print $date;

I get the result I expect, i.e. 13/06/08 (assuming I changed the
format to DD/MM/YY). However, the date used for as the reference point
is always the current date, and I want to be able to put in an
arbitrary date and have the reference resolved from there. For
example, if I use this time last year, "Friday" should be resolved to
a different date - 15/06/07. Does anyone know if it is possible to do
this within Date::Manip? I've tried using DateCalc, but this gives me
an offset from the current date, which isn't really what I want.

Thanks in advance,

Paul

P.S. I'm only dealing with recent dates (1990-present), so there
shouldn't be any problems with pre-1900 dates etc.
 
G

Grehom

I'm trying to use Date::Manip to resolve strings which represent dates
according to a known reference point - e.g. given a known date, what
does "Friday" represent?. If I do something like this:

my $date = ParseDate("Friday");
print $date;

I get the result I expect, i.e. 13/06/08 (assuming I changed the
format to DD/MM/YY). However, the date used for as the reference point
is always the current date, and I want to be able to put in an
arbitrary date and have the reference resolved from there. For
example, if I use this time last year, "Friday" should be resolved to
a different date - 15/06/07. Does anyone know if it is possible to do
this within Date::Manip? I've tried using DateCalc, but this gives me
an offset from the current date, which isn't really what I want.

Thanks in advance,

Paul

P.S. I'm only dealing with recent dates (1990-present), so there
shouldn't be any problems with pre-1900 dates etc.

yes, I know you were asking about Date::Manip, but a solution using
Date::Calc is:

#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw ( :all );
my @today = Today();
my @year_ago = Add_Delta_YM( Today(), -1, 0 );
my $that_day = Day_of_Week(@year_ago[0..2]);
my $day_reference = 5; # Friday is day 5 of week
my $days_until_reference = ($day_reference - $that_day + 7) % 7;
my @friday_a_year_ago = Add_Delta_YMD( @year_ago, 0, 0,
$days_until_reference);

print Date_to_Text( @friday_a_year_ago[0..2] );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top