You are accessing this site in a read-only mode. For full access to all member benefits, including message posting, please login or register. Registration is completely free, simple, and takes only a few seconds.
Login |
Free VBMonster.com registration |
Whole discussion thread
The message you are replying to and its parents are listed in the reverse order with the most recent posts first. This might not be the whole discussion thread. To read all the messages in this thread please click here.
Re: Return Month/Day from Day Number
| Rick Rothstein | 27 Jul 2009 02:23 |
> That won't work. 2000 was not a leap year. Yes it was.
 Signature Rick (MVP - Excel)
|
| James Hahn | 27 Jul 2009 01:21 |
That won't work. 2000 was not a leap year.
>> Because the purpose of function is to cover EVERY SINGLE DAY >> possible, whether or not that day exists for the current year. Note [quoted text clipped - 6 lines] > the result. There's no point, and no benefit, in making it any more > complicated. |
| Jim Mack | 26 Jul 2009 23:22 |
> Because the purpose of function is to cover EVERY SINGLE DAY > possible, whether or not that day exists for the current year. Note > that it returns Month/Day and not year as it is year neutral. There > is really no point in getting too deep into details why. Just trust > that it is important. :-) Then the best answer is the one you already noted: DateAdd the serial day number to a base date of 1999-12-31, and discard the year part of the result. There's no point, and no benefit, in making it any more complicated.
 Signature Jim
|
| Webbiz | 26 Jul 2009 20:42 |
>... >> Since the purpose of my function is to return a date based on a Day [quoted text clipped - 9 lines] >course, it's also a reasonable implementation w/ the addition of a >LeapYear logical input, too...) Because the purpose of function is to cover EVERY SINGLE DAY possible, whether or not that day exists for the current year. Note that it returns Month/Day and not year as it is year neutral. There is really no point in getting too deep into details why. Just trust that it is important. :-)
I don't understand the 'lookup' table. You mean to fill-up a complete table (like an array) that contains every single day, perhaps in strings? January 1, January 2, January 3...
The the DayNum would index this array (lookup table)?
Would you do it this way rather than adding the Day Num to December 31, 1999?
Thanks.
Webbiz
|
| dpb | 26 Jul 2009 02:54 |
...
> Since the purpose of my function is to return a date based on a Day > Number as if the year is a leap year (1 to 366), one can't just use > any date reference for that to happen, right? ...
OK, I'll bite...for what earthly reason would you not consider the actual number of days/year?
But, if you're adamant of it always being 366, then sure the lookup table based on accumulated days is as the logical implementation. (Of course, it's also a reasonable implementation w/ the addition of a LeapYear logical input, too...)
--
|
| Webbiz | 26 Jul 2009 02:35 |
>> Could someone help me with this? >> [quoted text clipped - 74 lines] >function would give you a bit of flexibility so that you could use it in >multiple applications. Hello Mike-
Since the purpose of my function is to return a date based on a Day Number as if the year is a leap year (1 to 366), one can't just use any date reference for that to happen, right?
So the routine MUST use the date of the last day just prior to a leap year so that by adding 1 to 366, you get January 1 to December 31st, right?
On another note, wouldn't putting DateAdd inside Format just be adding more overhead? Since I'm using an actual Date type and pre-loading it with #December 31, 1999#, using simple addition of 1 to 366 can easily be done without any conversion (such as Datevalue, etc.) and really no need for DateAdd.
I completely agree with the formatting issue in as much as limiting the re-usuability of the function. However, this particular routine resides in a class so it's contained and single-minded. It does one thing. It takes a number from 1 to 366 and returns the verbage of the month and day, such as "March 14", etc. It's for display purposes as one moves the mouse across a picturebox.
Thanks for your comments.
Webbiz
|
| MikeD | 26 Jul 2009 02:19 |
> Could someone help me with this? > [quoted text clipped - 30 lines] > > Any thoughts? I know I would definately use DateAdd:
Print DateAdd("d",DayNum,#12/31/1999#)
Always better to use date functions than to rely on mathematically adding and subtracting. If you want the date formatted a certain way, then nest that inside a Format function. Of course, you must have a date to base this on or devise a default date to use. For example, a logical choice for a default would be 12/31 of last year...and you could write that as such:
Print DateAdd("d", DayNum, DateSerial(Year(Date) - 1, 12, 31))
(assuming the PC's clock is correct...at least the year)
I don't know if I'd write this into a wrapper function (your GetMMDDFromDayNum). I suppose it depends on how many times I'd need to call it. If only once or twice, I doubt that I would. If a dozen times, probably. If I WERE going to write a wrapper function, I think I'd make the date an optional parameter (mind you, I'm pretty much just winging this). Something like this:
Public Function GetDateFromDayNum(ByVal DayNum As Long, Optional RefDate As Variant) As Date
If Not IsDate(RefDate) Then GetDateFromDayNum = DateAdd("d", DayNum, DateSerial(Year(Date) - 1, 12, 31)) Else GetDateFromDayNum = DateAdd("d", DayNum, RefDate) End If
End Function
(a Variant, in this case, is beneficial)
I would NOT have the function return a specifically formatted string as in your example because that would limit the function's usefulness (it wouldn't be generic anymore). Better to have it return a Date data type and let the calling procedure format it as appropriate.
I'm sure others will have ideas that may be better, but I think this function would give you a bit of flexibility so that you could use it in multiple applications.
-- Mike
|
| Webbiz | 25 Jul 2009 16:47 |
Could someone help me with this?
How might one convert a day number from 1 to 366 into a Month and Day string?
Most years are 365, I know. But the assumption will be that each year contains 366 (is a leap) by this function.
Day Num as Return Values:
1 = January 1 60 = February 29 366 = December 31
The above must always be true.
Now, I was thinking about doing something like this. Maybe this is the best way to go, maybe not. If you have a better idea, please let me know.
What I thought I'd do in my GetMMDDFromDayNum function is this:
------------------------ Function GetMMDDFromDayNum(ByVal DayNum as long) as String Dim dLeapYearRef as Date
'Ref date is leap year minus 1 day dLeapYearRef = #12/31/1999#
GetMMDDFromDayNum = Format(dLeapYearRef + DayNum, "MMMM dd")
End Function
Any thoughts?
Thanks.
Webbiz
|
Quick links:
|
|
|