DateDiff Function

C

cmdolcet69

I'm trying to use the DateDiff function to calculate the difference
whether a shift has been setup. when i run the code below with
strFirstShiftEnd as a stringor date or datetime. I get an error
Argument Date1 cannot be converted to type Date. an anyone clear this
up.

If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd,
"hh:mm AMPM")) <= 0 Then
'logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If
 
J

J.B. Moreno

cmdolcet69 said:
I'm trying to use the DateDiff function to calculate the difference
whether a shift has been setup. when i run the code below with
strFirstShiftEnd as a stringor date or datetime. I get an error
Argument Date1 cannot be converted to type Date. an anyone clear this
up.

If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd,
"hh:mm AMPM")) <= 0 Then
'logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If

Well, the obvious problem is that Format returns a string, not a
DateTime, which is what DateDiff requires.

After that, there's the question of what you were trying to do with the
Format, but that would require more info...
 
A

Armin Zingler

cmdolcet69 said:
I'm trying to use the DateDiff function to calculate the difference
whether a shift has been setup. when i run the code below with
strFirstShiftEnd as a stringor date or datetime. I get an error
Argument Date1 cannot be converted to type Date. an anyone clear
this up.

If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd,
"hh:mm AMPM")) <= 0 Then
'logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If

Never use Strings to try to calculate with. Use the Date and TimeSpan data
types to store these values. Convert to/from String only if needed, e.g. for
visual display/input or file I/O.

An example:

Dim d1, d2 As Date
Dim ts As TimeSpan

d1 = Date.Parse(InputBox("enter a date"))
d2 = Now
ts = d1.Subtract(d2)

MsgBox("Difference is: " & ts.TotalMinutes & " minutes")

So,
- Date (actually DateTime) stores any point in time (which does not have a
duration).
- TimeSpan stores the, surprise, time span between two points in time. You
can get the duration in any unit you want by accessing the Timespan's
members.


Armin
 
C

Cor Ligthert[MVP]

Or in version 2008 with option strict and infer on

\\\
Dim d1 = Date.Parse(InputBox("Enter a date"))
Dim d2 = Now
Dim ts = d1.Subtract(d2)
MessageBox.Show("Difference is: " & ts.TotalMinutes & " minutes")
///

Cor
 
C

cmdolcet69

Never use Strings to try to calculate with. Use the Date and TimeSpan data
types to store these values. Convert to/from String only if needed, e.g. for
visual display/input or file I/O.

An example:

      Dim d1, d2 As Date
      Dim ts As TimeSpan

      d1 = Date.Parse(InputBox("enter a date"))
      d2 = Now
      ts = d1.Subtract(d2)

      MsgBox("Difference is: " & ts.TotalMinutes & " minutes")

So,
- Date (actually DateTime) stores any point in time (which does not have a
duration).
- TimeSpan stores the, surprise, time span between two points in time. You
can get the duration in any unit you want by accessing the Timespan's
members.

Armin

Armin the short of it is this......i have a number of number up down
boxes that users will enter the hour in one box and the minute in the
other box and in a combo box select the choices AM or PM.....How can i
get those values into the above solution u gave me.

Merry Christmas
 
A

Armin Zingler

cmdolcet69 said:
Armin the short of it is this......i have a number of number up down
boxes that users will enter the hour in one box and the minute in
the other box and in a combo box select the choices AM or PM.....How
can i get those values into the above solution u gave me.

I don't know because above you are using "Now" which also contains the date.
Now it seems you only want to subtract times.

Reduced to the scenario you gave in the last paragraph, I would do it this
way:

Dim ts As TimeSpan
Dim hours, minutes As Integer

hours = CInt(Me.NumericUpDown1.Value)
minutes = CInt(Me.NumericUpDown2.Value)

If Me.ComboBox1.SelectedIndex = 1 Then
hours += 12
End If

ts = New TimeSpan(hours, minutes, 0)

Then you can work with the Timespan objects.

But, I'm not sure how you want to handle the date part.

Merry Christmas

Merry Christmas



Armin
 
C

cmdolcet69

I don't know because above you are using "Now" which also contains the date.
Now it seems you only want to subtract times.

Reduced to the scenario you gave in the last paragraph, I would do it this
way:

      Dim ts As TimeSpan
      Dim hours, minutes As Integer

      hours = CInt(Me.NumericUpDown1.Value)
      minutes = CInt(Me.NumericUpDown2.Value)

      If Me.ComboBox1.SelectedIndex = 1 Then
         hours += 12
      End If

      ts = New TimeSpan(hours, minutes, 0)

