datetime range vb.net for loop - vb.net

I have 2 DateTime values in a VB.NET application.
Dim startDate as DateTime
Dim endDate as DateTime
Now i want to make a loop for each day between startDate and endDate like:
For Each day As Integer In ??????????
Next
someone have an idea?

C#:
for(DateTime CurrentDate = StartDate;
CurrentDate < EndDate;
CurrentDate = CurrentDate.AddDays(1))
{
}
VB.NET:
Dim CurrentDate As DateTime = StartDate
While CurrentDate < EndDate
CurrentDate = CurrentDate.AddDays(1)
End While

Dim currentDate As Date = startDate
While currentDate <= endDate
'do something with this date...'
currentDate = currentDate.AddDays(1)
End While

Related

How do I get the start date and end date of the current week?

I have a function that returns the current week of the year where we are now, thanks to one of the answers in this forum. Now I need to get the initial date of that week and the final date of that week.
How can I accomplish this?
Protected Friend Function obtenerNumeroSemanas(ByVal dt As DateTime)
Dim cal As Calendar = CultureInfo.InvariantCulture.Calendar
Dim d As DayOfWeek = cal.GetDayOfWeek(dt)
If (d >= DayOfWeek.Monday) AndAlso (d <= DayOfWeek.Wednesday) Then
dt = dt.AddDays(3)
End If
Return cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
End Function
Dim dayOfWeek = Cint(DateTime.Today.DayOfWeek)
Dim startOfWeek = DateTime.Today.AddDays(-1 * dayOfWeek)
Dim endOfWeek = DateTime.Today.AddDays(7 - dayOfWeek).AddSeconds(-1)
Console.WriteLine(startOfWeek)
Console.WriteLine(endOfWeek)
Using today's date (2016-06-19) results in
2016-06-19 12:00:00 AM
2016-06-25 11:59:59 PM
Dim dayOfWeek = CInt(DateTime.Today.DayOfWeek)
Dim startOfWeek = DateTime.Today.AddDays(+1 * dayOfWeek).ToShortDateString
Dim endOfWeek = DateTime.Today.AddDays(4 + dayOfWeek).AddSeconds(+1).ToShortDateString
MessageBox.Show(startOfWeek)
MessageBox.Show(endOfWeek)
that to get the next monday date and the next friday
Dim result$ = DateTime.Today.AddMonths(-1).ToString("dd-MMM-yyyy")

VB.NET - Given a date, how can I get the date of last four fridays?

Given today's date want to get the date of the each Friday for the last four weeks.
Here is an easy LINQ approach:
Dim today = Date.Today
Dim lastFridays = From d In Enumerable.Range(0, Int32.MaxValue)
Let dt = today.AddDays(-d)
Where dt.DayOfWeek = DayOfWeek.Friday
Select dt
Dim lastFourFridays As Date() = lastFridays.Take(4).ToArray()
Since it's not the most efficient approach, here is a query that is still readable and maintainable but only searches the first friday and then takes only every 7th day:
Dim lastFriday = lastFridays.First() ' reuse of above query '
Dim fridays = From d In Enumerable.Range(0, Int32.MaxValue)
Let dt = lastFriday.AddDays(-d * 7)
Select dt
Dim lastFourFridays As Date() = fridays.Take(4).ToArray()
You may consume this one, which returns a list of such dates and excludes the one if the specifiedDate date is Friday:
Public Shared Function GetLastFourFridays(specifiedDate As DateTime) As List(Of DateTime)
Dim dtm As New List(Of DateTime)()
Dim dt As DateTime = specifiedDate
For i As Integer = 0 To 6
dt = dt.AddDays(-1)
If dt.DayOfWeek = DayOfWeek.Friday Then
dtm.Add(dt)
Exit For
End If
Next
dtm.Add(dt.AddDays(-7))
dtm.Add(dt.AddDays(-14))
dtm.Add(dt.AddDays(-21))
Return dtm
End Function
and the way you use it is:
Dim dtm As List(Of DateTime) = GetLastFourFridays(DateTime.Now)
For Each d As var In dtm
Console.WriteLine(String.Format("Date: {0}, Day: {1}", d.ToString(), [Enum].Parse(GetType(DayOfWeek), d.DayOfWeek.ToString())))
Next
Here is my way:
Function Last4Friday(ByVal StartDate As Date) As array
Dim L4F()
Dim mDate as date = StartDate
For value As Integer = 1 To 7
mDate = mDate.AddDays(-1)
If mDate.DayOfWeek = DayOfWeek.Friday Then
L4F = {mDate, mDate.AddDays(-7), mDate.AddDays(-14), mDate.AddDays(-21)}
exit for
End If
Next
Return L4F
End Function
Edit: If you need to check the inserted date and you want it returned in the array you may simply use:
Dim mDate as date = StartDate.AddDays(1)
instead of
Dim mDate as date = StartDate
Try this. It doesn't use a loop to find the starting Friday.
Dim someDate As DateTime = DateTime.Now
If someDate.DayOfWeek <> DayOfWeek.Friday Then
'do the math to get a Friday
someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
End If
Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
All of the other suggestions have used a loop to find the starting Friday. If this code is used infrequently then how the starting Friday is determined might not matter.
edit: as function
Function FindLastFourFridays(someDate As DateTime) As List(Of DateTime)
'Find first Friday to include
If someDate.DayOfWeek <> DayOfWeek.Friday Then
someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
' uncomment these two lines if you do not want initial someDate.DayOfWeek = DayOfWeek.Friday to be included
'Else
' someDate = someDate.AddDays(-7)
End If
'build the return (four fridays)
Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
Return last4Fridays
End Function
This function does not need to be passed a date it picks up today's date and gets the last four Friday's from today. It can be changed around to get any day of the week.
Dim todaysDate As Date = Date.Today
Dim oldDay As Integer
Dim thisWeek As Date
Dim firstWeek As Date
Dim secondWeek As Date
Dim thirdWeek As Date
Dim fourthWeek As Date
'finds the Friday of the end of the current week No mattter what day you are working
Dim daycount As Integer
'use this to check specific dates "Dim datetime As New DateTime(2015, 4, 13)"
oldDay = Weekday(todaysDate)
thisWeek = todaysDate
If oldDay < 6 Then
daycount = 6 - oldDay
thisWeek = thisWeek.AddDays(+daycount)
ElseIf oldDay > 6 Then
daycount = oldDay - 6
thisWeek = thisWeek.AddDays(-daycount)
End If
Dim currentDate As Date = Now
Do While Not currentDate.DayOfWeek = DayOfWeek.Friday
currentDate = currentDate.AddDays(-1)
Loop
fourthWeek = currentDate.AddDays(-21)
thirdWeek = currentDate.AddDays(-14)
secondWeek = currentDate.AddDays(-7)
firstWeek = currentDate

