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
Formatting a time string
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Formatting a time string
Try...
..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..
Regards,
Josh
Code: Select all
FormatTime("HHmmss", ElTimeToDateTime_s(CurrentTime_s));
Edit: For completeness, FormatDate() would then be called with..
Code: Select all
FormatDate("dd-MM-yyyy", ElDateToDateTime(CurrentDate));
Josh
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: Formatting a time string
8 is a numberHello
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
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
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
If (time_s) = 95548 then the result will be "095548" = witch represent 09:55:48
Martin
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;
Martin
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Resolve: Re: Formatting a time string
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.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
If (time_s) = 95548 then the result will be "095548" = witch represent 09:55:48Code: Select all
If StrLen(TimeExitStr) < 6 then TimeExitStr = "0" + TimeExitStr;
Regards,
Josh
Re: Resolve: Re: Formatting a time string
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.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
If (time_s) = 95548 then the result will be "095548" = witch represent 09:55:48Code: Select all
If StrLen(TimeExitStr) < 6 then TimeExitStr = "0" + TimeExitStr;
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
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Resolve: Re: Formatting a time string
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,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.
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;
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;
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
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;
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
Josh
- arnie
- Posts: 1594
- Joined: 11 Feb 2009
- Location: Portugal
- Has thanked: 481 times
- Been thanked: 514 times
Re: Formatting a time string
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...
which on a 10 second bar retrieves 240 seconds, which...
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...
So when using a 10sec chart this plots exactly what I want
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.
What am I missing here?
Thanks.
Fernando
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
Code: Select all
(24 * barinterval) / 60
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))))
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.
What am I missing here?
Thanks.
Fernando
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Formatting a time string
I think it's because your original code is based on a seconds chart (with the _s suffix):What am I missing here?
Code: Select all
FormatTime("mm:ss", el_timetodatetime_s((secondsToTime_s(24 * barinterval))))
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]));