Then you can work with the Timespan objects.

But, I'm not sure how you want to handle the date part.


Merry Christmas

Armin- Hide quoted text -

- Show quoted text -

The reason i was using the .now function was because i need to compare
the shift changes from the actual time. Do you think i can still do
this?
 
A

Armin Zingler

cmdolcet69 said:
The reason i was using the .now function was because i need to
compare the shift changes from the actual time. Do you think i can
still do
this?

Again I'm not sure. Do you want to subtract DateTime values or Timespan
values? Please give me a specific example (or some) which values you have
(Dates, times, whatever) and where you get them from. What is a "shift
change"?
Please explain it.


Armin
 
C

cmdolcet69

Again I'm not sure. Do you want to subtract DateTime values or Timespan
values? Please give me a specific example (or some) which values you have
(Dates, times, whatever) and where you get them from. What is a "shift
change"?
Please explain it.

Armin- Hide quoted text -

- Show quoted text -

Well what it all boils down to is this: a user will setup his shift
times he got 3 choices (1st shift, second shift, and third shift)
which he enters this information from a updown boxes. once set when he
taking data i need to have a timer that will compare the actual date
versus the shift time. If the shift time is older then the actual time
then it will log the user out...... I thhink i would need to have a
timer that would be triggered when the user first opens the
program....however how can i compare the actual time to the shift time
and if the shift time is older then the actual time then it will log
the user out.
 
A

Armin Zingler

cmdolcet69 said:
Well what it all boils down to is this: a user will setup his shift
times he got 3 choices (1st shift, second shift, and third shift)
which he enters this information from a updown boxes. once set when
he taking data i need to have a timer that will compare the actual
date versus the shift time. If the shift time is older then the
actual time then it will log the user out...... I thhink i would
need to have a timer that would be triggered when the user first
opens the
program....however how can i compare the actual time to the shift
time and if the shift time is older then the actual time then it
will log the user out.


So, you have a start time and an end time for each shift? IMO, in order to
detect a shift is over, the time is not sufficient because the shift is
every day and you have to distinguish between the 1st shift in day X and the
1st shift in day X+1.
You write that the user will be logged out automatically, so it must have
been logged in before. When logging in, you can get the relation to the
specific shift that the user is working in because you have the log-in
date and time. So, you can combine the login date and the shift end time
to get a DateTime value that can be compared to "Now".

"Technically":
- Use the Date property of a DateTime value to get the date without the time
part.
- To combine this date and a TimeSpan value (containing the time only), use
the DateTime object's Add function.
- Use the < > operators to compare DateTime values.


I hope this is what you're looking for.



Armin
 
C

cmdolcet69

So, you have a start time and an end time for each shift? IMO, in order to
detect a shift is over, the time is not sufficient because the shift is
every day and you have to distinguish between the 1st shift in day X and the
1st shift in day X+1.
You write that the user will be logged out automatically, so it must have
been logged in before. When logging in, you can get the relation to the
specific shift that the user is working in because you have the log-in
date and time. So, you can combine the login date and the shift end time
to get a DateTime value that can be compared to "Now".

"Technically":
- Use the Date property of a DateTime value to get the date without the time
part.
- To combine this date and a TimeSpan value (containing the time only), use
the DateTime object's Add function.
- Use the < > operators to compare DateTime values.

I hope this is what you're looking for.

Armin

Sorry i may be a little confused. What i want to do is have a user put
in a time span value.
combobox1= 6 combobox 2 = 30 combobox3=AM
combobox4=11 combobox5= 30 combobox6=AM

I want to take those values all in the combobox and compare then to
the actual time.

How can i do that?
 
A

Armin Zingler

cmdolcet69 said:
Sorry i may be a little confused. What i want to do is have a user
put in a time span value.
combobox1= 6 combobox 2 = 30 combobox3=AM
combobox4=11 combobox5= 30 combobox6=AM

I want to take those values all in the combobox and compare then to
the actual time.

How can i do that?

My previous example

Dim ts As TimeSpan
Dim hours, minutes As Integer

hours = CInt(Me.NumericUpDown1.Value)
minutes = CInt(Me.NumericUpDown2.Value)

If Me.ComboBox1.SelectedIndex = 1 Then
hours += 12
End If

ts = New TimeSpan(hours, minutes, 0)

