I'm trying to determine how many mondays there is between two dates.
These dates will be inserted by a datetimepicker
I know there are similar threads to this but they're all in PHP or VBScript.
Any ideas?
This post tells you how to do it in C#. Count number of Mondays in a given date range
I have run the code through a converter below. Have not tested it.
Private Shared Function CountDays(day As DayOfWeek, start As DateTime, [end] As DateTime) As Integer
Dim ts As TimeSpan = [end] - start
' Total duration
Dim count As Integer = CInt(Math.Floor(ts.TotalDays / 7))
' Number of whole weeks
Dim remainder As Integer = CInt(ts.TotalDays Mod 7)
' Number of remaining days
Dim sinceLastDay As Integer = CInt([end].DayOfWeek - day)
' Number of days since last [day]
If sinceLastDay < 0 Then
sinceLastDay += 7
End If
' Adjust for negative days since last [day]
' If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
If remainder >= sinceLastDay Then
count += 1
End If
Return count
End Function
Related
I have this setup here:
'highest number of days and lowest
niedrigsterTag = 8
hoechsterTag = 8
dim tageV(), tageB()
redim tageV(7), tageB(7)
'day-mapping
tageV(0) = replace(rs("TagVon"),"Mo", 1)
tageV(1) = replace(rs("TagVon"),"Di", 2)
tageV(2) = replace(rs("TagVon"),"Mi", 3)
tageV(3) = replace(rs("TagVon"),"Do", 4)
tageV(4) = replace(rs("TagVon"),"Fr", 5)
tageV(5) = replace(rs("TagVon"),"Sa", 6)
tageV(6) = 7
'for example: mo - fr
for each item in tageV
'save smallest weekday
if(isNumeric(item)) then
if(item <= niedrigsterTag) then
niedrigsterTag = item
Response.write(niedrigsterTag)
response.end()
end if
end if
next
As you might see, I'm pretty new into classic ASP. I don't understand what I'm missing on my loop. In pseudocode, it looks fine:
for each numeric value in my array, check if the current value of item is <= the current maxValue (hoechsterTag) - which is in the first iteration 8. If so, override the current value.
Now I'm stuck. I added a response.end() in the most-inner if. However, niedrigsterTag has a value of 7 instead of 1. Also, during the 1st iteration, item should be 1, right? For me it is 7. I imagined response.end() is an equivalent to PHP's die()
What I'm trying to realize:
if current iteration < current value, override it, so I'm ending up with the smallest value.
I know this is pretty basic, and so far I hadn't problems doing stuff like this in other languages. Don't know why this makes it so special.
Thank you for any hints and advices
When you are assigning the values to your tageV array, you are assigning them as strings and then comparing them to integers. You need to compare like datatypes for one.
Also, they way it is written is like this: In the first iteration, if the item is 1 and niedrigsterTag is 8 then niedrigsterTag is changed to 1 and response.end stops the loop and exits. What you need is more like this:
'highest number of days and lowest
niedrigsterTag = 8
hoechsterTag = 8
dim tageV(), tageB()
redim tageV(7), tageB(7)
'day-mapping
tageV(0) = replace(rs("TagVon"),"Mo", 1)
tageV(1) = replace(rs("TagVon"),"Di", 2)
tageV(2) = replace(rs("TagVon"),"Mi", 3)
tageV(3) = replace(rs("TagVon"),"Do", 4)
tageV(4) = replace(rs("TagVon"),"Fr", 5)
tageV(5) = replace(rs("TagVon"),"Sa", 6)
tageV(6) = 7
'for example: mo - fr
for each item in tageV
'save smallest weekday
if(CInt(item) <= niedrigsterTag) then
niedrigsterTag = CInt(item)
end if
next
Response.write("niedrigsterTag = " & niedrigsterTag)
This loops through the array and every time it finds a smaller value, the variable is assigned that value. Once the loop is done, the variable will hold the smallest value.
By the way, the reason you were getting 7 was because that was the only value that was making it past the if statements.
am generating a game using vb 6.0, at one face i need to generate non repeating random numbers between 1 and 100. am using the following code for generating the random numbers
dim n(10) as integer
for i=0 to 9
n(i)=round(rnd*100)
next i
the code is within a for loop hence it generate 10 Nos. randomly, but it includes repeating numbers and also '0', is their any suggestion to avoid repeating numbers and '0'
then output of my code is:
n()={42,14,10,22,5,42,12,0,59,72}
the numbers 42 is appear two times in the array and 0 cannot be avoided
thanks in advance
Here is a simple technique
Dim n(10) As Integer
Dim choice(100) As Integer
' Create a list of possible numbers
For i = 0 To 100
choice(i) = i
Next
' Populate the array with unique numbers
For i = 1 To 10
lastix = 101 - i
' Find one that has not been selected
ix = Round(Rnd * lastix)
' Assign it
n(i) = choice(ix)
' Replace with one that has not been used
choice(ix) = choice(lastix)
Next
You can use the same technique for shuffling cards.
To avoid 0, multiply by 99 and add 1. To avoid duplicates, keep track of what you generated and retry if you get a duplicate. (Since you need only a few numbers. If you need many, shuffle an array of all possible outcomes and take the initial members.)
the solution below is not the fastest, but makes up by that for being easy ...
it uses a hidden listbox control which contains the required values and retreives a random one every time
the values in this example are just the squared numbers of the index
run the project and click the command button to see what happens. it should show messageboxes with the square numbers in random order
'1 form with:
'1 listbox control : name=List1
'1 command button : name=Command1
Option Explicit
Private Sub Command1_Click()
Dim intIndex As Integer
'generate a new random sequence every time
Randomize Timer
'loop through the list and retreive 1 random value at a time
With List1
Do While .ListCount > 0
intIndex = Int(Rnd * .ListCount)
MsgBox .List(intIndex)
'remove the used item from the list
.RemoveItem intIndex
Loop
End With 'List1
End Sub
Private Sub Form_Load()
Dim intIndex As Integer
'hide listbox
List1.Visible = False
'fill listbox with values (squares)
List1.Clear
For intIndex = 1 To 10
List1.AddItem CStr(intIndex * intIndex)
Next intIndex
End Sub
btw i am just using 10 numbers so you dont have to click through 100 messageboxes :)
I have the If statement as below. If given values A and B as below, I wonder why the condition will be true in the end as the DateDiff will always return 0 and 0 is not larger than 100. I would appreciate for any comment.
A = ""
B = 100
If DateDiff("n", Me.A, Now()) > Val(Me.B) Then
End If
The datediff function returns the number of minutes since the start of the year 1900 which is a lot more than 100.
I couldn't get it to work with an empty string for A though
If, in MATLAB, I have a set of start and end dates that represent a contiguous period of time, how would I take a separate daily time series and accumulate any values from said time series over each start/end pair?
Is this something that can be done with accumarray()? It wasn't clear to me whether it would be efficient to construct the vector that groups each element of the time series by start/end pair.
Inputs
Start Date End Date
01/01/12 01/31/12
02/01/12 02/28/12
...
Date Value
01/01/12 23
01/02/12 87
01/03/12 5
01/04/12 12
...
02/01/12 4
Output
Start Date End Date Value
01/01/12 01/31/12 127
02/01/12 02/28/12 4
...
For consecutive periods of time, the following approach might work. Note that the strings containing dates are cellstrings and, for consecutive data, only the first column of your start date /end date matrix is necesssary.
Furthermore, note that I separated your time series data into two variables for the sake of clarity.
dateBins = {...
'01/01/12';
'02/01/12';
'03/01/12';
'04/01/12'};
dates = {
'01/01/12'
'01/02/12'
'01/03/12'
'01/04/12'
'02/01/12' };
values = [
23
87
5
12
4];
With these variables, the following code
[thrash, idx] = histc(datenum(dates), datenum(dateBins))
accumVal = accumarray(idx, values);
result = zeros(length(dateBins), 1);
result(1:length(accumVal),1) = accumVal;
will result in:
result =
127
4
0
0
Assuming you have already got two vectors with the start dates and end dates in a format that you can use to compare and you just want to count how many occurrences there are in each cateogory, then it is quite straightforward:
% Your data
Dates = 25*rand(10,1);
StartDate = [1 10 20];
EndDate = [9 19 29];
% The Calculation
Result = zeros(size(StartDate)); %Initialization
for d = 1:length(startdate)
idx = dates >= StartDate & dates <= EndDate;
Result(d) = sum(idx);
end
Note that this will require that you store your dates in a comparable format.
I would iterate over each pair of start/end dates. Then pick out the index start/stop pairs and sum them. If you use datestrs, you can make the following less brittle, while allowing for more flexibility in how you represent times that cross years, etc.
start_date = {'01/01/12'};
end_date={'01/31/12'};
datevec={'01/01/12','01/02/12','01/03/12','01/31/12'};
values=[23,87,5,12];
for i=1:numel(start_date)
is = find(ismember(datevec,start_date{i})==1);
ie = find(ismember(datevec,end_date{i})==1);
sum(values(is:ie))
end
I have 2 independent but contiguous date ranges. The first range is the start and end date for a project. Lets say start = 3/21/10 and end = 5/16/10. The second range is a month boundary (say 3/1/10 to 3/31/10, 4/1/10 to 4/30/10, etc.) I need to figure out how many days in each month fall into the first range.
The answer to my example above is March = 10, April = 30, May = 16.
I am trying to figure out an excel formula or VBA function that will give me this value.
Any thoughts on an algorithm for this? I feel it should be rather easy but I can't seem to figure it out.
I have a formula which will return TRUE/FALSE if ANY part of the month range is within the project start/end but not the number of days. That function is below.
return month_start <= project_end And month_end >= project_start
Think it figured it out.
=MAX( MIN(project_end, month_end) - MAX(project_start,month_start) + 1 , 0 )