MonthCalendar

S

Sand

I have a monthCalender that I want users to be able to
select a range of dates (or a single date) then calculate
a value based on the number of days that person has
selected. The values are higher for the weekends so I
need to be able to figure out how many of the selected
days are weekdays versus weekends.

I already have code to tell me the number of days the
person has selected. I also understand how to get the
DayOfWeek. I tried to code an nested if statement that
would check the dayofweek, see how far away from the
weekend it is and then check that against the number of
days the user selected but it has become unwieldly and,
well, pretty crazy looking.

I'm hoping that someone can tell me a simpler coding
method. My other thought was maybe it's possible
something to do with the SelectedRange? I thought maybe
there is a way to loop through selected range to see if
dayofweek for each weekend day fell inside that range but
I really don't know how I would do that (if it's even
possible)especially since dayofweek gives an integer while
selectedrange gives dates.

Oy!

Any help would be greatly appreciated.

Thank you.
 
C

Cor

Hi Sand,

If you want us to help you to make your code more simple, than I think we
first have to see that.

Just a thought,

Cor
 
H

Herfried K. Wagner [MVP]

* "Sand said:
I have a monthCalender that I want users to be able to
select a range of dates (or a single date) then calculate
a value based on the number of days that person has
selected. The values are higher for the weekends so I
need to be able to figure out how many of the selected
days are weekdays versus weekends.

I already have code to tell me the number of days the
person has selected. I also understand how to get the
DayOfWeek. I tried to code an nested if statement that
would check the dayofweek, see how far away from the
weekend it is and then check that against the number of
days the user selected but it has become unwieldly and,
well, pretty crazy looking.

I'm hoping that someone can tell me a simpler coding
method. My other thought was maybe it's possible
something to do with the SelectedRange? I thought maybe
there is a way to loop through selected range to see if
dayofweek for each weekend day fell inside that range but
I really don't know how I would do that (if it's even
possible)especially since dayofweek gives an integer while
selectedrange gives dates.

Post code!
 
B

Bernie Yaeger

Hi Sand,

First, set up 2 monthcalendar controls - start and end. Then have the user
select the same date in both if only one day. Then have a look at the
datediff function to determine the date range.

HTH,

Bernie Yaeger
 
G

Guest

I posted my original question after midnight, I was pretty
frazzled by that time. I think I can state the problem
more clearly now.

I have two variable dates. Projectstart and ProjectEnd I
also have two integers projectstart.day and projectend.day
that correspond to those dates. I want to find out
whether either of two weekend days fall in between them.
Both weekend days have a reacurring integer value based on
the week (5 and 6 because my weekends are actually friday
and saturday) though I am unsure how to refer to them in
code.


I don't have much code now, I deleted my nested if
statements when I realized it was going to be unworkable.
The idea is simple enough though I can recreate it.

Rough code:

Dim intNumberOfDays as integer
Dim intNumberOfWeekdays as integer
Dim intNumberOfWeekEndDays as integer

Private Sub MonthCalendar1_DateChanged(byVal...) Handles
dtmCharges.DateChanged

'Find the number of days the user selected
intNumberOfDays = projectEnd.day - projectStart.day

btnCalculateValue_click (byVal...) Handles
btnCalculateValue.Click

'Checking position of the project start day
'Calculate how many weekend versus weekdays based on how
'far away the start day is from the first weekend day
'then checking the total number of days selected to
'determine if it includes weekends
If projectStart.dayOfWeek = 1 and intNumberOfDays = 4 then
intNumberOfWeekDays = 3
intnumberOfWeekEndDDays = 1
else If projectStart.dayOfWeek = 1 and intNumberOfDays = 5
then
intNumberOfWeekDays = 3
intNumberOfWeekEndDays = 3
else if projectStart.dayOfWeek = 1 and intNumberofDays <=3
then
intnumberofWeekdays = intNumberOfDays
intnumberofWeekEndDays = 0
else if projectStart.dayOfWeek = 1 and intNumberOfDays >=6
intnumberOfWeekDays = intNumberOfDays - 2
intNumberOfWeekEndDays = 2
end if
end if
end if

end sub
end class


As you can probabaly tell Id basically have to do the same
code checking to see if the projectstart.day was 1-7
adjusting the numerical values if the day started
Monday,Tuesday,Wednesday,Thursday,Friday,Saturday or
Sunday and comparing it to the length of stay.

I hope that makes more sense then my original post.

thank you,

Sand
 
G

Guest

Hi Bernie,

I'm a bit confused. As far as I can tell

Date diff returns a Long value specifying the number of
time intervals between two Date values.

DateDiff(interval, date1 as object, date2 as object)

So if this is going to return the number of intervals
between two dates then that is no problem. The calendar
control has the ability to ive an integer value using .day
that I can then use to calculate the number of days
selected.

To get that I use

intNumberofDaysSelected = projectend.day - projectStart.day

I just tried to use the datediff function but because the
arguments Date1 and Date2 are objects not dates I'm
throwing exceptions. Maybe I could convert projectstart
and projectend to objects somehow? Even so if datediff is
just returning the number of intervals (in this case 1 day)
then my code already does that.

What I need is to check between thsoe dates to see if they
contain one or two weekend dates.

Thank you,
Sand
 
S

Sand

Well I was looking at that code I posted and I realized it
would be better to do it, make sure it works and then post
it. I'm not sure how to make code look like it does
inside.net so I'll try to make it close

