date only

  • Thread starter Thread starter Brian S.
  • Start date Start date
B

Brian S.

Hi,
why does this function return the date and time.. I only want the date to
return.. no time on it :(
Public Shared Function LastWorkDayOfTheMonth(ByVal dtmdate As Date) As Date



Dim x As Date = LastDayOfTheMonthDate(dtmdate)

Do While IsWeekday(x) = False

x = x.AddDays(-1)

Loop

Return x.Date

End Function
 
Brian S. said:
Hi,
why does this function return the date and time.. I only want the
date to return.. no time on it :(
Public Shared Function LastWorkDayOfTheMonth(ByVal dtmdate As Date)
As Date



Dim x As Date = LastDayOfTheMonthDate(dtmdate)

Do While IsWeekday(x) = False

x = x.AddDays(-1)

Loop

Return x.Date

End Function

What do you pass to the function? Where does the value come from?


Armin
 
I pass it a date.....
#4/1/08#.. when I follow through the code all the way to the Return x.Date,
it still has the #4/31/08# format, but in the textbox it includes 12:00
 
Hello Brian S.,
I pass it a date.....
#4/1/08#.. when I follow through the code all the way to the Return
x.Date,
it still has the #4/31/08# format, but in the textbox it includes
12:00

The VB DataType "Date" maps to the Framework "DateTime" DataType.

Therefore it will always contain a time component.

If you subtract the time in question from the date in question, you will
internally be storing 00:00:00 (midnight) on the date in question. depending
on localisation this might be displayed as "00:00:00" or "12:00:00" on the
given date.

This leads to various issues when comparing dates.

You might think is sensible to say...
-------------------------------------------------------------
If SomeDate = #20/Apr/2008# Then

End if
-------------------------------------------------------------
....when what you might really mean is...
-------------------------------------------------------------
If SomeDate >= #20/Apr/2008 00:00:00# andalso SomeDate < #21/Apr/2008 00:00:00
Then

Endif
 
Brian S. said:
I pass it a date.....
#4/1/08#.. when I follow through the code all the way to the Return
x.Date, it still has the #4/31/08# format, but in the textbox it
includes 12:00

A DateTime object represents a certain point in time. #4/31/08 12:00AM#
is one of these. Just like #4/31/08 15:00PM# is. If you want to display
a DateTime object, you don't have to display the time part.

If you have a double value 4.000 you can also not say that the value
"contains" decimal places. It's just the way the value is displayed. You
can omit them and display 4 only.


Armin
 
I understand about the time protion of a date type .... my problem is ..
when you put a break point in the Return x.Date
and in the immed window you do a ?x.date and it returns just #3/1/2008#
That is the part i am trying to understand, why is it displayed differently
there than it is in the text box
 
Ok, I think i do understand now.. somewhat....
I was calling this function by
Me.txtLastWorkDay.Text =
clsDateFunctions.LastWorkDayOfTheMonth(Me.MonthCalendar1.SelectionStart.Date)

and that would produce the #4/30/2008 12:00#

when I do this

Me.txtLastWorkDay.Text =
CStr(clsDateFunctions.LastWorkDayOfTheMonth(Me.MonthCalendar1.SelectionStart.Date))

it shows it the way I want it to...
 
Brian S. said:
I understand about the time protion of a date type .... my problem
is .. when you put a break point in the Return x.Date
and in the immed window you do a ?x.date and it returns just
#3/1/2008# That is the part i am trying to understand, why is it
displayed differently there than it is in the text box

Because you show it in the Textbox differently from how the IDE does it
in the immediate window. Maybe it's shown that way because #3/1/2008# is
the format used with date
literals. (dim var as date = #3/1/2008#) That's a language/culture
independet format just like the whole source code that must be
compilable no matter in which country you open the project. I also write
"If", not "Wenn" (German transl.) even if i don't have an English
version. :-)


Armin
 
Brian S. said:
Ok, I think i do understand now.. somewhat....
I was calling this function by
Me.txtLastWorkDay.Text =
clsDateFunctions.LastWorkDayOfTheMonth(Me.MonthCalendar1.SelectionStart.Date)

and that would produce the #4/30/2008 12:00#

when I do this

Me.txtLastWorkDay.Text =
CStr(clsDateFunctions.LastWorkDayOfTheMonth(Me.MonthCalendar1.SelectionStart.Date))

it shows it the way I want it to...

This shows (again) why you should enable Option Strict On. Assigning
a Date value to the Text property does not compile because these are
differnent types. Enable Option Strict in order to be made aware of
implicit conversions done otherwise. If you are forced to think about
the conversion, you will convert it the way you want right away.


Armin
 
Armin Zingler said:
This shows (again) why you should enable Option Strict On. Assigning
a Date value to the Text property does not compile because these are
differnent types. Enable Option Strict in order to be made aware of
implicit conversions done otherwise. If you are forced to think about
the conversion, you will convert it the way you want right away.


Armin

I think in the immediate window for objects (date included) you get the
..toString version of the object if you don't tell it otherwise.

LS
 
maybe you shouldn't assume things(again)... Option Strict On is the way my
enviroment is set up.
Thanks
 
Cor Ligthert said:
Armin,

The literal is only made for the US customers.

Sorry I don't understand. The literal is the same for everyone.

But I was wrong because I mixed up "?x.date" in the immediate window
with hovering over the DateTime variable and have the value displayed in
the tooltip. In the tooltip, the literal format is used.


Armin
 
Lloyd Sheen said:
I think in the immediate window for objects (date included) you get
the .toString version of the object if you don't tell it otherwise.

You're right. I mixed it up (see my reply to Cor).


Armin
 
Brian S. said:
maybe you shouldn't assume things(again)... Option Strict On is the
way my enviroment is set up.

This line indicates that it is not turned on:

Me.txtLastWorkDay.Text =
clsDateFunctions.LastWorkDayOfTheMonth(Me.MonthCalendar1.SelectionStart.Date)

What's the type of the return value of LastWorkDayOfTheMonth? It's Date,
taken from your other post.
What's the type of Me.txtLastWorkDay.Text? I guess it's string. Isn't
it?

Conclusion: If you can assign one to the other, then Option Strict is
turned OFF. But maybe I'm overlooking something that you could please
kindly explain. Thanks.



Armin
 
not sure i can explain it :(.. I just know, and checked again that Option
Strict is turned on.
 
Armin,

The representation of a Date in MM-dd-yyyy is solely in the US culture

Everywhere else on the world this is not an (official) date representation
but just some characters in a string

Cor
 
Cor Ligthert said:
Armin,

The representation of a Date in MM-dd-yyyy is solely in the US
culture

Everywhere else on the world this is not an (official) date
representation but just some characters in a string

Yes, I know. We know. :-) What I meant: You wrote "The *literal* is
only made for the US customers.", but the literal is always
#mm/dd/yyyy#, no matter in which country/culture (setting).


Armin
 
Armin,

I knew too that we know.

:-)

Cor



Armin Zingler said:
Yes, I know. We know. :-) What I meant: You wrote "The *literal* is only
made for the US customers.", but the literal is always #mm/dd/yyyy#, no
matter in which country/culture (setting).


Armin
 
Back
Top