Date problem ?

T

Tony B

I'm trying to write a simple vb function that takes a current date and
calculates for the first day of the month of that date what day of the week
it is.

I thought I could take a date object dt, use dt.Days() to return how many
days into the month, then create a date object equal to the 1st of the
month by subtracting the number of days from dt, and then use the dayofweek
method to find out what I need.
My difficulty is how create a date object that is the first day of this
month. The subtract methods need a date object or a timespan object as an
argument, but I cannot see how to set a timespan object to a certain number
of days ?
Am I being stupid here and missing the obvious ?
 
A

AMercer

I'm trying to write a simple vb function that takes a current date and
calculates for the first day of the month of that date what day of the week
it is.

I thought I could take a date object dt, use dt.Days() to return how many
days into the month, then create a date object equal to the 1st of the
month by subtracting the number of days from dt, and then use the dayofweek
method to find out what I need.
My difficulty is how create a date object that is the first day of this
month. The subtract methods need a date object or a timespan object as an
argument, but I cannot see how to set a timespan object to a certain number
of days ?
Am I being stupid here and missing the obvious ?

Assuming dt is a DateTime that contains some date, then the date that is the
first of the month is:
dt.AddDays(1 - dt.Day)
For example, assuming dt is the 10th of some month, the expression adds 1-10
(or -9) days to dt, and you end up on the first of the month. So,
dt.AddDays(1 - dt.Day).DayOfWeek
gets the day of the week of the first of the month.
 
A

Armin Zingler

Tony B said:
I'm trying to write a simple vb function that takes a current date
and calculates for the first day of the month of that date what day
of the week it is.

I thought I could take a date object dt, use dt.Days() to return how
many days into the month, then create a date object equal to the
1st of the month by subtracting the number of days from dt, and then
use the dayofweek method to find out what I need.
My difficulty is how create a date object that is the first day of
this month. The subtract methods need a date object or a timespan
object as an argument, but I cannot see how to set a timespan object
to a certain number of days ?
Am I being stupid here and missing the obvious ?


Private Shared Function WeekDayOfFirstDayInCurrentMonth() _
As DayOfWeek

Dim Today As Date = DateTime.Today

Return Today.AddDays(-Today.Day + 1).DayOfWeek

End Function

Usage:
MsgBox( _
Globalization.DateTimeFormatInfo.CurrentInfo.GetDayName( _
WeekDayOfFirstDayInCurrentMonth _
) _
)


Armin
 
T

Tony B

AMercer said:
Assuming dt is a DateTime that contains some date, then the date that is
the
first of the month is:
dt.AddDays(1 - dt.Day)
For example, assuming dt is the 10th of some month, the expression adds
1-10
(or -9) days to dt, and you end up on the first of the month. So,
dt.AddDays(1 - dt.Day).DayOfWeek
gets the day of the week of the first of the month.
Thank, that's exactly what I needed.
 
C

Cor Ligthert[MVP]

Tony,
Am I being stupid here and missing the obvious ?

Not wanting to be rude, yes.

Have the next time a look at the overloaded functions of methods (not only
this one), that will help you a lot.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top