Home All Groups Group Topic Archive Search About

Newbie: How to extract year from a Date object

Author
20 Sep 2005 2:55 PM
Newbie
Hi,

I have (had) the following problem which i worked around but i would still
like to find an answer.

if myDate is e.g. : #02/04/1998# how do i extract the year ?

Year(myDate) is only for VBA.
myDate.Year doesnt exist.

Any suggestions ?

Thanx in advance !
-steve

Author
20 Sep 2005 2:59 PM
Larry Lard
Newbie wrote:
> Hi,
>
> I have (had) the following problem which i worked around but i would still
> like to find an answer.
>
> if myDate is e.g. : #02/04/1998# how do i extract the year ?
>
> Year(myDate) is only for VBA.

Not sure what you mean by 'only for VBA', since

?Year(Now)

seems to work fine in VB6's Immediate window.

--
Larry Lard
Replies to group please
Author
20 Sep 2005 3:10 PM
Mike Williams
"Newbie" <st***@here.com> wrote in message news:GpVXe.812$hW.755@tor-nn1...

> Year(myDate) is only for VBA.
> myDate.Year doesnt exist.

The Year function works fine in VBA, VB5 and VB6 on my system. Try:

MsgBox Year(Now)

Which version of VB are you using?

Mike
Author
20 Sep 2005 4:07 PM
Jeff Johnson [MVP: VB]
"Newbie" <st***@here.com> wrote in message news:GpVXe.812$hW.755@tor-nn1...

> I have (had) the following problem which i worked around but i would still
> like to find an answer.
>
> if myDate is e.g. : #02/04/1998# how do i extract the year ?
>
> Year(myDate) is only for VBA.

Since when?
Author
20 Sep 2005 5:09 PM
Someone
I found that VB6 does not prevent you from using Year as a variable, and
hence, some developers think it doesn't work.


Show quoteHide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:%23lSce1fvFHA.3764@TK2MSFTNGP09.phx.gbl...
>
> "Newbie" <st***@here.com> wrote in message
> news:GpVXe.812$hW.755@tor-nn1...
>
>> I have (had) the following problem which i worked around but i would
>> still like to find an answer.
>>
>> if myDate is e.g. : #02/04/1998# how do i extract the year ?
>>
>> Year(myDate) is only for VBA.
>
> Since when?
>
Author
20 Sep 2005 5:36 PM
Someone
I just found out that if you use "Year" as an item in Enum, it will not
work. It would give you a compile time error.

Example:

Private Enum enumDates
    Year
    Month
    Day
End Enum

This will override Year(), Month(), and Day() functions since they are part
of VBA library, which is part of VB6.

To avoid these problems, I always prefix Enum elements with "enm" or "enum".
Most likely I would write it like this:

Private Enum enumDates
    enmDateYear
    enmDateMonth
    enmDateDay
End Enum



"Someone" <nob***@cox.net> wrote in message
news:pnXXe.44109$ct5.2692@fed1read04...
Show quoteHide quote
>I found that VB6 does not prevent you from using Year as a variable, and
>hence, some developers think it doesn't work.
>
>
> "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
> news:%23lSce1fvFHA.3764@TK2MSFTNGP09.phx.gbl...
>>
>> "Newbie" <st***@here.com> wrote in message
>> news:GpVXe.812$hW.755@tor-nn1...
>>
>>> I have (had) the following problem which i worked around but i would
>>> still like to find an answer.
>>>
>>> if myDate is e.g. : #02/04/1998# how do i extract the year ?
>>>
>>> Year(myDate) is only for VBA.
>>
>> Since when?
>>
>
>
Author
20 Sep 2005 5:18 PM
PC
you can use year,month,day in an enum
if you use the full syntax for the vba functions
example:
Private Enum enumDates
    Year
    Month
    Day
End Enum

Private Sub Command1_Click()
    Print VBA.Year(Now), VBA.Month(Now), VBA.Day(Now)
    Print Year, Month, Day
