Using DateDiff 
Author Message
 Using DateDiff

I know I can Google this, and have, but am up against a deadline, and I'm
not sure how to "convert" text dates such that they can be used with
DateDiff. I have two dates, output by a batch file, in the format:

Time1 = "Fri 01/22/2010 16:18:17.37"
Time2 = "Sun 01/24/2010 04:32:11.33"

I want to calculate the difference between these, with something like:

TimeElapsed = DateDiff("h", Time1, Time2)

and I need the time thrown in there, not just considering dates. I think I
have to convert Time1 and Time2 to some other type of value first, but am
not sure. I figure DateValue and TimeValue will be used in there, but
haven't gotten the syntax quite right yet.

Could someone steer me in the right direction?

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net



Sat, 14 Jul 2012 04:04:53 GMT  
 Using DateDiff


Quote:
>I know I can Google this, and have, but am up against a deadline, and I'm
>not sure how to "convert" text dates such that they can be used with
>DateDiff. I have two dates, output by a batch file, in the format:

> Time1 = "Fri 01/22/2010 16:18:17.37"
> Time2 = "Sun 01/24/2010 04:32:11.33"

> I want to calculate the difference between these, with something like:

> TimeElapsed = DateDiff("h", Time1, Time2)

> and I need the time thrown in there, not just considering dates. I think I
> have to convert Time1 and Time2 to some other type of value first, but am
> not sure. I figure DateValue and TimeValue will be used in there, but
> haven't gotten the syntax quite right yet.

> Could someone steer me in the right direction?

Use CDate() first to convert the String to Date. This will also convert the
time part for you.


Sat, 14 Jul 2012 05:13:21 GMT  
 Using DateDiff
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in

Quote:
> I know I can Google this, and have, but am up against a deadline, and
> I'm not sure how to "convert" text dates such that they can be used
> with DateDiff. I have two dates, output by a batch file, in the
> format:

> Time1 = "Fri 01/22/2010 16:18:17.37"
> Time2 = "Sun 01/24/2010 04:32:11.33"

> I want to calculate the difference between these, with something like:

> TimeElapsed = DateDiff("h", Time1, Time2)

> and I need the time thrown in there, not just considering dates. I
> think I have to convert Time1 and Time2 to some other type of value
> first, but am not sure. I figure DateValue and TimeValue will be used
> in there, but haven't gotten the syntax quite right yet.

> Could someone steer me in the right direction?

To add to nobody's post, adding a CDate() to each also requires removing
the day designation, and the fractional seconds.


Sat, 14 Jul 2012 05:28:24 GMT  
 Using DateDiff


Quote:
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in

>> I know I can Google this, and have, but am up against a deadline, and
>> I'm not sure how to "convert" text dates such that they can be used
>> with DateDiff. I have two dates, output by a batch file, in the
>> format:

>> Time1 = "Fri 01/22/2010 16:18:17.37"
>> Time2 = "Sun 01/24/2010 04:32:11.33"

>> I want to calculate the difference between these, with something like:

>> TimeElapsed = DateDiff("h", Time1, Time2)

>> and I need the time thrown in there, not just considering dates. I
>> think I have to convert Time1 and Time2 to some other type of value
>> first, but am not sure. I figure DateValue and TimeValue will be used
>> in there, but haven't gotten the syntax quite right yet.

>> Could someone steer me in the right direction?

> To add to nobody's post, adding a CDate() to each also requires removing
> the day designation, and the fractional seconds.

and you have to be careful about the locale settings or when you get to
"02/01/2010" you might find it being treatd as Jan 2 instead of Feb 1.  You
could pull out the date segments and use CInt and DateSerial to avoid that
potential pitfall.


Sat, 14 Jul 2012 05:54:18 GMT  
 Using DateDiff

Quote:

>> Time1 = "Fri 01/22/2010 16:18:17.37"
>> Time2 = "Sun 01/24/2010 04:32:11.33"
>Use CDate() first to convert the String to Date. This will also convert the
>time part for you.

When has CDate() converted a Unix format date to a Windows format date?  You
might try the technique first before posting it as an answer.

First you have to lop off the day name.  Then, you have to lop off the time
zone at the end if it's a typical Unix/Linux date format containing "GMT",
"UTC", "EST", etc., and then convert it into the time zone of choice.  I know
that the given example doesn't have the zone on it, but the format is Unix,
and eventually they'll probably see such a beast.



