Hi all!
I'm new in programing and in FreeBasic.
As the subject says, I'm trying to extract into integer variables (or long, or double...) separately the hour, minute and second from the function Time.
I already try with this code:
dim mins as long
dim secs as long
dim hh as long
hh = Val (Mid (Time, 1, 2))
mins = Val (Mid (Time, 4, 2))
secs = Val (Mid(Time,7,2))
print "My output:"
Print hh;"--";mins;"--";secs
Print "Done"
The output is:
My output:
0-- 49-- 24
Done
As you can see, there is an space in all of the values, at the beginning.
Sorry, but I have no idea about how to remove that space and get the number.
Thank you very much for your help.
[SOLVED] Extract hour, minute and second from time function.
[SOLVED] Extract hour, minute and second from time function.
Last edited by Miguindi on Nov 01, 2021 22:38, edited 1 time in total.
-
- Posts: 789
- Joined: Jul 26, 2018 18:28
Re: Extract hour, minute and second from time function.
You can do this:
Code: Select all
Print hh & "-" & mins & "-" & secs
Re: Extract hour, minute and second from time function.
For what is worth, you can use specific functions to retrieve this info if you include the datetime.bi header:
The issue you're having is due to the leading sign. If you want to get rid of that extra space, simply convert the number to a string by using the & operator as Xusinboy suggests above, or the str() function.
Code: Select all
#include once "datetime.bi"
? time()
? hour( now() )
? minute( now() )
? second( now() )
sleep()
Re: Extract hour, minute and second from time function.
Or define 'mins', 'secs', and 'hh', all as ulong.
Re: Extract hour, minute and second from time function.
Thank you very much all of you guys!
I finally make a functions in other file, then I call them from everywhere:
Declare Function GetHour (TimeNow As String) As uLong
Declare Function GetMins (TimeNow As String) As uLong
Declare Function GetSecs (TimeNow As String) As uLong
Function GetHour(TimeNow As String) as uLong
Dim hh_fn as uLong
hh_fn = ValInt(Left(TimeNow, 2))
Return hh_fn
End Function
Function GetMins(TimeNow As String) as uLong
Dim mm_fn as uLong
mm_fn = ValInt(Mid(TimeNow, 4, 2))
Return mm_fn
End Function
Function GetSecs(TimeNow As String) as uLong
Dim ss_fn as uLong
ss_fn = ValInt(Right(TimeNow, 2))
Return ss_fn
End Function
Then I call them from the main procedure:
Dim Ahora as string
Dim As uLong hh, mins, secs
Ahora = Time
hh = GetHour (Ahora)
mins = GetMins (Ahora)
secs = GetSecs (Ahora)
Then I can operate with the Time.
The purpose of this is to make a simple program to control a device with 8 relays to swith ON or OFF, depending on the programming time, but this will take me a work 'cause I don't program anything since almos 30 years ago. My last program was in QBasic 4.5 with windows 3.1 (Can you imagine?)
Ok, thank you for your help. All of You make great work...
P.D. This is the device...
http://www.robot-electronics.co.uk/htm/ ... 8btech.htm
I finally make a functions in other file, then I call them from everywhere:
Declare Function GetHour (TimeNow As String) As uLong
Declare Function GetMins (TimeNow As String) As uLong
Declare Function GetSecs (TimeNow As String) As uLong
Function GetHour(TimeNow As String) as uLong
Dim hh_fn as uLong
hh_fn = ValInt(Left(TimeNow, 2))
Return hh_fn
End Function
Function GetMins(TimeNow As String) as uLong
Dim mm_fn as uLong
mm_fn = ValInt(Mid(TimeNow, 4, 2))
Return mm_fn
End Function
Function GetSecs(TimeNow As String) as uLong
Dim ss_fn as uLong
ss_fn = ValInt(Right(TimeNow, 2))
Return ss_fn
End Function
Then I call them from the main procedure:
Dim Ahora as string
Dim As uLong hh, mins, secs
Ahora = Time
hh = GetHour (Ahora)
mins = GetMins (Ahora)
secs = GetSecs (Ahora)
Then I can operate with the Time.
The purpose of this is to make a simple program to control a device with 8 relays to swith ON or OFF, depending on the programming time, but this will take me a work 'cause I don't program anything since almos 30 years ago. My last program was in QBasic 4.5 with windows 3.1 (Can you imagine?)
Ok, thank you for your help. All of You make great work...
P.D. This is the device...
http://www.robot-electronics.co.uk/htm/ ... 8btech.htm