End Sub

"Someone" <nob***@cox.net> wrote in message
news:eMXXe.44114$ct5.9164@fed1read04...
Show quoteHide quote
> I just found out that if you use "Year" as an item in Enum, it will not
> work. It would give you a compile time error.
>
> Example:
>
> Private Enum enumDates
>     Year
>     Month
>     Day
> End Enum
>
> This will override Year(), Month(), and Day() functions since they are
part
> of VBA library, which is part of VB6.
>
> To avoid these problems, I always prefix Enum elements with "enm" or
"enum".
> Most likely I would write it like this:
>
> Private Enum enumDates
>     enmDateYear
>     enmDateMonth
>     enmDateDay
> End Enum
>
Author
20 Sep 2005 5:51 PM
Jeff Johnson [MVP: VB]
"Someone" <nob***@cox.net> wrote in message
news:pnXXe.44109$ct5.2692@fed1read04...

>I found that VB6 does not prevent you from using Year as a variable, and
>hence, some developers think it doesn't work.

It doesn't prevent you from using any function name as a variable. One
regular around here posted some code a while back with a variable named
"str" which he said he used all the time as a throwaway name. We were
surprised that it didn't conflict with the Str() function, and he was
surprised to learn that the Str() function existed. Gotta love VB....
Author
22 Sep 2005 12:46 PM
Ralph
Show quote Hide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:uTRXeQhvFHA.2932@TK2MSFTNGP10.phx.gbl...
>
> "Someone" <nob***@cox.net> wrote in message
> news:pnXXe.44109$ct5.2692@fed1read04...
>
> >I found that VB6 does not prevent you from using Year as a variable, and
> >hence, some developers think it doesn't work.
>
> It doesn't prevent you from using any function name as a variable. One
> regular around here posted some code a while back with a variable named
> "str" which he said he used all the time as a throwaway name. We were
> surprised that it didn't conflict with the Str() function, and he was
> surprised to learn that the Str() function existed. Gotta love VB....
>
>

My favorite "shoot oneself in the foot"...

I created a Class mimicking a data structure and one of the members was
"ID". So I created a quite logically named member variable - "mID".
I am embarrassed to report that there several hours of grief before I
recognized what the problem was. <g>

-ralph
Author
22 Sep 2005 6:31 PM
Jeff Johnson [MVP: VB]
Show quote Hide quote
"Ralph" <nt_consultin***@yahoo.com> wrote in message
news:HYKdndx_p8bjOq_eRVn-gA@arkansas.net...

>> It doesn't prevent you from using any function name as a variable. One
>> regular around here posted some code a while back with a variable named
>> "str" which he said he used all the time as a throwaway name. We were
>> surprised that it didn't conflict with the Str() function, and he was
>> surprised to learn that the Str() function existed. Gotta love VB....
>>
>>
>
> My favorite "shoot oneself in the foot"...
>
> I created a Class mimicking a data structure and one of the members was
> "ID". So I created a quite logically named member variable - "mID".
> I am embarrassed to report that there several hours of grief before I
> recognized what the problem was. <g>

And the IDE never changed it to Mid?
Author
22 Sep 2005 7:45 PM
Ralph
Show quote Hide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:%23XRKsP6vFHA.3312@TK2MSFTNGP09.phx.gbl...
>
> "Ralph" <nt_consultin***@yahoo.com> wrote in message
> news:HYKdndx_p8bjOq_eRVn-gA@arkansas.net...
>
> >> It doesn't prevent you from using any function name as a variable. One
> >> regular around here posted some code a while back with a variable named
> >> "str" which he said he used all the time as a throwaway name. We were
> >> surprised that it didn't conflict with the Str() function, and he was
> >> surprised to learn that the Str() function existed. Gotta love VB....
> >>
> >>
> >
> > My favorite "shoot oneself in the foot"...
> >
> > I created a Class mimicking a data structure and one of the members was
> > "ID". So I created a quite logically named member variable - "mID".
> > I am embarrassed to report that there several hours of grief before I
> > recognized what the problem was. <g>
>
> And the IDE never changed it to Mid?
>

