Date Function

They have: 19 posts

Joined: Oct 2000

Hi,

I still haven't resolved the basic authentication issue but have another problem.

I have one date eg: 10/3/00
I have an amount of days eg: 422

I need to be able to calculate the expiry date (date + days) and print it to screen in the above format ie: 15/5/01

Any ideas?

Denise Smiling

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Denise,

Don't really remember the exact syntax, but in javascript something like:

var myMSec = myDate.getMilliseconds(); // myDate is 10/3/00 in date format
var myDaysinMSec = numOfDays * (60 * 60 * 24 * 1000)
var totalMsecs = myMSec + myDaysinMSec;
var newDate = setTime(totalMsecs);

I'll check later tonight, but if someone knows for certain, please slap me over the head and correct the code. Thanks
Vinny

Where the world once stood
the blades of grass cut me still

They have: 19 posts

Joined: Oct 2000

Hi Vincent,

Thanks for the tip but I eventually worked out a Perl sub routine. The only thing I'm stuck on now is displaying the date correctly. Well, I can bluff a way around it but was wondering whether anyone knew the easiest/proper way of displaying integers in two figures. ie: 1 to "01" and 2 to "02"?

Denise Smiling

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Denise,

Do you mean something like:

if (x < 10) print("0",x)

BTW: my perl is shaky, so I'm hoping Ken will correct the syntax if it's wrong Smiling

vinny

Where the world once stood
the blades of grass cut me still

They have: 850 posts

Joined: Jul 1999

I don't know of a 'proper' way to do it. But this would work:

$num="0$num",if(length($v)==1);
'

They have: 19 posts

Joined: Oct 2000

Thanks all,

I had:

if($day < 10) {$day = "0$day";}
if($month < 10) {$day = "0$month";}

So I think we're all in the same ball park!

Denise Smiling

They have: 161 posts

Joined: Dec 1999

If you want to do simple string formatting, use the sprintf() and printf() functions (borrowed from C, of course):

($day,$mon,$year) = (3,4,1982);
$date = sprintf "%02d/%02d/%4d", $mon, $day, $year;
print $date;  # 04/03/1982
'

Read perldoc -f sprintf and perldoc -f printf for information on those functions.

For date calculations, you should be safe in using the Time::Local module (it comes with Perl):

use Time::Local;
$now = '10/17/2000';
$plus_days = 500;
$future = add_days($now,$plus_days);

sub add_days {
  my ($date,$days) = @_;
  my ($m,$d,$y) = split '/', $date;
  my $time;

  $m--, $y -= 1900;  # $m is 9, $y is 100
  $time = timelocal(0,0,0,$d,$m,$y);
  $time += 86400 * $days;
  ($d,$m,$y) = (localtime($time))[3,4,5];
  return sprintf "%02d/%02d/%4d", $m+1, $d, $y+1900;
}
'

If you're doing more complex date computation, I highly suggest looking into the Date::Calc module, available on CPAN.

[Edited by japhy on 10-17-2000 at 05:21 PM]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.