my question is how can I calculate age using datetimepicker in another textbox?
e.g. If user picks a 10/06/2014 date assuming today is 11/06/2014, textbox should show "1 day"
or if user picks 02/03/2014, txtbox should show "3 months"
This is my code for calculate age in years
Private Sub BirthDate_ValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles BirthDate.ValueChanged
txtAge.Text = Fix((DateDiff(DateInterval.Day, BirthDate.Value, Now.Date)) / 365) & " years"
End Sub
Thanks in advance!
Check out the DateDiff specification: http://msdn.microsoft.com/en-us/library/b5xbyt6f%28v=vs.90%29.aspx
You're already using it, but if you want years, you don't need to divide the days by 365, you can use DateInterval.Year.
You can apply the same thing to other time intervals, you'll need to set parameters for when you'd like to use each.
For example (pseudocode):
if DateDiff(DateInterval.Day, Birthdate.Value, Now.Date) < 30
print DateDiff(DateInterval.Day, Birthdate.Value, Now.Date)
elseif 29 < DateDiff(DateInterval.Day, Birthdate.Value, Now.Date) < 365
print DateDiff(DateInterval.Month, Birthdate.Value, Now.Date)
elseif DateDiff(DateInterval.Day, Birthdate.Value, Now.Date) > 365
print DateDiff(DateInterval.Year, Birthdate.Value, Now.Date)
I hope this helps!
Related
Afternoon all;
I'm looking for some advice on a MS Access 2016 app that I've been working on. I have a form with a combo box that is sourced from a table containing a week beginning date (Monnday) and a week ending date (Sunday).
My hope was that when the form was opened, the combo box would jump to the current week's Monday. For example, today is the third, so the combo box would have 2/1/21 - 2/7/211 pre-selected.
Is there a way to do this or is this a pipe dream?
Any suggestions would be most appreciated.
I think it will be something like this.
Private Sub Command_Click()
Dim sDate As String
Dim i As Integer
sDate = "01/01/" & Year(Date)
If Day(CDate(sDate)) <> vbMonday Then
sDate = DateAdd("d", vbMonday - Day(CDate(sDate)) - 1, CDate(sDate))
End If
For i = 0 To 51
Combo1.AddItem Format(DateAdd("ww", i, sDate), "mmm dd, yyyy")
If Date > DateAdd("ww", i, sDate) And Date < DateAdd("ww", i + 1, sDate) Then
Combo1.ListIndex = i
End If
Next i
End Sub
Also, see the relevant discussion from the link below.
MS Access 2010 (Design View): return Monday of the current week with Monday as 1st day of the week
I need some help calculating hours worked between two dates, but with an 8-hour work day. I also need to adjust for weekends so I don't say someone took longer then they actually did. I am using VB.net
For example, date1 is 1/23/2020 9:00:00 AM, the start date, and date2 is 1/27/2020 1:30:00 PM, the finish time.
If I run this code:
Dim hours As double = DateDiff(DateInterval.hour, date1, date2)
it would give me the total hours, but would include weekends and not filter it for an 8 hour day.
How can I filter out workday times and weekends? Any help in refining this would be appreciated
One possible option
'Get all days between the start date and the end date
Dim midDays As Date() = Enumerable.Range(0, endDate.Subtract(startDate.AddDays(1)).Days).Select(Function(offset) startDate.AddDays(offset)).ToArray
'Filter out any weekend days
Dim weekdays As Date() = midDays.Where(Function(day) day.DayOfWeek <> DayOfWeek.Saturday AndAlso day.DayOfWeek <> DayOfWeek.Sunday).ToArray
'Assume all days are a full 8 hours
Dim hoursWorked As Decimal = weekdays.Count * 8
This essentially creates a list of all days between the start and end date. Remove weekends from the list and calculates 8 hours for each remaining day.
Of course you would then add the hours from the first and last day to the total.
Simple loop
Dim td As DateTime
If endDate < startDate Then
'switch
td = endDate
endDate = startDate
startDate = td
End If
Dim hours As Integer = 0
td = startDate
While td < endDate
If td.DayOfWeek <> DayOfWeek.Saturday AndAlso td.DayOfWeek <> DayOfWeek.Sunday Then
hours += 8
End If
td = td.AddDays(1)
End While
I want to calculate how many days there are in a month. Obviously if there is a leap year then February would have 29 days.
I have some code that works out if it a leap year or not, but I do not know if it is useful.
I tried to do the current year and to count the days of the month that is entered; however there's an error and I'm not sure what to do.
Sub daysInMonth()
Console.WriteLine("Please Enter month you will like to calculate number of days: ")
Dim inputMonth As DateTime = Console.ReadLine()
Dim newMonth = DateAndTime.Month(inputMonth)
Dim current = Now()
Dim currentYear = (Year(current))
Dim febuaryLeapYear = System.DateTime.DaysInMonth(currentYear, newMonth)
End Sub
Solved
I have this function which returns the number of days in any month, any year. Hope helps
Private Function Dates(ByVal Year As Integer, Month As Integer) As Integer
Return DateTime.DaysInMonth(Year, Month)
End Function
I think the problem in this case is that DaysInMonth requires the month to be expressed as an integer. So if you change inputMonth to Integer, you shouldn't receive an error.
Integer.TryParse(string, integerVariable) will check the string to see if it can be converted to an Integer. It will return True or False so it can be used in an If statemnent. In addition it fills the integerVariable with the Integer representation of the string.
The AndAlso parts of the If will never execute if the previous condition is false. This is called short circuiting. Your code will not blow up comparing numbers in the AndAlso conditions because if the first part is false it never executes.
Sub CalculateDaysInMonth() 'It is a bad idea to name your Sub the same as a .net method
Do 'The method will loop until the user enters correct data
'A more explicit message will help user to enter correct data
Console.WriteLine("Please enter a month by entering a number between 1 and 12")
Dim inputMonth As Integer
If Integer.TryParse(Console.ReadLine, inputMonth) AndAlso inputMonth > 0 AndAlso inputMonth < 13 Then
Dim currentYear As Integer = Now.Year
Dim numberOfDays As Integer = DateTime.DaysInMonth(currentYear, inputMonth)
'This is an interpolated string. If your version doesn't support this you can use String.Format
'String.Format("There are {0} in month {1} this year", numberOfDays, inputMonth)
Console.WriteLine($"There are {numberOfDays} in month {inputMonth} this year")
Return
End If
Loop
End Sub
how to use for next with dates in VB.Net
I need to say something like this
For dt As Date = (someDate) to Today().Date step 1 Month
' do something
next
Try bellow code (using Do While...Loop) :
Label1.Text = ""
Dim somedate As Date = CDate("2017-1-16") 'this is Your somedate
Do While somedate <= Today.Date
'do something, in this case show date in label
Label1.Text += String.Format("{0:yyyy-MM-dd}", somedate) + vbCrLf
'after do something increase somedate for one month
somedate = somedate.AddMonths(1)
Loop
It's important You first do something and then increase Your somedate, otherwise, loop will pass Today.Date and will "jump" to next month after Today.Date.
I'm trying to get a quick ETA on some pre-determined values, 16 and 40. So for example, I need my code to quickly calculate an ETA on an item if it takes 16 hours, but only count the 9-5 (8) hours per day. Obviously I'd need to include the remaining hours of that day, which I have in the code snipped below. However I'm giving myself an ofly sore head trying to work out the best way to proceed with the code. Perhaps someone's got a good idea?
Dim TargetTime as Integer = 16
Dim currentHr As Integer = current.Hour
Dim TodaysRemainingHours As Integer = 0
If currentHr >= 9 AndAlso currentHr < 17 Then
'Count remaining hours
TodaysRemainingHours = (17- currentHr)
Else
'Dont count today
TodaysRemainingHours = 0
End If
My plan is:
TargetTime - TodaysRemainingHours --- Gives the value to count
to.
Somehow calculate the hours based on 9-5 time spans only.
Display lblOutput as: "ETA: 2pm 25/11/2016"
As you can see I know how to get the vaule I need to count to, but I need some help with firstly only counting the hours in each day from 9-5 and then returning the actual hour estimated. This isn't for anything profitable, it's a personal ETA program.
Thank you topshot, your comment helped me work it out! The below code seems to work for me, I haven't identified any issues anyway. I had to make sure I wasn't counting the remaining hours in the current day if the time is past 5pm as well. Thank you.
Dim TargetTime As Integer = 16
Dim current As Datetime = DateTime.now
Dim currentHr As Integer = current.Hour
Dim TodaysRemainingHours As Integer = 0
If currentHr >= 9 AndAlso currentHr < 17 Then
'Count remaining hours
TodaysRemainingHours = (17 - currentHr)
Else
'Dont count today
TodaysRemainingHours = 0
End If
If currentHr >= 9 AndAlso currentHr < 17 Then
'Deduct todays hours from target time.
TargetTime = (TargetTime - TodaysRemainingHours)
'Display results
MsgBox("ETA: " & Now.AddDays(TargetTime / 8))
Else
'Skip todays hours and count from tomorrow morning at 9am
Dim Tomorrow As DateTime = Today.AddDays(1)
Dim TomorrowMorning As TimeSpan = new TimeSpan(09, 00, 0)
Tomorrow = Tomorrow.Date + TomorrowMorning
'Display results
MsgBox("ETA: " & Tomorrow.AddDays(TargetTime / 8))
End If