Never. Well, never so much that I noticed it.

I was so dense at the time, that when I got a peculiar error, I was always
able to "get around" it. I was using a class generator.

I can't remember the exact final situation, but I think it was while using
it as a parameter. Error would be something like wrong parameters, etc. I
kept chewing on the original call. <g>

I hit the floor laughing, when it finally dawned on me.

-ralph
Author
27 Sep 2005 11:33 AM
DRBarkley
Show quote Hide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:%23XRKsP6vFHA.3312@TK2MSFTNGP09.phx.gbl...
>
> "Ralph" <nt_consultin***@yahoo.com> wrote in message
> news:HYKdndx_p8bjOq_eRVn-gA@arkansas.net...
>
> >> It doesn't prevent you from using any function name as a variable. One
> >> regular around here posted some code a while back with a variable named
> >> "str" which he said he used all the time as a throwaway name. We were
> >> surprised that it didn't conflict with the Str() function, and he was
> >> surprised to learn that the Str() function existed. Gotta love VB....
> >>
> >>
> >
> > My favorite "shoot oneself in the foot"...
> >
> > I created a Class mimicking a data structure and one of the members was
> > "ID". So I created a quite logically named member variable - "mID".
> > I am embarrassed to report that there several hours of grief before I
> > recognized what the problem was. <g>
>
> And the IDE never changed it to Mid?
>
>

Jeff,

When I read this the other day, it rang a bell that I had just added mID to
one of my classes.
I just got a chance to investigate it, and I had to share:

When I searched for "mID", I found that all of my Mid() functions had been
"renamed" to mID.
Typing over them didn't help, either; they would just revert to from Mid()
back to mID().
Interestingly, there were no errors, and when I right-clicked mID, and
selected "Definition", it went to the Dim'd one.

In an attempt to fix it, I renamed my member variable (I used m_ID), but the
mID() held its caseness (made that up myself).
Even after saving, exiting, and restarting VB, it persisted!

To fix it, I Dim'd a variable with the correct case, saved it, deleted the
"Dim Mid..." line, and re-saved it.

Fun, fun,

DRBarkley
Author
23 Sep 2005 8:31 AM
J French
On Thu, 22 Sep 2005 07:46:06 -0500, "Ralph"
<nt_consultin***@yahoo.com> wrote:

<snip>

>My favorite "shoot oneself in the foot"...
>
>I created a Class mimicking a data structure and one of the members was
>"ID". So I created a quite logically named member variable - "mID".
>I am embarrassed to report that there several hours of grief before I
>recognized what the problem was. <g>

Nice One !

You outscoped Mid - how comical

I avoid that sort of problem by using UDTs

  Private Type TM
        ID As String
        OtherProp AS OLE_COLOR
  End Type

  Private m As TM
Author
23 Sep 2005 2:09 PM
Jeff Johnson [MVP: VB]
Show quote Hide quote
"J French" <erew***@nowhere.uk> wrote in message
news:4333bc69.86474918@news.btopenworld.com...

>>My favorite "shoot oneself in the foot"...
>>
>>I created a Class mimicking a data structure and one of the members was
>>"ID". So I created a quite logically named member variable - "mID".
>>I am embarrassed to report that there several hours of grief before I
>>recognized what the problem was. <g>
>
> Nice One !
>
> You outscoped Mid - how comical
>
> I avoid that sort of problem by using UDTs

I avoid it by using the underscore with my scope identifiers:

    m_ID vs. mID

