create date with semi-monthly schedule - vb.net

I created a simple Lending program that generate MONTHLY payment mode. The generated schedule is 4 periods so every month the borrower should pay. Just shown screenshot below.
Here is my code:
Private Sub GenerateScheule()
Dim PrincipalAmount As Double = Me.tbPrincipalAmount.Text
Dim InterestRate As Double = Me.tbInterestRate.Text
Dim Period As Integer = Me.tbPeriod.Text
Dim LoanDate As Date = Me.dtpLoanDate.Text
Dim ModePayment As String = Me.cbModeOfPayment.Text
Me.tbLastPayment.Text = LoanDate.AddMonths(Period)
If cbModeOfPayment.Text = "Semi-Monthly" Then
For i As Integer = 1 To Period
Dim dtDate As Date = LoanDate.AddMonths(i)
dgvLoanSchedule.Rows.Add(i, Format(dtDate, "MMM/dd/yyyy"))
Next
End If
Now i want also to create a SEMI-MONTHLY payment mode. If borrowers has 4 periods it doubles up to 8 periods but in semi-monthly format. Just shown screenshot below.
Please someone help me out. I appreciate your help. Thank you guyz.

Related

Loop through textboxes with datetime

I have this code to add days to the first day of the month.Could someone help me to write this shorter by looping through the Textboxes and add to each box an extra day?
Dim dpt As DateTimePicker = DateTimePicker1
Dim day1 As DateTime = dpt.Value.ToString("01/MM/yyyy")
Dim textBoxes() As TextBox = {TextBox187, TextBox188, TextBox189, TextBox190, TextBox191,TextBox192, TextBox193, TextBox194, TextBox195, TextBox196, TextBox197, TextBox198, TextBox199, TextBox200, TextBox201, TextBox202, TextBox203, TextBox204, TextBox205, TextBox206, TextBox207, TextBox208, TextBox209, TextBox210, TextBox211, TextBox212, TextBox213, TextBox214, TextBox215, TextBox216, TextBox217}
textBoxes(0).Text = day1
textBoxes(1).Text = day1.AddDays(1)
textBoxes(2).Text = day1.AddDays(2)
textBoxes(3).Text = day1.AddDays(3) etc..
Thanks
For loop should fit here:
For i = 0 To textBoxes.Length - 1
textBoxes(i).Text = day1.AddDays(i)
Next

Strange Date Parsing Results

I am trying to make a small helper app to assist in reading SCCM logs. Parsing the dates has been pretty straightforward until I get to the timezone offset. It is usually in the form of "+???". literal example: "11-01-2016 11:44:25.630+480"
DateTime.parse() handles this well most of the time. But occasionally I run into a time stamp that throws an exception. I cannot figure out why. This is where I need help. See example code below:
Dim dateA As DateTime = Nothing
Dim dateB As DateTime = Nothing
Dim dateStr_A As String = "11-07-2016 16:43:51.541+600"
Dim dateStr_B As String = "11-01-2016 11:44:25.630+480"
dateA = DateTime.Parse(dateStr_A)
dateB = DateTime.Parse(dateStr_B)
MsgBox(dateA.ToString & vbCrLf & dateB.ToString)
IF run it would seem dateStr_B is an invalid time stamp? Why is this? I've tried to figure out how to handle the +480 using the 'zzz' using .ParseExact() format as shown here Date Formatting MSDN
Am I missing something with the timezone offset? I've searched high and low but these SCCM logs seem to use a non standard way of representing the offset. Any insight would be greatly appreciated
The problem is that +480 is indeed an invalid offset. The format of the offset from UTC (as produced when using the "zzz" Custom Format Specifier) is hours and minutes. +600 is 6 hours and 0 minutes ahead of UTC, which is valid. +480 would be 4 hours and 80 minutes ahead of UTC, which is invalid as the number of minutes can't be more than 59.
If you have some external source of date and time strings that uses an offset that is simply a number of minutes (i.e. +600 means 10 hours and +480 means 8 hours), you will need to adjust the offset before using DateTime.Parse or DateTime.ParseExact.
[Edit]
The following function takes a timestamp with a positive or negative offset (of any number of digits) in minutes, and returns a DateTime. It throws an ArgumentException if the timestamp is not in a valid format.
Public Function DateTimeFromSCCM(ByVal ts As String) As DateTime
Dim pos As Integer = ts.LastIndexOfAny({"+"c, "-"c})
If pos < 0 Then Throw New ArgumentException("Timestamp must contain a timezone offset", "ts")
Dim offset As Integer
If Not Integer.TryParse(ts.Substring(pos + 1), offset) Then
Throw New ArgumentException("Timezone offset is not numeric", "ts")
End If
Dim hours As Integer = offset \ 60
Dim minutes As Integer = offset Mod 60
Dim timestamp As String = ts.Substring(0, pos + 1) & hours.ToString & minutes.ToString("00")
Dim result As DateTime
If Not DateTime.TryParse(timestamp, result) Then
Throw New ArgumentException("Invalid timestamp", "ts")
End If
Return result
End Function
Thank you for the insight. I had a feeling I would need to handle this manually. I just wanted to make sure I wasn't missing something simple in the process. My knowledge of the date and time formatting is a bit lacking.
As such, I have altered my code so that it handles the offset. Granted I will have to add some more input validation in the final product.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dateA As DateTime = Nothing
Dim dateB As DateTime = Nothing
Dim dateStr_A As String = correctOffset("11-07-2016 16:43:51.541+600")
Dim dateStr_B As String = correctOffset("11-07-2016 16:43:51.541+480")
dateA = DateTime.Parse(dateStr_A)
dateB = DateTime.Parse(dateStr_B)
MsgBox(dateA.ToString & vbCrLf & dateB.ToString)
End Sub
Public Function correctOffset(ByVal ts As String)
Dim offset As Integer = CInt(ts.Substring(ts.Length - 3))
Dim offHour As Integer = offset / 60
Dim offMin As Integer = offset - (offHour * 60)
Dim strhour As String = Nothing
Dim strmin As String = Nothing
If offHour <= 9 Then
strhour = "0" & CStr(offHour)
Else
strhour = CStr(offHour)
End If
If offMin <= 9 Then
strmin = "0" & CStr(offMin)
Else
strmin = CStr(offMin)
End If
Return ts.Substring(0, ts.Length - 3) & strhour & ":" & strmin
End Function