Here is the code, jsut checking if the starting date is
monday. I'd have to write the same code for tuesday-
sunday adjusting the values as I went.

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

If projectStart.DayOfWeek = 1 And intDaysStayed <= 3 Then
intWeekDay = intDaysStayed
intWeekend = 0
ElseIf projectStart.DayOfWeek = 1 And intDaysStayed = 4
Then
intWeekDay = 3
intWeekend = 1
ElseIf projectStart.DayOfWeek = 1 And intDaysStayed = 5
Then
intWeekDay = 3
intWeekend = 2
ElseIf projectStart.DayOfWeek = 1 And intDaysStayed <= 10
Then
intWeekDay = intDaysStayed - 2
intWeekend = 2
ElseIf projectStart.DayOfWeek = 1 And intDaysStayed = 11
Then
intWeekDay = intDaysStayed - 3
intWeekend = 3
ElseIf projectStart.DayOfWeek = 1 And intDaysStayed = 12
Then
intWeekDay = intDaysStayed - 4
intWeekend = 4
ElseIf projectStart.DayOfWeek = 1 And intDaysStayed >= 13
Then
intWeekDay = intDaysStayed - 4
intWeekend = 4

End If
 
B

Bernie Yaeger

Hi Sand,

DateDiff will return the difference between d1 and d2, as you are aware.
Convert your objects to date variables and you will have the range. The you
can use dateinterval.weekday inside datediff to get the number of weekdays.
Then compare # of weekdays to the prior range total and if there's a 2 day
or more difference, you have your answer.

HTH,

Bernie
 
G

Guest

I'm afraid I dont really understand. I used dafediff as follows:

intTotalDays = datediff(dateinterval.day, projectstart, projectend) that gives me the total number of days the user has selected as you said that returns the interval, which is the number of days between the start and end of the user selection.

If i then use datediff(dateinterval.weekday, projectstart, projectend) I get some odd results that I don't know what to do with. If the user starts and thursday and goes two weeks I get a dateinterval.weekday of 1. If the user selects Thursday and only goes one week I still get a dateinterval.weekday or 1. In the first case they are including 2 weekend days in the second 4 but I'm getting the same result.

I don't know enough to figure out what you are saying. Can you state it in another way maybe?

Thank you so much,
Sand
 
B

Bernie Yaeger

Hi Sand,

Sorry I couldn't get back to you sooner.

Not your fault - I misunderstood what weekday returns.

However, I did figure out how you can do what you need to do. Below is a
snippet of code that shows how to get the weekdays enum for a range of time.
It's a short way from this to each of the 6's and 7's (sat and sun). It
uses datediff, dateadd and datepart functions. Let me know if you have any
questions.
Dim sdate, edate As DateTime

sdate = #12/3/2003#

edate = #12/23/2003#

Dim dlist As New ArrayList

Dim l As Long

l = DateDiff(DateInterval.Day, sdate, edate)

Dim i As Integer

For i = 0 To l - 1

dlist.Add(DatePart(DateInterval.Weekday, DateAdd(DateInterval.Day, i,
sdate)))

Next

For i = 0 To dlist.Count - 1

MessageBox.Show(dlist(i))

Next

HTH,

Bernie



sand said:
I'm afraid I dont really understand. I used dafediff as follows:

intTotalDays = datediff(dateinterval.day, projectstart, projectend) that
gives me the total number of days the user has selected as you said that
returns the interval, which is the number of days between the start and end
of the user selection.
If i then use datediff(dateinterval.weekday, projectstart, projectend) I
get some odd results that I don't know what to do with. If the user starts
and thursday and goes two weeks I get a dateinterval.weekday of 1. If the
user selects Thursday and only goes one week I still get a
dateinterval.weekday or 1. In the first case they are including 2 weekend
days in the second 4 but I'm getting the same result.
 
C

Cor

Hi Sand,

I did look at your code and also that what Bernie was telling you.

I think he helps you a lot in the right way and on this point of your code I
think I cannot add much more than Bernie does. So I wait how you both
succeed in this.

Cor
 
S

sand

Thank you very much bernie, that was exactly what I was
looking for.
-----Original Message-----
Hi Sand,

Sorry I couldn't get back to you sooner.

Not your fault - I misunderstood what weekday returns.

However, I did figure out how you can do what you need to do. Below is a
snippet of code that shows how to get the weekdays enum for a range of time.
It's a short way from this to each of the 6's and 7's (sat and sun). It
uses datediff, dateadd and datepart functions. Let me know if you have any
questions.
Dim sdate, edate As DateTime

sdate = #12/3/2003#

edate = #12/23/2003#

Dim dlist As New ArrayList

Dim l As Long

l = DateDiff(DateInterval.Day, sdate, edate)

Dim i As Integer

For i = 0 To l - 1

dlist.Add(DatePart(DateInterval.Weekday, DateAdd (DateInterval.Day, i,
sdate)))

Next

For i = 0 To dlist.Count - 1

MessageBox.Show(dlist(i))

Next

HTH,

Bernie



projectend) that
gives me the total number of days the user has selected as you said that
returns the interval, which is the number of days between the start and end
of the user selection. projectstart, projectend) I
get some odd results that I don't know what to do with. If the user starts
and thursday and goes two weeks I get a
dateinterval.weekday of 1. If the
 

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