473,804 Members | 3,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateDiff Function

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
strFirstShiftEn d 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(strFirst ShiftEnd,
"hh:mm AMPM")) <= 0 Then
'logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If
Dec 24 '07 #1
14 3580
cmdolcet69 <co************ @hotmail.comwro te:
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
strFirstShiftEn d 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(strFirst ShiftEnd,
"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...

--
J.B. Moreno
Dec 24 '07 #2
"cmdolcet69 " <co************ @hotmail.comsch rieb
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
strFirstShiftEn d 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(strFirst ShiftEnd,
"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(Inpu tBox("enter a date"))
d2 = Now
ts = d1.Subtract(d2)

MsgBox("Differe nce 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

Dec 24 '07 #3
Or in version 2008 with option strict and infer on

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

Cor
Dim d1, d2 As Date
Dim ts As TimeSpan

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

MsgBox("Differe nce is: " & ts.TotalMinutes & " minutes")
Dec 24 '07 #4
On Dec 24, 5:16*am, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
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
strFirstShiftEn d 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(strFirst ShiftEnd,
"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(Inpu tBox("enter a date"))
* * * d2 = Now
* * * ts = d1.Subtract(d2)

* * * MsgBox("Differe nce 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
Dec 25 '07 #5
"cmdolcet69 " <co************ @hotmail.comsch rieb

Dim d1, d2 As Date
Dim ts As TimeSpan

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

MsgBox("Differe nce 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.
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.Numeric UpDown1.Value)
minutes = CInt(Me.Numeric UpDown2.Value)

If Me.ComboBox1.Se lectedIndex = 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

Dec 26 '07 #6
On Dec 26, 12:34*am, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb


Dim d1, d2 As Date
Dim ts As TimeSpan
d1 = Date.Parse(Inpu tBox("enter a date"))
d2 = Now
ts = d1.Subtract(d2)
MsgBox("Differe nce 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.

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.Numeric UpDown1.Value)
* * * minutes = CInt(Me.Numeric UpDown2.Value)

* * * If Me.ComboBox1.Se lectedIndex = 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- 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?
Dec 26 '07 #7
"cmdolcet69 " <co************ @hotmail.comsch rieb
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- 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?
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
Dec 27 '07 #8
On Dec 27, 5:07*am, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb


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- 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?

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....howe ver 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.
Dec 27 '07 #9
"cmdolcet69 " <co************ @hotmail.comsch rieb
>
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.Numeric UpDown1.Value)
minutes = CInt(Me.Numeric UpDown2.Value)

If Me.ComboBox1.Se lectedIndex = 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.Ti meOfDay. 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

Jan 2 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7160
by: News Central | last post by:
To all! I use the DateDiff function using VB6 and get this error 'Wrong number of argument or invalid property assignment' ... have anyone seen this problem? thanks ....
7
1917
by: Drago | last post by:
Hi, I got the next question, I want to know the diference between two dates and thats is really easy, but my problem is get that diference in the following format ex. "the diference is= 0 year, 9 months, 11 days" var1 = 04/04/1999 var2 = 01/11/2000 Response.Write("var1 to var2 is " & DateDiff("d", var1, var2) & " days
6
16521
by: Lofty | last post by:
Hi all. I have to write an app that interacts with mySQL (I really must have done some evil, evil stuff in a previous life to be landed with this!) I need to work out the difference in days between values in the database and the current date. "No problem," thought I , "I'll just use the SQL DATEDIFF command." Heh! Well, the user interface I'm using didn't even recognise DATEDIFF as being a function, so I decided to visit the mySQL...
1
8009
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days until completion: DateDiff("d",,) is TODAY the appropriate syntax for today's date? Could the function/expression even be set up that way, or do I need to do something else to get 'days until completion'? (My Access manuals do not mention a TODAY...
4
11920
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS STARTINGDATE1 ENDINGDATE1 STARTINGDATE2
1
4985
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a blank. I've tried; > >DateDiff("y",-4,DateIn) and get errors > >Please any assistance would be greatly appreciated. >
5
6693
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than Access. I have a database where they customer wants to purge records from certain tables after three years. When I was asked to make the changes I originally thought this was an incredibly simple thing to do. I looked at the function list and...
7
15509
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
6
7651
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
2
2782
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error like datediff(mm,date1,date2) datediff(mmm,date1,date2) datediff(month,date1,date2) datediff("mm",date1,date2) datediff("mmm",date1,date2) datediff("month",date1,date2)
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.