Qbasicnews.com

Full Version: day/month to date converter...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Anyone have a day/month to date converter? :-)

e.g.: X day of Y month = N date of the year.
Not the most straightforward way, but no one answered it....


DOY(y,m,d)=julian(y,m,d)-julian(y,1,1)+1

and julian date is:

Code:
Function tojulian&(y,M,D)
  'From Egbert Zijlema  ABC packets 1996
  temp& = (M  - 14) \ 12
  JulPart& = D  - 32075 + (1461 * (Y  + 4800 + temp&) \ 4)
  JulPart& = JulPart& + (367 * (M  - 2 - temp& * 12) \ 12)
  tojulian& = JulPart& - (3 * ((Y  + 4900 + temp&) \ 100) \ 4)
end function
With a little patience you could rework it and remove everything that cancels in the substraction and get a simpler formula.

y=year ,m=month number, d=day of month

Hope it helps!
pseudo-code:
Code:
function getDayOfYear( day, month )

    int[] days_elapsed_per_month = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
    return day + days_elapsed_per_month[ month ];

end function

This has the assumption that February always has 29 days, which is necessary for days of different years to match up properly (e.g. the 10th of March will always be day 90, regardless of whether it's a leap year or not).
Running that code you supplied returns a value which seemingly makes no sense at all...

Code:
declare function tojulian&(y,M,D)


print tojulian&(2006,09,29)

sleep
end

Function tojulian&(y,M,D)
  'From Egbert Zijlema  ABC packets 1996
  temp& = (M  - 14) \ 12
  JulPart& = D  - 32075 + (1461 * (Y  + 4800 + temp&) \ 4)
  JulPart& = JulPart& + (367 * (M  - 2 - temp& * 12) \ 12)
  tojulian& = JulPart& - (3 * ((Y  + 4900 + temp&) \ 100) \ 4)
end function

...and the result:
Quote:2454008

Can you explain this to me please? I am working on an answer to this solution, too, Agamenmus. Although being an experienced coder, you'd probably know how to do it anyway. But I could be wrong. Tongue

EDIT:
http://www.qbasicnetwork.com/stuff/yeardate.bi

Code:
'#include: "yeardate.bi"

converttodate(2000,1,1)

Enjoy.
C# has all that functionality built in :p Check the msdn.

VB.net should have it as well cosidering it is comptaible with all C#'s functions...
Anarky:
Yes , today's julian date is 2454008, as you can check here http://aa.usno.navy.mil/data/docs/JulianDate.html

Julian date is a "date serial" of the same kind VB or FB uses. The only difference is the position of the origin. VB's 0 is 0hAM of December 30,1899, while julian date starts ar noon of January 1, 4713 BC (don't ask me why, it's used for astronomical calculations ).

The idea was to substract today's serial from january 1's serial. Obviously in VB or FB you could use the built-in date serial.
Code:
#include "vbcompat.bi"
doy=dateserial(y,m,d)-dateserial(y,1,1)+1
Ah, I see...

I'm working on another function to go with this program. It will be able to count the number of days passed given any two dates from any two years assuming the calendar is current the whole time.

I wonder if this could be included in FB's library package?
Code:
declare function tojulian&(y,M,D)


later = tojulian&(2006,09,30)
earlier = tojulian&(1981,08,06)

totaldays = later - earlier
print totaldays
sleep
end

Function tojulian&(y,M,D)
  temp& = (M  - 14) \ 12
  JulPart& = D  - 32075 + (1461 * (Y  + 4800 + temp&) \ 4)
  JulPart& = JulPart& + (367 * (M  - 2 - temp& * 12) \ 12)
  tojulian& = JulPart& - (3 * ((Y  + 4900 + temp&) \ 100) \ 4)
end function

So simple... and here I was trying to make a 200 line long routine to do this...
In FB you can use DateDiff
Mrmm.. I used FB's date functions after writing my own.
Pages: 1 2