Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Julian Date Calculator from YYYY-MM-DD

Status
Not open for further replies.

Ramnarayan

Programmer
Jan 15, 2003
56
0
0
US
Hi,

I am trying to write a script to calculate the Julian date of the year from the given string which is of the format "YYYY-MM-DD". Can someone help me out here. I am at a lost after going through all the various Date::Calc modules and nothing seems to work for me. Can someone be kind enough to help me out in this!

Thanks very much
 
I too failed to find a Perl solution but I got round the problem by populating a MySQL database with all the date details for the previous 5 years and the next 14 years.
Using MySQL queries allows any date calculations to be carried out.
A flat file would also work but would require more processing.
Just a suggestion which works for me.

Keith
 
I have already seen this. I think I did not pose the correct question. It should have been the other way round. I.e. to calculate the date from the julian date!

So if the julian date is 13, it should prompt me the date corresponding to this julian date i.e. 2006-01-13.

The perl sub gives the opposite. If you know how to reverse this I will be grateful for that!
 
Seems to be some confusion here. "Julian Date" has several radically different meanings, according to . Do you mean you want the yyyy-mm-dd corresponding to a specified nth day of the year? If so, you could use something like:
[tt]
$year=2006;
$juliandate=50;

use POSIX;
$jan1=POSIX::mktime(0,0,0,1,0,$year-1900);
$dt=$jan1+$juliandate*86400-1;
@ymd=localtime($dt);
$ymd=sprintf("%d-%02d-%02d",1900+$ymd[5],$ymd[4]+1,$ymd[3]);
print "$ymd\n";
[/tt]
 
Sorry, that should have been:
[tt]
$dt=$jan1+($juliandate-1)*86400;
[/tt]
 
Tony, might not be a bad idea to retrofit that into Date::Manip, with the authors permission of course ;)

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hey Tony,

Thanks for the tip. However when I execute your statement, It is returning $ymd as 2006-02-19!. Is there some numbers to be changed inorder to get the correct julian date.

Yes I mean the julian day for the year. Hence for today, (2006-01-16), the julian day is 16.
 
all arrays should be 0-based, so that should be accounting for at least two of the missing $days--, the other one ... $ymd[4]+1, dunno, but that could be it ...

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
This is roughly the same as Tony's code except shoved into a sub. I tried to avoid changing the date values around except where necessary.
Code:
print "16th Day: ", DOY_to_YMD(16), "\n";

sub DOY_to_YMD {
    use POSIX;
    my $DoY = shift; # Day of Year
    my ($day, $month, $year) = (localtime)[3..5];
    my $jan1 = POSIX::mktime(0,0,0,0,0,$year);  # Actually Dec 31 of prev year
    my @time = localtime($jan1 + $DoY * 86400);
    return sprintf "%4d-%02d-%02d", $time[5]+1900, $time[4]+1, $time[3];
}
 
Ramnarayan:

The test date used in my code was day 50 of 2006, which is 2006-02-19. You would obviously use your own data instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top