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
| Larry Serflaten | 26 Jul 2009 23:19 |
> > I didn't see an advantage to using a function (DateAdd) within another > > function (Format) > > making the code more self-documenting, easier to read and understand, and > therefore easier to maintain Yet another way to skin that cat....
Function DOY(ByVal DayOfYear As Long) As String DOY = Format$(DateSerial(1999, 12, 31 + Num), "mmmm dd") End Function
LFS
|
| Bob Butler | 26 Jul 2009 23:04 |
<cut>
> I didn't see an advantage to using a function (DateAdd) within another > function (Format) making the code more self-documenting, easier to read and understand, and therefore easier to maintain
|
| Webbiz | 26 Jul 2009 20:48 |
>> 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 [quoted text clipped - 19 lines] > >LFS Okay! Now we're talking.
I get the conversion part, and your example does remove that one step. I didn't see an advantage to using a function (DateAdd) within another function (Format) within my main procedure function as a step up to what I was doing. As you imply, both produced conversions. Yours apparently does not as you have already provided the converted form of the date and a value. I like it.
This is why I ask these kinds of questions!
Thanks Larry.
Webbiz
|
| Larry Serflaten | 26 Jul 2009 03:29 |
> 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. Au contrare.
The Date and Long are two different data types. One must be coersed into the other before they can be added. I'd guess the Long get converted to a Date type (a widening operation) in the process.
Even if you don't include a conversion, the VB compiler will put one in there.
Since you pass in a Long, you could do the addition as Longs, and then the formatting:
Function GetMMDDFromDayNum(ByVal DayNum As Long) As String 'Ref date is #12/31/1999# GetMMDDFromDayNum = Format$(36525 + DayNum, "MMMM dd") End Function
LFS
|
| 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:
|
|
|