-
Jan 13th, 2021, 07:51 PM
#1
Thread Starter
Junior Member
[RESOLVED] How do you work out the difference between two dates.
Basically I have two Date/Time Pickers within my program. One for Arrival and One for Departure.
I need to work out the difference between the two dates selected so I can use them in a calculation later on.
-
Jan 13th, 2021, 07:54 PM
#2
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
''
'Displaying Prices for Liverpool Pod
If cboxPod.Text = "Liverpool" Then
TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate - ArrivalDate))
VAT.Text = ("£" & TotalPrice.Text * 0.2)
Subtotal.Text = ("£" & TotalPrice.Text * 0.8)
End If
''
This is the code I currently have, I'm getting the error codes:
''
Severity Code Description Project File Line Suppression State
Error BC30452 Operator '-' is not defined for types 'DateTimePicker' and 'DateTimePicker'. GloriousGetaways C:\Users\THopw\Documents\Year 13\Computer Science\Glorious Getaways\GloriousGetaways\Forms\Bookings\BookAPod.vb 86 Active
''
-
Jan 13th, 2021, 09:00 PM
#3
Re: How do you work out the difference between two dates.
There are a number of ways you could do this.
Perhaps try this (I didn't test it)
Code:
TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate.Subtract(ArrivalDate).Days)
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 13th, 2021, 09:03 PM
#4
Re: How do you work out the difference between two dates.
Code:
TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate - ArrivalDate).Days)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:03 PM
#5
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
Thank you for your reply, It has given me the error 'Subtract is not a member of DateTimePicker'
-
Jan 13th, 2021, 09:05 PM
#6
Re: How do you work out the difference between two dates.
Are DepartureDate and ArrivalDate DateTimePickers?
Code:
TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate.Value - ArrivalDate.Value).Days)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:06 PM
#7
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
@.paul. I was given this error
Attachment 179860
-
Jan 13th, 2021, 09:07 PM
#8
Re: How do you work out the difference between two dates.
Paul's code may work for you. (p.s. I guess not, as Paul assumed as I did that you were subtracting dates, not trying to subtract DateTimePickers).
My code would need to be updated to use the Value of the DateTimePicker.
Code:
TotalPrice.Text = ("£" & 9 * People.Value * (DepartureDate.Value.Subtract(ArrivalDate.Value).Days)
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 13th, 2021, 09:07 PM
#9
Re: How do you work out the difference between two dates.
Your attachment is not working. What type of control is People?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:12 PM
#10
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
People is NumericUpDown, although this is working
-
Jan 13th, 2021, 09:15 PM
#11
Re: How do you work out the difference between two dates.
So what is the problem? You need to explain. It's not working doesn't give anyone a clue
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:19 PM
#12
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
The calculation is working, but without the difference between the two dates included within it. So at the minute, the program is only doing 9 * People.Value instead of 9 * People.Value * (ArrivalDate.Value - Departure Date.Value). If it means anything, this code is within a Private Sub cboxPod_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboxPod.SelectedIndexChanged, whether it should be placed somewhere else, I'm unsure
-
Jan 13th, 2021, 09:20 PM
#13
Re: How do you work out the difference between two dates.
Does this give you the result that you're expecting?
Code:
TotalPrice.Text = ("£" & 9 * People.Value * Math.Abs((DepartureDate.Value - ArrivalDate.Value).Days))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:24 PM
#14
Re: How do you work out the difference between two dates.
If you look at DepartureDate and ArrivalDate, what do you expect the subtraction to be? also, where you calculate your totals is a good place for your code to be, whether that is a Button.Click or a ListBox.SelectedIndexChanged, etc... It depends whether you're doing running calculations, or all at the end.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:30 PM
#15
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
I want to do running calculations.
For example: If I changed the ComboBox Input, the price would update. If I changed the NumericUpDown Value, the price would update. If I changed the Arrival or Departure Date, The price would update.
In terms of the DateTimePickers, I would like the subtraction to be the Day the customer attends the Pod (Arrival Date) minus the Day the Customer Leaves the Pod (Departure Date) which will equal the number of days the customer is staying at the pod. This number will then be used to calculate the price.
-
Jan 13th, 2021, 09:39 PM
#16
Re: How do you work out the difference between two dates.
Use a multiple event handler, like this...
Code:
Private Sub doTotals(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, _
People.ValueChanged, _
DepartureDate.ValueChanged, _
ArrivalDate.ValueChanged
TotalPrice.Text = ("£" & 9 * People.Value * Math.Abs((DepartureDate.Value - ArrivalDate.Value).Days))
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 13th, 2021, 09:48 PM
#17
Thread Starter
Junior Member
Re: How do you work out the difference between two dates.
Brilliant, That's worked perfectly. Thank you very much.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|