insert records for day of week within date range

I am trying to figure out how to do this. Been doing research, can't find anything for it that I can figure out how to use.
I have a startdate, enddate, recurringday, customer
startdate = 04/01/2013
enddate = 04/30/2013
customer = john
recurringday = monday
I want to insert a record for john every monday within startdate and enddate, can someone please help me? new to vb.net
Thanks
Use DayofWeek function to check if today's day is monday. Then use Insert command.
Dim startdate As DateTime = Convert.ToDateTime("01 Apr 2013")
Dim enddate As DateTime = Convert.ToDateTime("30 Apr 2013")
Dim DofW = Now.DayOfWeek()
dim recurringday = "Monday"
If now >= startdate And now <= enddate Then
If DofW = recurringday Then
'Insert Record
End If
End If
#Sweety suggested .DayofWeek, but I am not sure his method fulfills your requirements. Try this:
Dim StartDate As DateTime = CDate("01 Apr 2013")
Dim FinishDate As DateTime = CDate("30 Apr 2013")
Dim RecurringDay = "Monday"
Dim Period = FinishDate.Subtract(StartDate).TotalDays
Dim CurrentDate As DateTime
For Counter As Integer = 0 To Period
CurrentDate = StartDate.AddDays(Counter)
If CurrentDate.DayOfWeek = RecurringDay Then
'Insert Record
End If
Next
Try this.
Dim StartDate As DateTime = #4/1/2013#
Dim FinishDate As DateTime = #4/30/2013#
Dim RecurringDay As Integer = DayOfWeek.Monday
'force to first RecurringDay
If RecurringDay < StartDate.DayOfWeek Then StartDate = StartDate.AddDays(7)
StartDate = StartDate.AddDays(RecurringDay - StartDate.DayOfWeek)
Do While StartDate <= FinishDate
Debug.WriteLine(StartDate.ToLongDateString)
'insert record
StartDate = StartDate.AddDays(7)
Loop

Date time validation in vb.net

Can anyone help me on how can I validate given DateTime between two DateTime using vb.net?
the given DateTime must not in between the two DateTime..
You can compare date instances the same way you would compare numbers as they override the LessThan, LessThanOrEqual, GreaterThan and GreaterThanOrEqual operators:
Dim minDate = New DateTime(2009, 4, 17)
Dim maxDate = New DateTime(2011, 4, 17)
Dim selectedDate = New DateTime(2010, 4, 17)
If selectedDate > minDate And selectedDate < maxDate Then
Console.WriteLine("selected date is valid")
Else
Console.WriteLine("selected date is invalid")
End If
Use DateTime.Compare
Dim date1 As New DateTime(100)
Dim date2 As New DateTime(200)
Dim workingDate As New DateTime(150)
If DateTime.Compare(workingDate , date1) >= 0 AND DateTime.Compare(workingDate , date2) <= 0 Then
Console.WriteLine("Between")
Else
Console.WriteLine("Not Between")
End If
Dim dt As Date = DateTimePicker1.Value.Date
If dt < CDate("2010-04-01") Or dt > CDate("2010-04-10") Then
MessageBox.Show("Date is not in the first 10 days of April so is ok")
End If

