Hello,
I have a problem implementing datediff function in script with the
input from txt file, where the data is in the followingformat:
10:00
08:01

I need to subtruct the timelast-timeprev, where the timelast is the
first line in the file and
timeprev is the second line in the file. Here is the script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\timerun.txt", 1)
timeLast = objFile.Read(6)
'wscript.echo timeLast
timePrev = objFile.Read(10)
' wscript.echo timePrev
timeDiff= Round((DateDiff("n", timeprev, timelast)/60),2)
'timeDiff= (DateDiff("n", timeprev, timelast)/60)
timeDiff2= DateDiff("n", timeprev, timelast)
'wscript.echo timeDiff

If timeDiff2 < 60 then
mytime =timediff2
InfoMB=MsgBox ("result:"&Space(1) &mytime &Space(1)
&"minutid",vbInformation + vbOKCancel, "test")
wscript.echo "result:" &Space(1) &mytime &Space(1) &"min"


Do you have any suggestions?
Thanks.
Vitali.

Re: Datediff by McKirahan

McKirahan
Mon Sep 11 09:17:25 CDT 2006

<korn@hot.ee> wrote in message
news:1157981275.427996.298480@m73g2000cwd.googlegroups.com...
> Hello,
> I have a problem implementing datediff function in script with the
> input from txt file, where the data is in the followingformat:
> 10:00
> 08:01
>
> I need to subtruct the timelast-timeprev, where the timelast is the
> first line in the file and
> timeprev is the second line in the file. Here is the script:
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("c:\timerun.txt", 1)
> timeLast = objFile.Read(6)
> 'wscript.echo timeLast
> timePrev = objFile.Read(10)
> ' wscript.echo timePrev

[snip]

Before we worry about calculating a time difference
let's ensure that the two time values are available.

If your text file contains two lines with:
10:00
08:01
Why are you reading the number of characters
(and not allowing for a CrLf) rather than using the
ReadLine method?


Try this. Watch for word-wrap.

Option Explicit
Const cOTF = "C:\timerun.txt"
Dim timeLast, timePrev, timeDiff
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile
Set objFile = objFSO.OpenTextFile(cOTF, 1)
timeLast = objFile.ReadLine
timePrev = objFile.ReadLine
Set objFile = Nothing
Set objFSO = Nothing
timeDiff = Round((DateDiff("n", timeprev, timelast)/60),2)
WScript.Echo timeDiff & " hours difference between " & timeLast & " and " &
timePrev



Re: Datediff by \

\
Mon Sep 11 18:11:04 CDT 2006

I would also recommend massaging the raw data with the cDate() function before
doing the subtraction. It may not be essential, but it can't hurt.

timeLast = cDate(objFile.ReadLine)
timePrev = cDate(objFile.ReadLine)
--
Crash

"I don't make jokes -- I just watch the government and report the facts."
~ Will Rogers ~



Re: Datediff by korn

korn
Tue Sep 12 01:28:23 CDT 2006

Thanks for the reply.

The data in timerun.txt is always in the same format, 2 time readings
(they were taken from the event log). Reading characters also works the
same as readline does.
If the first time reading is 10:00 and the second is 08:01
then timediff subtracts and the result is 1, 98 , whereas the right is
1hr59min
How is it possible to get the right time subtraction?

Vitali.

McKirahan =D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0):

