I want to calculate time difference in vb6. i had been done it in vb.net. but i don't know how to convert in vb6.Because the datetime type will not be available in vb6.
i have pasted my vb.net code below. how to do the same in vb6?
Public Sub timecal()
Dim dFrom As DateTime
Dim dTo As DateTime
Dim tempstarttime As DateTime
Dim idletime As String = "00:05:00"
Dim Needtosub As String = "00:01:00"
Dim timeDiff As String
If DateTime.TryParse(start_time, dFrom) AndAlso DateTime.TryParse(end_time, dTo) Then
Dim TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim secs As Integer = TS.Seconds
timeDiff = ((hour.ToString("00") + ":") + mins.ToString("00") + ":") + secs.ToString("00")
sscheck1 = False
If timeDiff >= idletime Then
tempstarttime = System.DateTime.Parse(end_time)
tempstarttime = tempstarttime.AddMinutes(-1)
start_time = Strings.Format(tempstarttime, "yyyy-MM-dd HH:mm:ss")
sscheck1 = True
End If
End If
End Sub
You can calculate the time difference between two Date values (which will include the time) using the DateDiff function
For difference in minutes:
Dim lMinutes as Long
lMinutes = DateDiff("n", dFrom, dTo)
For difference in seconds:
Dim lSeconds as Long
lSeconds = DateDiff("s", dFrom, dTo)
For difference in hours:
Dim lHours as Long
lHours = DateDiff("h", dFrom, dTo)
For the purposes of getting your string time diff in hours:minutes:seconds, I would do:
Dim lSeconds as Long, lMinutes as Long
lSeconds = DateDiff("s", dFrom, dTo)
lMinutes = Fix(lSeconds / 60) '// Gets the whole number (not rounded)
lSeconds = ((lSeconds / 60) - lMinutes) * 60 '// get the remaining seconds
sTimeDiff = "00:" & Format$(lMinutes, "00") & ":" & Format$(lSeconds, "00")
Note: If lMinutes is greater than 60, you'll need to do similar math to pull out the hours portion before pulling the minutes and seconds. This code assumes your timespan is less than an hour based on your example.
use vb DateDiff Function i.e
Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)
or can be done with another general way
Dim dtStart As Long
Dim dtEnd As Long
Dim result As Long
Dim i As Integer
dtStart = GetTickCount
dtEnd = GetTickCount
result = dtEnd – dtStart // miliseconds
OutAddIn.objApptItem.Start = CDbl(result) // miliseconds to date
Related
I'm creating a payroll system I want to calculate the minute late of Employee using two dtpicker
Dtpicker1 is for time in and Dtpicker2 for Timeout
Private Sub calc_Click()
oras = DateDiff("n", DTPicker1, DTPicker2)
Text1.Text = oras
End sub
If all employees are working the same amount of hours (8 hours/day for example):
Private Sub calc_Click()
Dim iWorkdayHours As Integer
Dim iMinutesWorked As Integer
Dim iMinutesLate As Integer
' Get the amount of minutes between two dates
iMinutesWorked = DateDiff("n", DTPicker1, DTPicker2)
' Get number of hours employee should have worked
iWorkdayHours = 8
iMinutesLate = (iWorkdayHours * 60) - iMinutesWorked
If iMinutesLate > 0 Then
Text1.Text = iMinutesLate & " minutes late."
Else
Text1.Text = "On time."
End If
End Sub
If employees have different shift lengths, you can update iWorkdayHours.
I want to divide a string into three parts. I am using following code.
dim length1 as string
dim length2 as string
dim lenght3 as string
length1=Mid$(Text1.text,1,30)
length2=Mid$(Text1.text,31,70)
length3=Mid$(Text1.text,71,100)
msgbox length1
msgbox lenght2
msgbox length3
msgbox 2 show me the length of 11,30. Why?
What I have tried:
What have wrong with my code? I know that Mid$ start at the left of the string.
I am assuming that you are wanting each string to be 10 characters long? Your problem appears to be that you keep changing the length of the string by 10.
Edit: Since OP provided more information in his comments, I updated code to accommodate request.
Sub Test()
Dim length1 As String
Dim length2 As String
Dim length3 As String
Dim divTot As Integer, leftOver As Integer
divTot = Len(text1.Text) / 3
leftOver = Len(text1.Text) - (divTot * 2)
length1 = Mid$(text1.Text, 1, divTot)
length2 = Mid$(text1.Text, divTot + 1, divTot)
length3 = Mid$(Text1.Text, (divTot * 2) + 1, leftOver)
MsgBox length1
MsgBox length2
MsgBox length3
End Sub
I'm doing an assignment for my class called "Risk!", the basis of it is that you start with 1000 points, and input a number to risk. You roll 2 dice. If it's even, you lose and the input is removed from score. If it's odd, you win and input is added to score. For some reason, the score isn't displayed correctly.
Private Sub cmdQuit_Click()
Unload Me
End Sub
Private Sub cmdRollDice_Click()
intNumOutput1 = Int(Rnd * 6) + 1
intNumOutput2 = Int(Rnd * 6) + 1
lblNumOutput1.Caption = intNumOutput1
lblNumOutput2.Caption = intNumOutput2
intBothOutputs = intNumOutput1 + intNumOutput2
If intBothOutputs Mod 2 > 0 Then
intScore = intScore + intNumInput
MsgBox "odd, win"
Else
intScore = intScore - intNumInput
MsgBox "even, lose"
End If
lblTotal.Caption = "Your new point total is " & intScore
End Sub
Private Sub Form_Load()
Randomize
Dim intScore As Integer
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
Dim intNumInput As Integer
txtNumInput.Text = intNumInput
intScore = 1000
txtNumInput.Text = ""
lblNumOutput1.Caption = ""
lblNumOutput2.Caption = ""
End Sub
When you want to use variables in more than one method (e.g. sub, function), you declare the variables outside of any method.
Now, since you declared your variables inside Form_Load, you can't use them in cmdRollDice_Click or in any other method. So, what happens when you use them in a method other than the one they were declared in? Well, if you have Option Explicit statement on top of your code, you'll get a run-time error. If you don't (which is your current case), the variables will get initialized -with zero value- each time the method is called (note: they're now not the same variables that were declared in Form_Load).
Hence, you need to declare your variables on top of your file (before all functions/subs) like the following:
Dim intScore As Integer
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
Dim intNumInput As Integer
' The rest of your code
Private Sub Form_Load()
End Sub
Private Sub cmdRollDice_Click()
End Sub
'
'
So, as a rule: you declare variables inside a method ONLY if you don't need to use them outside that method.
For more information about this, read Understanding the Scope of Variables
Hope that helps :)
For string concatenation its best practice to convert data types to string using cstr. e.g. CStr(intScore)
Add the event handler for txtNumInput. You have not assigned the value to intNumInput whenever button is clicked.
Try below.
Option Explicit
Private intScore As Integer
Private intNumOutput1 As Integer
Private intNumOutput2 As Integer
Private intBothOutputs As Integer
Private intNumInput As Integer
Private Sub cmdRollDice_Click()
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
intNumOutput1 = Int(Rnd * 6) + 1
intNumOutput2 = Int(Rnd * 6) + 1
lblNumOutput1.Caption = intNumOutput1
lblNumOutput2.Caption = intNumOutput2
intBothOutputs = intNumOutput1 + intNumOutput2
If intBothOutputs Mod 2 > 0 Then
intScore = intScore + intNumInput
MsgBox "odd, win"
Else
intScore = intScore - intNumInput
MsgBox "even, lose"
End If
lblTotal.Caption = "Your new point total is " & CStr(intScore)
End Sub
Private Sub txtNumInput_Change()
If IsNumeric(txtNumInput.Text) Then
intNumInput = CInt(txtNumInput.Text)
End If
End Sub
all files from my source file location has been copied in
destination folder.
how can i copy then delete the 2 files only if my
datefrom = 12/07/2014 and my
dateto = 12/09/2014.
filename: date modified
file1 12/07/2014
file2 12/09/2014
file3 01/06/2015
this is my code.
Dim ObjFso
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim StrSourceLocation
Dim StrDestinationLocation
Dim StrSourceFileName
dim SourceLocation
dim DestinationLocation
dim datefrom, dateto, datectr
datefrom = DateValue("01/01/2015")
dateto = DateValue("01/01/2015")
datefrom = InputBox ("date from: (mm/dd/yyyy)")
dateto = InputBox ("date to: (mm/dd/yyyy)")
StrSourceLocation = InputBox ("source:")
StrDestinationLocation = InputBox ("destination:")
datectr = datefrom
dateto = dateto
intYear=0
intMonth=0
indDay=0
intFileCtr=0
intFileDayCtr=0
SourceLocation = StrSourceLocation + "\*.*"
DestinationLocation = StrDestinationLocation + "\"
Const OverwriteExisting = True
Do Until datectr>dateto
intYear=Year(datectr)
intMonth=Month(datectr)
intDay=Day(datectr)
for each f in objFSO.Getfolder(StrSourceLocation).Files
if Year(f.DateLastModified) = intYear And Month(f.DatelastModified) = intMonth And Day(f.DateLastModified) = intDay Then
objFSO.CopyFile SourceLocation , DestinationLocation, OverwriteExisting
objFSO.DeleteFile(SourceLocation), True
end if
Next
datectr=DateAdd("d", 1, datectr)
datectr = DateValue(datectr)
Loop
I have no idea what you are doing. Dates are simple in VBS.
If DateValue("01/01/1980") < DateValue("01/01/1990") or DateValue("01/01/1980") > DateValue("01/01/1960") then
' blah
End If
Is how to check dates within a range.
There is also the similar DateSerial. Dates are numbers, x days since a day in the past (01/01/100 AD) with time being a a fraction which is a fraction of a day (12 pm is 0.5).
label1 displays the last transaction date/time which I get from a database through a query. label2 is the system date/time. I have a timer that executes a command button after which I want to check if the date/time in label1 is smaller than 5 minutes. If so then I want to show a massage.
But I don’t know why my code is failing to perform this function.
Any help will be much appreciated.
Private Sub Command1_Click()
Dim date1 As Date
Dim date2 As Date
date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
date2 = Format(label1, "yyyy/mm/dd hh:mm:ss")
If DateDiff("n", date1, date2) < 2 Then
MsgBox ("Not Vending")
End If
End Sub
I've also tried:
Private Sub Command1_Click()
Dim date1 As Date
Dim label1 As Date
date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
date2 = label1
If DateDiff("m", Now, date1) > DateDiff("m", Now, label1) Then
MsgBox ("Not Vending")
End If
End Sub
As well as:
Private Sub Command1_Click()
If DateDiff("n", Now, label1) > 5 Then
MsgBox ("Not Vending")
End If
End Sub
If the date pulled from the DB is earlier than Now, DateDiff will always return a negative number if you pass Now as the second parameter. It looks like you're checking for the passage of time, so I'll assume the dates in DB will always be before Now. You need to switch the order of Now and the date to which it is being compared (DateDiff("n", date1, Now) instead of DateDiff("n", Now, date1).
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
If DateDiff("n", date1, Now) < 5 Then
MsgBox ("Not Vending")
End If
End Sub
An other way without using the DateDiff-function would be direct calculation with the Date-variables - since under the hood these are actually Doubles.
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If (date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
If you want to check the time difference you can also use
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If Abs(date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
Where Abs(date1 - Now) returns the time difference as Date-value
Btw. date1 = Format(Now, "yyyy/mm/dd hh:mm:ss") doesn't make sence. Since Now returns a Date-value, Format converts that Date-value into a String and assigning it to date1 converts that String back to a Date-value ùsing the local system settings for date formats - which means that the program will behave different on different systems.