Finding the date of monday in a week with VB.NET

I need to find a way to find the date (DD/MM/YYYY) of the Monday for any week we're on.
For example, for this week, monday would be 09/11/2009, and if this were next week it'd be 16/11/2009.
I managed to get somewhere in the forms of code, but all I got was 'cannot convert to Integer' errors. I was using Date.Today and AddDays().
Thanks for any help. :)
If Sunday is the first day of week, you can simply do this:
Dim today As Date = Date.Today
Dim dayDiff As Integer = today.DayOfWeek - DayOfWeek.Monday
Dim monday As Date = today.AddDays(-dayDiff)
If Monday is the first day of week:
Dim today As Date = Date.Today
Dim dayIndex As Integer = today.DayOfWeek
If dayIndex < DayOfWeek.Monday Then
dayIndex += 7 'Monday is first day of week, no day of week should have a smaller index
End If
Dim dayDiff As Integer = dayIndex - DayOfWeek.Monday
Dim monday As Date = today.AddDays(-dayDiff)
DateTime.DayOfWeek is an enum that indicates what day a given date is. As Monday is 1, you can find the Monday of the current week using the following code:
Dim monday As DateTime = Today.AddDays((Today.DayOfWeek - DayOfWeek.Monday) * -1)
=Format(DateAdd("d", (-1 * WeekDay(Date.Today()) + 2), Date.Today()), "dd/MM/yyyy")
A simple method should get you what you want:
private static DateTime GetMondayForWeek(DateTime inputDate)
{
int daysFromMonday = inputDate.DayOfWeek - DayOfWeek.Monday;
return inputDate.AddDays(-daysFromMonday);
}
You could also extend it for any day that you want as well:
private static DateTime GetDayForWeek(DateTime inputDate, DayOfWeek inputDay)
{
int daysAway = inputDate.DayOfWeek - inputDay;
return inputDate.AddDays(-daysAway);
}
To call the first example just use something like:
DateTime mondayDate = GetMondayForWeek(new DateTime(2009, 11, 15));
Console.WriteLine(mondayDate);
Another approach if Monday is the first day, is this:
Dim today As Date = Date.Today
Dim dayDiff As Integer = today.DayOfWeek - DayOfWeek.Monday
Dim monday As Date = today.AddDays(-dayDiff)
dayDiff = DayOfWeek.Saturday - today.DayOfWeek + 1
Dim sunday As Date = today.AddDays(dayDiff)
There is a day of week method that you can use
Dim instance As DateTime
Dim value As DayOfWeek
value = instance.DayOfWeek
see: http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx
I just did this in a project I'm working on --I promise, it's correct. This is a method that returns the nth monday after the given date. If the given date is a monday, it returns the next monday.
Public Function GetSubsequentMonday(ByVal startDate As DateTime, ByVal subsequentWeeks As Integer) As DateTime
Dim dayOfWeek As Integer = CInt(startDate.DayOfWeek)
Dim daysUntilMonday As Integer = (Math.Sign(dayOfWeek) * (7 - dayOfWeek)) + 1
'number of days until the next Monday
Return startDate.AddDays(CDbl((daysUntilMonday + (7 * (subsequentWeeks - 1)))))
End Function
Following on from my comments to Meta-Knight's answer, here is a short function that makes the correction I mention in the comments:
Public Function GetFirstOfLastWeek() As DateTime
Dim today As DateTime, daysSinceMonday As Integer
today = DateTime.Today
daysSinceMonday = today.DayOfWeek - DayOfWeek.Monday
If daysSinceMonday < 0 Then
daysSinceMonday += 7
End If
Return today.AddDays(-daysSinceMonday)
End Function
And if your week starts from Monday then you can use something like this:
DateTime mondayDate = DateTime.Now.AddDays(((DateTime.Now.DayOfWeek == DayOfWeek.Sunday?7: (int)DateTime.Now.DayOfWeek) - 1)*-1);
DateTime sundayDate = DateTime.Now.AddDays(7 - (DateTime.Now.DayOfWeek == DayOfWeek.Sunday?7: (int)DateTime.Now.DayOfWeek ));
Dim dayOfWeek = CInt(DateTime.Today.DayOfWeek)
Dim startOfWeek = DateTime.Today.AddDays(+1 * dayOfWeek).ToShortDateString
Dim endOfWeek = DateTime.Today.AddDays(6 + dayOfWeek).AddSeconds(+1).ToShortDateString
MessageBox.Show(startOfWeek)
MessageBox.Show(endOfWeek)
Example:
If today is 03/09/2020,
startOfWeek will be 07/09/2020 and
endOfWeek will be 13/09/2020.