> <korn@hot.ee> wrote in message
> news:1157981275.427996.298480@m73g2000cwd.googlegroups.com...
> > Hello,
> > I have a problem implementing datediff function in script with the
> > input from txt file, where the data is in the followingformat:
> > 10:00
> > 08:01
> >
> > I need to subtruct the timelast-timeprev, where the timelast is the
> > first line in the file and
> > timeprev is the second line in the file. Here is the script:
> >
> > Set objFSO =3D CreateObject("Scripting.FileSystemObject")
> > Set objFile =3D objFSO.OpenTextFile("c:\timerun.txt", 1)
> > timeLast =3D objFile.Read(6)
> > 'wscript.echo timeLast
> > timePrev =3D objFile.Read(10)
> > ' wscript.echo timePrev
>
> [snip]
>
> Before we worry about calculating a time difference
> let's ensure that the two time values are available.
>
> If your text file contains two lines with:
> 10:00
> 08:01
> Why are you reading the number of characters
> (and not allowing for a CrLf) rather than using the
> ReadLine method?
>
>
> Try this. Watch for word-wrap.
>
> Option Explicit
> Const cOTF =3D "C:\timerun.txt"
> Dim timeLast, timePrev, timeDiff
> Dim objFSO
> Set objFSO =3D CreateObject("Scripting.FileSystemObject")
> Dim objFile
> Set objFile =3D objFSO.OpenTextFile(cOTF, 1)
> timeLast =3D objFile.ReadLine
> timePrev =3D objFile.ReadLine
> Set objFile =3D Nothing
> Set objFSO =3D Nothing
> timeDiff =3D Round((DateDiff("n", timeprev, timelast)/60),2)
> WScript.Echo timeDiff & " hours difference between " & timeLast & " and "=
&
> timePrev


Re: Datediff by McKirahan

McKirahan
Tue Sep 12 08:50:34 CDT 2006

<korn@hot.ee> wrote in message
news:1158042503.382917.213040@m73g2000cwd.googlegroups.com...
> Thanks for the reply.
>
> The data in timerun.txt is always in the same format, 2 time readings
> (they were taken from the event log). Reading characters also works the
> same as readline does.
> If the first time reading is 10:00 and the second is 08:01
> then timediff subtracts and the result is 1, 98 ,
> whereas the right is 1hr59min
> How is it possible to get the right time subtraction?


It would have helped if you shown the result you got
and the result you wanted in your original post.

First determine the number of hours difference
then determine the number of minutes difference.

Try this. Watch for word-wrap.

Option Explicit

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile
Set objFile = objFSO.OpenTextFile("timerun.txt", 1)
Dim timeLast
timeLast = CDate(objFile.ReadLine)
Dim timePrev
timePrev = CDate(objFile.ReadLine)
Set objFile = Nothing
Set objFSO = Nothing

Dim minsDiff
minsDiff = DateDiff("n", timeprev, timelast)
Dim hourDiff
If minsDiff > 60 Then
hourDiff = minsDiff \ 60
minsDiff = minsDiff - (hourDiff * 60)
End If

Dim timeDiff
timeDiff = hourDiff & "hr" & minsDiff & "min"
wscript.echo timeDiff & " difference between " & timeLast & " and " &
timePrev



Re: Datediff by \

\
Tue Sep 12 12:21:11 CDT 2006

A variation on your plan:

diff= DateDiff("n", timeprev, timelast)
timeDiff=int(diff/60) & ":" & right("0" & diff mod 60,2)

With the sample input, this will yield 1:59
--
Crash

"The Good Old Days weren't."
~ Unknown source ~



Re: Datediff by Dr

Dr
Wed Sep 13 09:37:14 CDT 2006

JRS: In article <1157981275.427996.298480@m73g2000cwd.googlegroups.com>,
dated Mon, 11 Sep 2006 06:27:55 remote, seen in news:microsoft.public.sc
ripting.vbscript, korn@hot.ee posted :

>I have a problem implementing datediff function in script with the
>input from txt file, where the data is in the followingformat:
>10:00
>08:01

You don't need DateDiff.

Firstly, verify that you are in fact extracting the correct strings from
your file. Then, consider

D1 = CDate("10:00")
D2 = CDate("08:01")
DD = CDate(D1 - D2)
wscript.echo DD, " : ", Left(DD, 5)

which gives me
01:59:00 : 01:59

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Re: Datediff by korn

korn
Thu Sep 14 05:12:44 CDT 2006

Thanks it works great.
Another question is how can i add 2 numbers, which i get from the text
file.
...
timeLast = cDate(objFile.ReadLine)
timePrev = cDate(objFile.ReadLine)
timeadd=(timeast+timeprev)

this does not work, how can i treat these readings as Integer?

Thanks.

"Crash" Dummy wrote:
> A variation on your plan:
>
> diff= DateDiff("n", timeprev, timelast)
> timeDiff=int(diff/60) & ":" & right("0" & diff mod 60,2)
>
> With the sample input, this will yield 1:59
> --
> Crash
>
> "The Good Old Days weren't."
> ~ Unknown source ~


