TheSwamp

Code Red => VB(A) => Topic started by: Amsterdammed on August 23, 2005, 11:19:56 AM

Title: Compare Now and a date and time in String format
Post by: Amsterdammed on August 23, 2005, 11:19:56 AM
Hello VBA experts,

I need to compare a start time and date , with is written in  a string with the time and date provided by the “now” function. The Date is in European format.

So I have as example “23-8-2005 10:21:05” as a string expression and want it to compare with now,  23-8-2005 11:16:08.

When I try to format the string with Cdate I get an error with type mismatch.

What don’t I see here? :?:  :?:

Thanks in advance,

Bernd
Title: Compare Now and a date and time in String format
Post by: Swift on August 23, 2005, 11:25:05 AM
Hey Berend
I don't have time to work this out right now but I THINK you'll have to split the text into a date and a time and then compare the two.
Title: Compare Now and a date and time in String format
Post by: Keith™ on August 23, 2005, 11:33:13 AM
You can compare the two, but it is hard to give you a solution when I don't know exactly what you want to have as a result ... do you want a difference in the two?
Title: Compare Now and a date and time in String format
Post by: Amsterdammed on August 23, 2005, 11:39:49 AM
You are right,  Keith

I should have add this line

m = DateDiff("n", logdur, Now)

Logdur is the string “23-8-2005 10:21:05”

M will be the time difference in minutes. Later I convert it in hours and minutes as User information in a UserForm..
Title: Compare Now and a date and time in String format
Post by: jjs on August 23, 2005, 11:40:14 AM
use the format function.
Code: [Select]

function comparetimeanddate(strtimedategiven as string) as boolean
strNow = now
strDateNow=format (strnow, MM/DD/YY)
strTimeNow=Format(strnow, HH:MM)
strDategiven=format (strtimedategiven, MM/DD/YY)
strTimegiven=Format(strtimedategiven, HH:MM)

if strtimenow=strtimegiven and strdatenow=strdategiven then
bolequal=true
end if

comparetimeanddate=bolequal

end function

i made that all up. but it should be close i think
Title: Compare Now and a date and time in String format
Post by: jjs on August 23, 2005, 11:42:18 AM
well, if you need the difference, that would be easy enough to change also.

function ... as string


get the difference between the days, then the times,


then return it as a string
Title: Compare Now and a date and time in String format
Post by: Amsterdammed on August 23, 2005, 12:49:05 PM
Sorry JJs

but
Code: [Select]
strTimeNow=Format(strnow, HH:MM)
gives a syntax error
Title: Compare Now and a date and time in String format
Post by: jjs on August 23, 2005, 01:25:55 PM
you will have to look up how the format function works and what you need to pass to it. i was providing pseudo code that was pretty close to real code.
Title: Compare Now and a date and time in String format
Post by: Amsterdammed on August 23, 2005, 01:49:58 PM
OK
Title: Compare Now and a date and time in String format
Post by: Keith™ on August 23, 2005, 01:52:41 PM
I tested this and it works fine .. you are missing some quotes ...

Code: [Select]

strTimeNow = Format$(Now, "HH:MM")
Title: Compare Now and a date and time in String format
Post by: Amsterdammed on August 23, 2005, 02:27:56 PM
That works Keith,

But i still don't know how to get the difference between the times in HH:MM format :?:
Title: Compare Now and a date and time in String format
Post by: jjs on August 23, 2005, 02:31:57 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp
Title: Compare Now and a date and time in String format
Post by: Amsterdammed on August 25, 2005, 10:36:49 AM
Thanks, Gentlemen