Sign in to follow this  
Followers 0
derkaderka

Change in time

3 posts in this topic

Hello, I am writing a program that is required to calculate the change in time. The user puts in a start and finish time and gets out the change. I got it to work, however there is a bug. Say the start time is 24 hours and 50 minutes, and the end time is 25 hours and 10 minutes. It should be 20 minutes. However is is saying 1 hour and 20 minutes. Is there any way I can accurately calculate this without having to convert to minutes? Here is an example of what I mean:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("", 503, 167, 192, 124)
$Input1 = GUICtrlCreateInput("", 24, 40, 121, 21)
$Input2 = GUICtrlCreateInput("", 24, 72, 121, 21)
$Input3 = GUICtrlCreateInput("", 264, 40, 121, 21)
$Input4 = GUICtrlCreateInput("", 264, 72, 121, 21)
$Label1 = GUICtrlCreateLabel("Hours", 152, 40, 36, 17)
$Label2 = GUICtrlCreateLabel("Minutes", 152, 72, 36, 17)
$Label3 = GUICtrlCreateLabel("Hours", 392, 40, 36, 17)
$Label4 = GUICtrlCreateLabel("Minutes", 392, 72, 36, 17)
$Button1 = GUICtrlCreateButton("Calculate", 208, 120, 75, 25)
$Group1 = GUICtrlCreateGroup("Start Time", 16, 24, 233, 81)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("End Time", 256, 24, 241, 81)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Calculate()
    EndSwitch
WEnd

Func _Calculate()
    $hourread1 = GUICtrlRead($Input1)
    $hourread2 = GUICtrlRead($Input3)
    $minutesread1 = GUICtrlRead($Input2)
    $minutesread2 = GUICtrlRead($Input4)
    $Form2 = GUICreate("", 294, 89, 302, 218)
    $Input5 = GUICtrlCreateInput("", 8, 16, 121, 21)
    $Input6 = GUICtrlCreateInput("", 8, 56, 121, 21)
    $Label5 = GUICtrlCreateLabel("Hours", 136, 16, 124, 17)
    $Label6 = GUICtrlCreateLabel("Minutes", 136, 56, 124, 17)
    GUISetState(@SW_SHOW)
    $finalhours = $hourread2 - $hourread1
    $minutesfinal = $minutesread2 - $minutesread1
    GUICtrlSetData($Input5, $finalhours)
    If $minutesfinal < 0 Then
        $minutesfinal2 = 60 - $minutesread2
        $minutesfinal3 = $minutesfinal + $minutesread1
        GUICtrlSetData($Input6, $minutesfinal3)
    EndIf
    If $minutesfinal >= 0 Then
        GUICtrlSetData($Input6, $minutesfinal)
    EndIf
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
EndFunc

I know this probably is not the best way to do it but if there is any other way please help me.

Share this post


Link to post
Share on other sites



_DateDiff

Included in "Date.au3" ;)


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello derkaderka,

Try This Calculate() function:

Func _Calculate()
    $hourread1 = GUICtrlRead($Input1)
    $hourread2 = GUICtrlRead($Input3)
    $minutesread1 = GUICtrlRead($Input2)
    $minutesread2 = GUICtrlRead($Input4)
    $Form2 = GUICreate("", 294, 89, 302, 218)
    $Input5 = GUICtrlCreateInput("", 8, 16, 121, 21)
    $Input6 = GUICtrlCreateInput("", 8, 56, 121, 21)
    $Label5 = GUICtrlCreateLabel("Hours", 136, 16, 124, 17)
    $Label6 = GUICtrlCreateLabel("Minutes", 136, 56, 124, 17)
    GUISetState(@SW_SHOW)
    $finalhours = $hourread2 - $hourread1
    If $minutesread1>$minutesread2 Then
        $finalhours -=1
        $minutesread2 +=60
    EndIf
    $minutesfinal = $minutesread2 - $minutesread1
    GUICtrlSetData($Input5, $finalhours)
    GUICtrlSetData($Input6, $minutesfinal)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
EndFunc

Realm

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0