was using NumericUpDown controls, but you can replace it by comboboxes, too.
Use the SelectedIndex instead to determine the hours/minutes. It shows how
to get the time from the UI into a TimeSpan variable. To get the current
time, call DateTime.Now.TimeOfDay. To compare TimeSpan values in VB 2003
(which you are using IIRC), compare their Ticks property (TS1.ticks <
TS2.ticks), otherwise use the < > operators.


Armin
 
C

cmdolcet69

My previous example

Dim ts As TimeSpan
Dim hours, minutes As Integer

hours = CInt(Me.NumericUpDown1.Value)
minutes = CInt(Me.NumericUpDown2.Value)

If Me.ComboBox1.SelectedIndex = 1 Then
hours += 12
End If

ts = New TimeSpan(hours, minutes, 0)

was using NumericUpDown controls, but you can replace it by comboboxes, too.
Use the SelectedIndex instead to determine the hours/minutes. It shows how
to get the time from the UI into a TimeSpan variable. To get the current
time, call DateTime.Now.TimeOfDay. To compare TimeSpan values in VB 2003
(which you are using IIRC), compare their Ticks property (TS1.ticks <
TS2.ticks), otherwise use the < > operators.

Armin

Armin sorry i should have told you i attempted to add the code:

Dim hours, minutes As Integer

hours = CInt(Me.NumericUpDown1.Value)
minutes = CInt(Me.NumericUpDown2.Value)

If Me.ComboBox1.SelectedIndex = 1 Then
hours += 12
End If

ts = New TimeSpan(hours, minutes, 0)

If ActualTime.Now.ToShortTimeString > ts Then
MessageBox.Show("Your Shift Has Expired. Please Log back
on", "User Shift Notice")
CloseAllCOMPorts()
Me.Close()
Exit Sub
End If


and it gave me an error on the ts saying it was overloaded?
 
A

Armin Zingler

cmdolcet69 said:
Armin sorry i should have told you i attempted to add the code:

Dim hours, minutes As Integer

hours = CInt(Me.NumericUpDown1.Value)
minutes = CInt(Me.NumericUpDown2.Value)

If Me.ComboBox1.SelectedIndex = 1 Then
hours += 12
End If

ts = New TimeSpan(hours, minutes, 0)

If ActualTime.Now.ToShortTimeString > ts Then
MessageBox.Show("Your Shift Has Expired. Please Log back
on", "User Shift Notice")
CloseAllCOMPorts()
Me.Close()
Exit Sub
End If


and it gave me an error on the ts saying it was overloaded?


I don't understand you:

- Sometimes you have Comboboxes, sometimes NumericUpDown controls. Which one
is correct?
- Why "ActualTime.Now"? why not DateTime.Now? Now is a Shared property.
- Why Now.ToShortTimeString? You must not convert to a string.
- Why not Now.TimeOfDay, as I wrote?
- Why not compare the Ticks property, as I wrote?


Armin
 
C

cmdolcet69

I don't understand you:

- Sometimes you have Comboboxes, sometimes NumericUpDown controls. Which one
is correct?
- Why "ActualTime.Now"? why not DateTime.Now? Now is a Shared property.
- Why Now.ToShortTimeString? You must not convert to a string.
- Why not Now.TimeOfDay, as I wrote?
- Why not compare the Ticks property, as I wrote?

Armin

either one sorry i will be using numupdown boxes which your first
example has.
1-The actualtime is just a named variable that i want to assign the
current time.
2-i want to compare the now time with the time set in the numberupdown
boxes.
3-the now.timeofday is a possibility didn;t see that
4-i guess i can let me try that

also how can i get a 01,02,03,04 to display in a numberupdown box?
 
C

cmdolcet69

I don't understand you:

- Sometimes you have Comboboxes, sometimes NumericUpDown controls. Which one
is correct?
- Why "ActualTime.Now"? why not DateTime.Now? Now is a Shared property.
- Why Now.ToShortTimeString? You must not convert to a string.
- Why not Now.TimeOfDay, as I wrote?
- Why not compare the Ticks property, as I wrote?

Armin- Hide quoted text -

- Show quoted text -

Armin used your solution and it worked. I was overcomplicating things
as usual. Anyways did u catch my last post on how to display a 02 or
anything with a 0 infront of the number in a numupdown control?
 
A

Armin Zingler

cmdolcet69 said:
Armin used your solution and it worked. I was overcomplicating things
as usual. Anyways did u catch my last post on how to display a 02 or
anything with a 0 infront of the number in a numupdown control?

Sorry, forgot that one - no, I don't know how to display leading zeros
there. ... Browsing the properties, it is as expected.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top