Sat, 14 Jul 2012 06:24:05 GMT  
 Using DateDiff


Quote:

>>> Time1 = "Fri 01/22/2010 16:18:17.37"
>>> Time2 = "Sun 01/24/2010 04:32:11.33"

>>Use CDate() first to convert the String to Date. This will also convert
>>the
>>time part for you.

> When has CDate() converted a Unix format date to a Windows format date?
> You
> might try the technique first before posting it as an answer.

> First you have to lop off the day name.  Then, you have to lop off the
> time
> zone at the end if it's a typical Unix/Linux date format containing "GMT",
> "UTC", "EST", etc., and then convert it into the time zone of choice.  I
> know
> that the given example doesn't have the zone on it, but the format is
> Unix,
> and eventually they'll probably see such a beast.

Actually, the format is Windows. DOS window, batch file Windows, but
Windows. But you're right, there are no ready tools to convert the
day/date/time format that Windows produces using Windows (VB6, anyhow)
functions. So, I made my own function, which:

Lops off the day of the week.
Uses DateDiff to calculate the difference in days (then convert to hours)
Parse the Hours, Minutes and Seconds manually, converting to hours or
whatever is desired.

I just figured that if Windows produces that format, there ought to be a
standard way to extract the info to get DateDiff. Guess not. ;-)

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net



Sat, 14 Jul 2012 20:58:26 GMT  
 Using DateDiff

Quote:
> then press Ctrl+J and try the various options until you get one that takes
> a string input then you'll get your homework finished.

He's not a student. Rick's been posting here for quite a while.


Sun, 15 Jul 2012 00:29:14 GMT  
 Using DateDiff

Quote:
> and you have to be careful about the locale settings or when you get to
> "02/01/2010" you might find it being treatd as Jan 2 instead of Feb 1.
> You could pull out the date segments and use CInt and DateSerial to avoid
> that potential pitfall.

Hasn't anybody written a generic "date-ripping" class/module? You know,
where you can give it a string and have it either A) do a best guess or B)
provide a format string to tell it how to parse the date? Seems like it
would be a useful thing.


Sun, 15 Jul 2012 00:31:07 GMT  
 Using DateDiff

Quote:


>> and you have to be careful about the locale settings or when you get to
>> "02/01/2010" you might find it being treatd as Jan 2 instead of Feb 1.
>> You could pull out the date segments and use CInt and DateSerial to avoid
>> that potential pitfall.

> Hasn't anybody written a generic "date-ripping" class/module? You know,
> where you can give it a string and have it either A) do a best guess or B)
> provide a format string to tell it how to parse the date? Seems like it
> would be a useful thing.

I did the later. Google for ConvertAnyDateFormat().

http://groups.google.com/group/microsoft.public.vb.general.discussion...



Sun, 15 Jul 2012 00:47:50 GMT  
 Using DateDiff

Quote:


>> and you have to be careful about the locale settings or when you get to
>> "02/01/2010" you might find it being treatd as Jan 2 instead of Feb 1.
>> You could pull out the date segments and use CInt and DateSerial to avoid
>> that potential pitfall.

> Hasn't anybody written a generic "date-ripping" class/module? You know,
> where you can give it a string and have it either A) do a best guess or B)
> provide a format string to tell it how to parse the date? Seems like it
> would be a useful thing.

I did, but sadly, it's not my code to give away.
It steps through the format string looking for changes in the characters
(or quotes) then at each change, extracts that part from the source string.
It then just uses Date/TimeSerial to reassemble the components at the end.

--

i-Catcher Development Team

iCode Systems



Tue, 17 Jul 2012 17:40:50 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Using DateDiff to get decimal ages

2. Calculating Time Difference using DateDiff Function in VBScript

3. Using DateDiff to calculate differences in minutes

4. Using Datediff

5. vbscript newbie needs help using DateDiff - getting headache tryi ng to figure out logic

6. VBRUN300.dll crashes using DateDiff

7. Using DATEDIFF in Crystal Rpts 7.0

8. DateDiff using multiple controls....hhheeelllppp PLEASE.

9. Using the DateDiff function

10. DateDiff programming problem

11. Help with DateDiff function

12. datediff

 

 
Powered by phpBB® Forum Software