VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Given a number of seconds this code will calculate the date / time in the past and in the future. I

by Dennis Anthony (3 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 20th June 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Given a number of seconds this code will calculate the date / time in the past and in the future. I use this to create time-frames in which

API Declarations


'Put a timer on your form as well as 3 empty labels, a textbox and a command-'button.


Rate Given a number of seconds this code will calculate the date / time in the past and in the future. I




If Text1.Text = "" Then
   MsgBox "Enter a value first", vbExclamation
   Exit Sub
End If

Call Calculate_Date_Time(Label6, Text1.Text, pa_DateTimeLow, pa_DateTimeHigh)

Label4 = pa_DateTimeLow
Label5 = pa_DateTimeHigh
End Sub

Private Sub Timer1_Timer()
    Label6 = Date & " " & Time
End Sub

'****************************************************
'PUT THIS CODE IN A MODULE NAMDE "CALCULATIONS"     *
'****************************************************

Public Function Calculate_Date_Time(ByRef pa_datetime As String, ByRef pa_seconds As String, pa_DateTimeLow, pa_DateTimeHigh)
Dim pa_TimeLow As String
Dim pa_TimeHigh As String
pa_TimeLow = pa_datetime
pa_TimeHigh = pa_datetime

If pa_seconds > 60 Then
    Do While pa_seconds > 60
        Call Calc_TimeLow(pa_TimeLow, 60)
        Call Calc_TimeHigh(pa_TimeHigh, 60)
        pa_seconds = pa_seconds - 60
    Loop
    If pa_seconds > 0 Then
        Call Calc_TimeLow(pa_TimeLow, pa_seconds)
        Call Calc_TimeHigh(pa_TimeHigh, pa_seconds)
    End If
Else
    Call Calc_TimeLow(pa_TimeLow, pa_seconds)
    Call Calc_TimeHigh(pa_TimeHigh, pa_seconds)
End If
pa_DateTimeLow = pa_TimeLow
pa_DateTimeHigh = pa_TimeHigh
End Function

Private Function Calc_TimeLow(pa_TimeLow As String, ByRef pa_seconds As String)
Dim pa_secs As Integer
Dim pa_secs_low As String
Dim pa_mins As Integer
Dim pa_mins_low As String
Dim pa_hours As Integer
Dim pa_hours_low As String
Dim pa_date As Date
Dim pa_date_low As Date

pa_date = Left(pa_TimeLow, Len(pa_TimeLow) - 9)
pa_secs = Right(pa_TimeLow, 2)
pa_mins = Left(Right(pa_TimeLow, 5), 2)
pa_hours = Left(Right(pa_TimeLow, 8), 2)

'calculate date-time-low
If pa_secs - pa_seconds < 0 Then
    If pa_mins - 1 < 0 Then
        If pa_hours - 1 < 0 Then
            pa_secs_low = pa_secs + 60 - pa_seconds
            pa_mins_low = pa_mins + 60 - 1
            pa_hours_low = "23"
            pa_date_low = pa_date - 1
        Else
            pa_secs_low = pa_secs + 60 - pa_seconds
            pa_mins_low = pa_mins + 60 - 1
            pa_hours_low = pa_hours - 1
            pa_date_low = pa_date
        End If
    Else
        pa_secs_low = pa_secs + 60 - pa_seconds
        pa_mins_low = pa_mins - 1
        pa_hours_low = pa_hours
        pa_date_low = pa_date
    End If
Else
    pa_secs_low = pa_secs - pa_seconds
    pa_mins_low = pa_mins
    pa_hours_low = pa_hours
    pa_date_low = pa_date
End If

'set all values to double digits
If Len(pa_secs_low) = 1 Then pa_secs_low = "0" & pa_secs_low
If Len(pa_mins_low) = 1 Then pa_mins_low = "0" & pa_mins_low
If Len(pa_hours_low) = 1 Then pa_hours_low = "0" & pa_hours_low

'Build the returnvariables time-low and time-high
pa_TimeLow = pa_date_low & " " & pa_hours_low & ":" & pa_mins_low & ":" & pa_secs_low

'Clear all variables
pa_secs = 0
pa_secs_low = ""
pa_mins = 0
pa_mins_low = ""
pa_hours = 0
pa_hours_low = ""
pa_date_low = 0
End Function

Private Function Calc_TimeHigh(ByRef pa_TimeHigh As String, ByRef pa_seconds As String)
Dim pa_secs As Integer
Dim pa_secs_high As String
Dim pa_mins As Integer
Dim pa_mins_high As String
Dim pa_hours As Integer
Dim pa_hours_high As String
Dim pa_date As Date
Dim pa_date_high As Date

pa_date = Left(pa_TimeHigh, Len(pa_TimeHigh) - 9)
pa_secs = Right(pa_TimeHigh, 2)
pa_mins = Left(Right(pa_TimeHigh, 5), 2)
pa_hours = Left(Right(pa_TimeHigh, 8), 2)

'Calulate date-time-high
If pa_secs + pa_seconds > 59 Then
    If pa_mins + 1 > 59 Then
        If pa_hours + 1 > 23 Then
            pa_secs_high = pa_secs - 60 + pa_seconds
            pa_mins_high = pa_mins - 60 + 1
            pa_hours_high = "00"
            pa_date_high = pa_date + 1
        Else
            pa_secs_high = pa_secs - 60 + pa_seconds
            pa_mins_high = pa_mins - 60 + 1
            pa_hours_high = pa_hours + 1
            pa_date_high = pa_date
        End If
    Else
        pa_secs_high = pa_secs - 60 + pa_seconds
        pa_mins_high = pa_mins + 1
        pa_hours_high = pa_hours
        pa_date_high = pa_date
    End If
Else
    pa_secs_high = pa_secs + pa_seconds
    pa_mins_high = pa_mins
    pa_hours_high = pa_hours
    pa_date_high = pa_date
End If

'set all values to double digits
If Len(pa_secs_high) = 1 Then pa_secs_high = "0" & pa_secs_high
If Len(pa_mins_high) = 1 Then pa_mins_high = "0" & pa_mins_high
If Len(pa_hours_high) = 1 Then pa_hours_high = "0" & pa_hours_high

'Build the returnvariables time-low and time-high
pa_TimeHigh = pa_date_high & " " & pa_hours_high & ":" & pa_mins_high & ":" & pa_secs_high

'Clear all variables
pa_secs = 0
pa_secs_high = ""
pa_mins = 0
pa_mins_high = ""
pa_hours = 0
pa_hours_high = ""
pa_date_high = 0
End Function


Download this snippet    Add to My Saved Code

Given a number of seconds this code will calculate the date / time in the past and in the future. I Comments

No comments have been posted about Given a number of seconds this code will calculate the date / time in the past and in the future. I. Why not be the first to post a comment about Given a number of seconds this code will calculate the date / time in the past and in the future. I.

Post your comment

Subject:
Message:
0/1000 characters