Re: Datediff by korn

korn
Thu Sep 14 05:19:42 CDT 2006

I've got it , its the CInt.

Thanks to all.

korn@hot.ee wrote:
> Thanks it works great.
> Another question is how can i add 2 numbers, which i get from the text
> file.
> ...
> timeLast = cDate(objFile.ReadLine)
> timePrev = cDate(objFile.ReadLine)
> timeadd=(timeast+timeprev)
>
> this does not work, how can i treat these readings as Integer?
>
> Thanks.
>
> "Crash" Dummy wrote:
> > A variation on your plan:
> >
> > diff= DateDiff("n", timeprev, timelast)
> > timeDiff=int(diff/60) & ":" & right("0" & diff mod 60,2)
> >
> > With the sample input, this will yield 1:59
> > --
> > Crash
> >
> > "The Good Old Days weren't."
> > ~ Unknown source ~


Re: Datediff by korn

korn
Tue Oct 10 06:28:20 CDT 2006

Hello,

How is it possible to subtract time value 00:51 - 22:58 ? I've tried
the suggested ways with DateDiff function and CDate function, but the
result is wrong.
The script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("timerun.txt", 1)

timeLast = CDate(objFile.ReadLine)
timePrev = CDate(objFile.ReadLine)

diff= DateDiff("n", timeprev, timelast)
timeDiff=int(diff/60) & ":" & right("0" & diff mod 60,2)

wscript.echo timediff

Vitali.




korn@hot.ee wrote:
> I've got it , its the CInt.
>
> Thanks to all.
>
> korn@hot.ee wrote:
> > Thanks it works great.
> > Another question is how can i add 2 numbers, which i get from the text
> > file.
> > ...
> > timeLast = cDate(objFile.ReadLine)
> > timePrev = cDate(objFile.ReadLine)
> > timeadd=(timeast+timeprev)
> >
> > this does not work, how can i treat these readings as Integer?
> >
> > Thanks.
> >
> > "Crash" Dummy wrote:
> > > A variation on your plan:
> > >
> > > diff= DateDiff("n", timeprev, timelast)
> > > timeDiff=int(diff/60) & ":" & right("0" & diff mod 60,2)
> > >
> > > With the sample input, this will yield 1:59
> > > --
> > > Crash
> > >
> > > "The Good Old Days weren't."
> > > ~ Unknown source ~


Re: Datediff by Evertjan

Evertjan
Tue Oct 10 07:21:46 CDT 2006

wrote on 10 okt 2006 in microsoft.public.scripting.vbscript:

> How is it possible to subtract time value 00:51 - 22:58 ?

You cannot, since it is not clear if the result is:

- 1:53
- 25:53
- 49:53
or even
+ 22.07

Normally in time substraction "same day" is understood,
so here one has to take some extra measures:

========================
d1 = "00:51"
d2 = "22:58"

d1s = CDate(d1)
d2s = CDate(d2)
dr = d1s - d2s
sg = ""
if dr<0 then
dr=dr+1
sg = "-"
end if
drd = CDate(dr)
Response.write sg & drd
=============================

working with DateDiff() is another possiblilty

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: Datediff by korn

korn
Wed Oct 11 02:47:13 CDT 2006

Thanks it works great!.
Vitali.

Evertjan. wrote:
> wrote on 10 okt 2006 in microsoft.public.scripting.vbscript:
>
> > How is it possible to subtract time value 00:51 - 22:58 ?
>
> You cannot, since it is not clear if the result is:
>
> - 1:53
> - 25:53
> - 49:53
> or even
> + 22.07
>
> Normally in time substraction "same day" is understood,
> so here one has to take some extra measures:
>
> ========================
> d1 = "00:51"
> d2 = "22:58"
>
> d1s = CDate(d1)
> d2s = CDate(d2)
> dr = d1s - d2s
> sg = ""
> if dr<0 then
> dr=dr+1
> sg = "-"
> end if
> drd = CDate(dr)
> Response.write sg & drd
> =============================
>
> working with DateDiff() is another possiblilty
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)