Calculating days to a specific date

Dim intYear, intMonth, intDay As Integer, strResult As String, tspResult As TimeSpan
intYear = Integer.Parse(txtYear.Text)
intMonth = Integer.Parse(txtMonth.Text)
intDay = Integer.Parse(txtDay.Text)
Dim dteDatum As New System.DateTime(intYear, intMonth, intDay)
tspResult = Now.Subtract(dteDate)
strResult = Math.Abs(tspResult.Days).ToString
MessageBox.Show(strResult)
For example:
today it is the 1st of october and i want to know how many days till the 4th of october.
The program will say 2days but that's wrong, it must be 3 days.
How can i fix this?
(Calculations in the past are good)
Thanks in advance and sorry if my english sucks.
Try use datediff
' The following statements set datTim1 to a Thursday
' and datTim2 to the following Tuesday.
Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)

Subtracting days from date

I'm struggling in vein to work out how to remove 5 days from today's date...
I have the following simple code that searches compares the result of a text file array search and then compares them to today's date. If the date within the text file is older than today then it deletes, if not it doesn't.
What i want though is to say if the date in the text file is 5 days or older then delete.
This is being used in the English date format.
Sub KillSuccess()
Dim enUK As New CultureInfo("en-GB")
Dim killdate As String = DateTime.Now.ToString("d", enUK)
For Me.lo = 0 To UBound(textcis)
If textcis(lo).oDte < killdate Then
File.Delete(textcis(lo).oPath & ".txt")
End If
Next
End Sub
Thanks
You can use the AddDays method; in code that would be something like this:
Dim today = DateTime.Now
Dim answer = today.AddDays(-5)
msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx
Which would make your code
Sub KillSuccess()
Dim killdate = DateTime.Now.AddDays(-5)
For Me.lo = 0 To UBound(textcis)
If textcis(lo).oDte < killdate Then
File.Delete(textcis(lo).oPath & ".txt")
End If
Next
End Sub

Using DatePicker to get the current day within the year in VB.NET?

I am currently programming a new Windows Phone Application and within this app i want the user to be able to find the hex date for any date of their choice. For this reason i am letting the user use a date picker to choose their desired date. Before i included the date picker i was allowing the user to get the hex date for today's date by using the following code:
ReverseString = 365 - DateTime.Today.DayOfYear
txtReverseJulian.Text = ReverseString.PadLeft(3, "0")
However now i can't use the DateTime function because i am letting them choose a date from the date picker, how can i change this code so that it will work?
Thank you
I am guessing at what you are trying to do... Could you provide clarification?
Dim foo As DateTime = New DateTime(DateTime.Today.Year, 12, 31) 'last day of the current year
Dim ldoy As Integer = foo.DayOfYear 'number of days in year
Dim dr As Integer = ldoy - DateTime.Today.DayOfYear 'number of days until end of year
Using a DatePicker
Dim foo As DateTime = New DateTime(aDatePicker.Value.Year, 12, 31) 'last day of the current year
Dim ldoy As Integer = foo.DayOfYear 'number of days in year
Dim dr As Integer = ldoy - aDatePicker.Value.DayOfYear 'number of days until end of year
This may help you
//Here is event fire when datepicker value will be change
ValueChanged="DatePicker_ValueChanged"
Private Sub DatePicker_ValueChanged(sender As Object, e As DateTimeValueChangedEventArgs)
ReverseString = e.NewDateTime.Value.ToString()
End Sub
txtReverseJulian.Text = ReverseString.PadLeft(3, "0")