Other than constants, it's my only use of underscores in variable names.
(But then constant isn't a variable, now is it? You know what I mean....)
Author
23 Sep 2005 5:10 PM
Ralph
Show quote Hide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:OaxcshEwFHA.2864@TK2MSFTNGP10.phx.gbl...
>
> "J French" <erew***@nowhere.uk> wrote in message
> news:4333bc69.86474918@news.btopenworld.com...
>
> >>My favorite "shoot oneself in the foot"...
> >>
> >>I created a Class mimicking a data structure and one of the members was
> >>"ID". So I created a quite logically named member variable - "mID".
> >>I am embarrassed to report that there several hours of grief before I
> >>recognized what the problem was. <g>
> >
> > Nice One !
> >
> > You outscoped Mid - how comical
> >
> > I avoid that sort of problem by using UDTs
>
> I avoid it by using the underscore with my scope identifiers:
>
>     m_ID vs. mID
>
> Other than constants, it's my only use of underscores in variable names.
> (But then constant isn't a variable, now is it? You know what I mean....)
>

Not that I or anyone else around here have blind personal preferences. <g>

I avoided "warts" (m_xxx). I, who abuse Hungarian outragiously, just thought
they looked ugly. Even going so far as calling it toad-programming. However,
since having been bitten several times in the proverbial location, I have
learned to appreciate their usefulness. <g>

-ralph
Author
24 Sep 2005 9:55 AM
J French
On Fri, 23 Sep 2005 12:10:04 -0500, "Ralph"
<nt_consultin***@yahoo.com> wrote:

<snip>

>Not that I or anyone else around here have blind personal preferences. <g>

>I avoided "warts" (m_xxx). I, who abuse Hungarian outragiously, just thought
>they looked ugly. Even going so far as calling it toad-programming. However,
>since having been bitten several times in the proverbial location, I have
>learned to appreciate their usefulness. <g>

Except for Objects (especially visual ones)  I really dislike
Hungarian notation

One can spend ages looking at the prefix wondering
    'What the heck does that lot mean ?'

- acbogJerry  ( a virtual beer to anyone who can suss that out )
Author
24 Sep 2005 2:36 PM
Ralph
"J French" <erew***@nowhere.uk> wrote in message
news:43352102.177778265@news.btopenworld.com...
> On Fri, 23 Sep 2005 12:10:04 -0500, "Ralph"
> <nt_consultin***@yahoo.com> wrote:
>
> <snip>
>
> >Not that I or anyone else around here have blind personal preferences.
<g>
Show quoteHide quote
>
> >I avoided "warts" (m_xxx). I, who abuse Hungarian outragiously, just
thought
> >they looked ugly. Even going so far as calling it toad-programming.
However,
> >since having been bitten several times in the proverbial location, I have
> >learned to appreciate their usefulness. <g>
>
> Except for Objects (especially visual ones)  I really dislike
> Hungarian notation
>
> One can spend ages looking at the prefix wondering
>     'What the heck does that lot mean ?'
>
> - acbogJerry  ( a virtual beer to anyone who can suss that out )
>

I love Hungarian notation. I adopted it early using a home-grown version in
my pre-ANSI C days. It has a proven track record in assisting programmers to
write readable and maintainable code.

Like all standards it depends on one to actually use the 'standard' - I
suspect that you have either combined several conventions (MS, ADO,
Leszynski, Hungarian, ...) or it is malformed. (My guess - you meant to type
"gacboJerry".)

In any case a variable 'name' is always meaningless when removed from the
context in which it was used. Whether 'English' or 'Hungarian'.

-ralph
(8 lines, poor)
Author
25 Sep 2005 10:17 AM
J French
On Sat, 24 Sep 2005 09:36:56 -0500, "Ralph"
<nt_consultin***@yahoo.com> wrote:
<snip>

>I love Hungarian notation. I adopted it early using a home-grown version in
>my pre-ANSI C days. It has a proven track record in assisting programmers to
>write readable and maintainable code.

>Like all standards it depends on one to actually use the 'standard' - I
>suspect that you have either combined several conventions (MS, ADO,
>Leszynski, Hungarian, ...) or it is malformed. (My guess - you meant to type
>"gacboJerry".)

I first really ran into it when one part of our shop was migration to
C in order to use OS/2

The problem was that their system used (and needed) a fair variety of
complicated types, the Hungarian just became illegible.

>In any case a variable 'name' is always meaningless when removed from the
>context in which it was used. Whether 'English' or 'Hungarian'.

Not necessarily, with a little common sense one can establish
meaningful names that are still 'meaningful' out of 'context'

For example:
     InRoutineFlag   is a Boolian preventing recursion

     By convention Flag is always Boolean
     (even before Boolean existed)

Another problem with using Hungarian for simple variables is that one
may wish to change a data type from say, Integer to Long, so one has
to go through changing : iCount to lCount

If one is sensible then one only gives 'names' to things with a life
longer than a dragon fly :
        S is a much easier thing to recognize than sTemp

By my convention 'S' /is/ 'temporary' so sTemp is repetitious

Similarly  P  is a dragonfly pointer but pStart is significant

IMO a smattering of Hungarian is useful, but excessive use of it makes
one forget 'Plain English'
Author
25 Sep 2005 12:56 PM
Bob Butler
"J French" <erew***@nowhere.uk> wrote in message
news:43367359.4090163@news.btopenworld.com
> Not necessarily, with a little common sense one can establish
> meaningful names that are still 'meaningful' out of 'context'

To a degree, yes.  "CustomerName" is probably a string while
'TotalAmountDue' is probably currency; at the very least you know it's
probably numeric.

> For example:
>      InRoutineFlag   is a Boolian preventing recursion
>
>      By convention Flag is always Boolean
>      (even before Boolean existed)

by your convention; by the convention many use possibly.  by the conventions
that I use 'flag' would indicate a numeric variable that can take on any of
several states

> Another problem with using Hungarian for simple variables is that one
> may wish to change a data type from say, Integer to Long, so one has
> to go through changing : iCount to lCount

True enough although I can't remember a case where I've needed to do that.
It's probably come up but with a global search and replace it's not like it
takes all that long to do.  The tradeoff in being able to see the data type
readily while working with the code makes the risk more than worth it as far
as I'm concerned.

> If one is sensible then one only gives 'names' to things with a life
> longer than a dragon fly :
>         S is a much easier thing to recognize than sTemp
>
> By my convention 'S' /is/ 'temporary' so sTemp is repetitious

I *might* use 's' for a string in an isolated routine of a half-dozen lines
or so where the intent is obvious and the declaration is right there.
Anything longer and 'sTemp' I find much more easily read.

> Similarly  P  is a dragonfly pointer but pStart is significant

p for pointer?  don't have much use for that in VB! <g>

> IMO a smattering of Hungarian is useful, but excessive use of it makes
> one forget 'Plain English'

I think we agree on that in the general sense but I'll bet our definitions
of 'excessive' don't line up too well! <g>

Use (and abuse) of naming conventions is definitely mostly personal
experience and preference.  One person's elegant system is another's awkward
jumble.  I've long held that it's really more important to have some sort of
system and use it consistently than what the details of that system are.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
25 Sep 2005 1:42 PM
J French
On Sun, 25 Sep 2005 05:56:49 -0700, "Bob Butler"
<tiredofit@nospam.com> wrote:

>"J French" <erew***@nowhere.uk> wrote in message
>news:43367359.4090163@news.btopenworld.com
>> Not necessarily, with a little common sense one can establish
>> meaningful names that are still 'meaningful' out of 'context'

>To a degree, yes.  "CustomerName" is probably a string while
>'TotalAmountDue' is probably currency; at the very least you know it's
>probably numeric.

Agreed - generally I work 'Sum' into the name to indicate Currency

>> For example:
>>      InRoutineFlag   is a Boolian preventing recursion
>>
>>      By convention Flag is always Boolean
>>      (even before Boolean existed)

>by your convention; by the convention many use possibly.  by the conventions
>that I use 'flag' would indicate a numeric variable that can take on any of
>several states

To me that would probably be 'Value'
- possibly an Enum

>> Another problem with using Hungarian for simple variables is that one
>> may wish to change a data type from say, Integer to Long, so one has
>> to go through changing : iCount to lCount

>True enough although I can't remember a case where I've needed to do that.

Normally I get bitten on String stuff there
- the old assumptions from very old Strings

>It's probably come up but with a global search and replace it's not like it
>takes all that long to do.  The tradeoff in being able to see the data type
>readily while working with the code makes the risk more than worth it as far
>as I'm concerned.

Actually I'm keen on $, & an %  especially when calling APIs
- the Suffix /cannot/ lie

>> If one is sensible then one only gives 'names' to things with a life
>> longer than a dragon fly :
>>         S is a much easier thing to recognize than sTemp

>> By my convention 'S' /is/ 'temporary' so sTemp is repetitious

>I *might* use 's' for a string in an isolated routine of a half-dozen lines
>or so where the intent is obvious and the declaration is right there.
>Anything longer and 'sTemp' I find much more easily read.

No, if is more than a dragonfly it would get a name of some sort

>> Similarly  P  is a dragonfly pointer but pStart is significant

>p for pointer?  don't have much use for that in VB! <g>

Oddly I do occasionally find uses for it
- normally APIs
- or when sorting an Integer/Long Array that indexes another Array or
offsets

>> IMO a smattering of Hungarian is useful, but excessive use of it makes
>> one forget 'Plain English'

>I think we agree on that in the general sense but I'll bet our definitions
>of 'excessive' don't line up too well! <g>

Partly I find excessive 'Hungarian' very ugly
- but agreed about the 'definition'

>Use (and abuse) of naming conventions is definitely mostly personal
>experience and preference.  One person's elegant system is another's awkward
>jumble.  I've long held that it's really more important to have some sort of
>system and use it consistently than what the details of that system are.

I'll certainly agree that an /inconsistent/ set of conventions is a
complete nightmare

However, I do like code to read like 'plain English'
- generally one gets something working fine, but to understand it one
has to squint at the code
- a bit of extra time 'combing' the code often improves the algorithm,
and makes maintenance a lot easier

Partly the acid test is looking at some old code and asking :-
   'What idiot wrote that ?'

The answer is generally very embarrassing  <g>
Author
25 Sep 2005 3:08 PM
Bob Butler
"J French" <erew***@nowhere.uk> wrote in message
news:4336a46e.16656705@news.btopenworld.com
> Actually I'm keen on $, & an %  especially when calling APIs
> - the Suffix /cannot/ lie

All I can say about that is "blech!"
<g>

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
24 Sep 2005 9:48 AM
J French
On Fri, 23 Sep 2005 10:09:31 -0400, "Jeff Johnson [MVP: VB]"
<i.get@enough.spam> wrote:

<snip>

>> I avoid that sort of problem by using UDTs

>I avoid it by using the underscore with my scope identifiers:

>    m_ID vs. mID

Yes, I started off using that convention
- but it looked ugly
- and I found the side effects of using a UDT rather useful

>Other than constants, it's my only use of underscores in variable names.
>(But then constant isn't a variable, now is it? You know what I mean....)

Ditto - and I agree about Constants not really being Variables
.... although in some languages you can 'Writeable Constants'
- a concept that makes my stomach heave
Author
20 Sep 2005 4:22 PM
Jim Edgar
Show quote Hide quote
"Newbie" wrote:

> Hi,
>
> I have (had) the following problem which i worked around but i would still
> like to find an answer.
>
> if myDate is e.g. : #02/04/1998# how do i extract the year ?
>
> Year(myDate) is only for VBA.
> myDate.Year doesnt exist.
>
> Any suggestions ?
>
> Thanx in advance !
> -steve
>
>
>

As Larry pointed out, Year(myDate) does return the year in VB6.  Are you
using VB.Net?

Jim Edgar