[Gambas-user] extracting a fractional year from a date

Kevin Fishburne kevinfishburne at ...1887...
Wed Nov 6 03:34:02 CET 2013


On 11/05/2013 09:16 PM, Benoît Minisini wrote:
> Le 06/11/2013 03:04, Kevin Fishburne a écrit :
>> If you have a Date, is there a reasonably simple way to extract which
>> fraction of the year it is? For example a date of 12:00 am on January
>> 1st would return zero and 11:49 pm on December 31st would return one.
>> The stuff I'm trying now using the Month and Day functions
>> seems...wrong. If there's no obvious way to do this I'll keep hacking at it.
>>
> The problem is that a year can have 365 or 366 days.
>
> If you don't care, do:
>
> (MyDate - Date(Year(MyDate), 1, 1)) /
> (Date(Year(MyDate)+1,1,1) - Date(Year(MyDate), 1, 1))
>

What I'm doing now is this:

Print (Month(DateCurrent - System.TimeZone / 86400) - 1) / 12 + 
(Day(DateCurrent - System.TimeZone / 86400) - 1) / 31 / 12 + 
(Hour(DateCurrent - System.TimeZone / 86400) - 0) / 24 / 31 / 12

I -think- it works, but seems inaccurate and generally ass-backward.

Your code adapted for my project looks like this:

Print ((DateCurrent - System.TimeZone / 86400) - Date(Year((DateCurrent 
- System.TimeZone / 86400)), 1, 1)) / (Date(Year((DateCurrent - 
System.TimeZone / 86400)) + 1, 1, 1) - Date(Year((DateCurrent - 
System.TimeZone / 86400)), 1, 1))

and also seems to work well, though with greater precision (my 
resolution stops at the hour).

I don't need to be precise but the output needs to be smooth without 
jumping backward significantly or returning negative values. Other than 
DST (which I hope I negated by subtracting the client timezone) would 
your algorithm create any noticeable hiccups during leap years, etc.? 
The output of this calculation will be used to set the colors of tree 
foliage as leaves die in autumn as well as the low and high temperature 
ranges for any given day. I'll use your algorithm for the time being as 
it seems pretty slick. Thanks. :)

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list