Page 1 of 1

Formatting a time string

Posted: 02 Dec 2011
by arjfca
Hello

I just turn around my tail here so sorry if it is an amateur question

I need to format a time string so it as always the same format (hhmmss) even when time is like 08:00 i got 8 as the result I want 080000

Formatitme("HHmmss",(time_S) ) does not give me a proper value

Martin

Re: Formatting a time string

Posted: 02 Dec 2011
by JoshM
Try...

Code: Select all

FormatTime("HHmmss", ElTimeToDateTime_s(CurrentTime_s));
..since FormatTime(), like FormatDate(), requires a 'DateTime'-time format, and the EasyLanguage Time, Time_s, and Date are in a different format, a conversion to DateTime is needed first.

Edit: For completeness, FormatDate() would then be called with..

Code: Select all

FormatDate("dd-MM-yyyy", ElDateToDateTime(CurrentDate));
Regards,
Josh

Re: Formatting a time string

Posted: 02 Dec 2011
by TJ
Hello

I just turn around my tail here so sorry if it is an amateur question

I need to format a time string so it as always the same format (hhmmss) even when time is like 08:00 i got 8 as the result I want 080000

Formatitme("HHmmss",(time_S) ) does not give me a proper value

Martin
8 is a number
080000 is a string

BowlesJ posted a function to do some kind of conversion.
You can do a search to find out.

Resolve: Re: Formatting a time string

Posted: 02 Dec 2011
by arjfca
Hello

I have resolve my quest by looking at the length of the string. The only time that the length is smaller than 6 is when the time is between 00:00:00 and 09:59:59

Code: Select all

If StrLen(TimeExitStr) < 6 then TimeExitStr = "0" + TimeExitStr;
If (time_s) = 95548 then the result will be "095548" = witch represent 09:55:48

Martin

Re: Resolve: Re: Formatting a time string

Posted: 02 Dec 2011
by JoshM
I have resolve my quest by looking at the length of the string. The only time that the length is smaller than 6 is when the time is between 00:00:00 and 09:59:59

Code: Select all

If StrLen(TimeExitStr) < 6 then TimeExitStr = "0" + TimeExitStr;
If (time_s) = 95548 then the result will be "095548" = witch represent 09:55:48
I'm glad you got it solved, but (perhaps I'm not following you completely) isn't this 'making something simple complex'? The help in the PowerLanguage Editor for FormatTime contains the various formatting options for displaying the time, including the leading zero's you're looking for. In my opinion, that would give a quicker and more efficient code processing than doing string manipulations. Just saying. :)

Regards,
Josh

Re: Resolve: Re: Formatting a time string

Posted: 02 Dec 2011
by arjfca
I have resolve my quest by looking at the length of the string. The only time that the length is smaller than 6 is when the time is between 00:00:00 and 09:59:59

Code: Select all

If StrLen(TimeExitStr) < 6 then TimeExitStr = "0" + TimeExitStr;
If (time_s) = 95548 then the result will be "095548" = witch represent 09:55:48
I'm glad you got it solved, but (perhaps I'm not following you completely) isn't this 'making something simple complex'? The help in the PowerLanguage Editor for FormatTime contains the various formatting options for displaying the time, including the leading zero's you're looking for. In my opinion, that would give a quicker and more efficient code processing than doing string manipulations. Just saying. :)

John,
Function Time_S return a string

Function Formattime( "HHmmss", Time_s) that should be use to enable to preceiding "0", will not work because Time_S is a string and FormatTime is looking to format a time value as a number. That what I concluded.

Hope I'm clear enough ;)

Regards,
Josh

Re: Resolve: Re: Formatting a time string

Posted: 02 Dec 2011
by JoshM
Function Formattime( "HHmmss", Time_s) that should be use to enable to preceiding "0", will not work because Time_S is a string and FormatTime is looking to format a time value as a number. That what I concluded.
Ah okay, now I see your reasoning. :) Though your problem is solved, I just want to add that Time_s (and Time, CurrentTime_s, and Date, etc.) are numeric variables. For example,

Code: Select all

variables: myString1(""), myString2("");

if LastBarOnChart_s = True then begin

value1 = Time_s;
value2 = Time_s - 100;

// myString1 = Time_s; // gives a "types not compatible"-error
// myString2 = "Hello" - 100; // Same error here


Print("Time_s: ", Time_s, Spaces(3), "-5: ", (Time_s - 5), Spaces(3), "-500: ", (Time_s - 500));

end;
..will give an error if those two lines are not commented out. So, if you want to calculate with time, you can do that since it are numeric variables. For example:

Code: Select all

if LastBarOnChart_s = True then begin

value1 = CurrentTime_s;

