[SOLVED] Extract hour, minute and second from time function.

New to FreeBASIC? Post your questions here.
Post Reply
Miguindi
Posts: 6
Joined: Oct 23, 2021 23:15

[SOLVED] Extract hour, minute and second from time function.

Post by Miguindi »

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.
Last edited by Miguindi on Nov 01, 2021 22:38, edited 1 time in total.
Xusinboy Bekchanov
Posts: 789
Joined: Jul 26, 2018 18:28

Re: Extract hour, minute and second from time function.

Post by Xusinboy Bekchanov »

You can do this:

Code: Select all

Print hh & "-" & mins & "-" & secs
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Extract hour, minute and second from time function.

Post by paul doe »

For what is worth, you can use specific functions to retrieve this info if you include the datetime.bi header:

Code: Select all

#include once "datetime.bi"

? time()

? hour( now() )
? minute( now() )
? second( now() )

sleep()
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.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Extract hour, minute and second from time function.

Post by fxm »

Or define 'mins', 'secs', and 'hh', all as ulong.
Miguindi
Posts: 6
Joined: Oct 23, 2021 23:15

Re: Extract hour, minute and second from time function.

Post by Miguindi »

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
Post Reply