// Substracting 1 minute from the current time
value2 = DateTime2ELTime_s(ELTimeToDateTime_s(value1) - ELTimeToDateTime(1));
value3 = value1 - 100;

Print(NewLine, "TimeNow: ", value1, Spaces(3), "With DateTime: ", value2, Spaces(3), "With Substracting from CurrentTime: ", value3);

Print(Spaces(3), "In more readable print: ", NumToStr(value1, 0), Spaces(3), NumToStr(value3, 0), NewLine,
Spaces(3), "Or even more creative: ", FormatTime("HH:mm [ss]", ElTimeToDateTime_s(value1)),
Spaces(3), FormatTime("hh:mm:ss tt", ElTimeToDateTime_s(value2)),
Spaces(3), FormatTime("h:s:s t", ELTimeToDateTime_s(value1)),
Spaces(3), FormatTime("ss:mm:HH", ELTimeToDateTime_s(value1)));

RecalcLastBarAfter(5);

end;
Which gives the following output..

Code: Select all

TimeNow: 173631.00 With DateTime: 173531.00 With Substracting from CurrentTime: 173531.00
In more readable print: 173631 173531
Or even more creative: 17:36 [31] 05:35:31 5:31:31 31:36:17

TimeNow: 173636.00 With DateTime: 173536.00 With Substracting from CurrentTime: 173536.00
In more readable print: 173636 173536
Or even more creative: 17:36 [36] 05:35:36 5:36:36 36:36:17

TimeNow: 173641.00 With DateTime: 173541.00 With Substracting from CurrentTime: 173541.00
In more readable print: 173641 173541
Or even more creative: 17:36 [41] 05:35:41 5:41:41 41:36:17

TimeNow: 173647.00 With DateTime: 173547.00 With Substracting from CurrentTime: 173547.00
In more readable print: 173647 173547
Or even more creative: 17:36 [47] 05:35:47 5:47:47 47:36:17
Just to show that practically anything can be done. :)

Edit: To give a more concrete example that actually can be useful, the function below (called "TimeNow") prints the time and date in a formatted string, so if I want to display the current time and date, that function can be used, which is more convenient than manually work with the time formats each time:

Code: Select all

TimeNow = Text(
FormatDate("dd-MM", ELDateToDateTime(CurrentDate)),
"_",
FormatTime("HH:mm:ss", ELTimeToDateTime_s(CurrentTime_s)),
Spaces(2));

For example:

Code: Select all

if LastBarOnChart_s = True then begin
Print(TimeNow);

RecalcLastBarAfter(5);
end;
Which looks like..

Code: Select all

02-12_17:51:05
02-12_17:51:10
02-12_17:51:15
02-12_17:51:20
02-12_17:51:25
02-12_17:51:30
02-12_17:51:35
Regards,
Josh

Re: Formatting a time string

Posted: 02 Dec 2011
by arjfca
This is a true reference post

Martin :)

Re: Formatting a time string

Posted: 16 Sep 2012
by arnie
I'm having a problem here with some similarities with arjfca's first post...

Let's say that I've a pattern that was built during 24 bars of 10 seconds.
What I want to know is how many minutes is that.

I need to use barinterval since the study is used with any time period.

So I start by saying...

Code: Select all

24 * barinterval
which on a 10 second bar retrieves 240 seconds, which...

Code: Select all

(24 * barinterval) / 60
retrieves 4min.

My problem was how to put this into a string to be plotted on the chart as text.

Using JoshM examples, I was able to write...

Code: Select all

FormatTime("mm:ss", el_timetodatetime_s((secondsToTime_s(24 * barinterval))))
So when using a 10sec chart this plots exactly what I want

Image

But when applying to a 3min chart things act a bit strange.
This 3min chart shows a 20 bar move which corresponds to 1 hour. Using the prior format "mm:ss" I get 1:00 which corresponds to 1 hour and not 1 minute but when changing the format to "hh:mm:ss" I get 12:01:00 instead of 01:00:00.

Image

What am I missing here?

Thanks.
Fernando

Re: Formatting a time string

Posted: 22 Sep 2012
by JoshM
What am I missing here?
I think it's because your original code is based on a seconds chart (with the _s suffix):

Code: Select all

FormatTime("mm:ss", el_timetodatetime_s((secondsToTime_s(24 * barinterval))))
And you then applied that to a minute chart. I suppose that if you multiply BarInterval (to go from Time to Time_s) it might work fine.

But.. wouldn't it be easier to not use BarInterval but take the time of the current bar and subtract the time of the previous bar? (Assuming you use a fixed time-based interval chart)

For example:

Code: Select all

FormatTime("mm:ss", EL_TimeToDateTime_s(Time_s) - EL_TimeToDateTime_s(Time_s[1]));
Note: there might be an issue with this is the bar is the first bar of the session (since Time_s[1] will then refer to yesterday).