convert to HH;MM:SS in active reports - sql

I have a table with a column, TotalTime that is an Integer value in seconds.
In Active reports I want to display it in HH:MM:SS format.
Thanks!
also editing it in here comments not giving me enough space =-)
numberVar Seconds := {xxxx};
numberVar Hours := Truncate(Seconds / 3600);
numberVar Minutes := Truncate((Seconds - (Hours*3600))/ 60);
if (Hours > 0) then
ToText(Hours, "0") + ":" + ToText(Minutes, "00") + ":" + ToText(Seconds - (Hours*3600) - (Minutes*60), "00")
else
"0:" + ToText(Minutes, "00") + ":" + ToText(Seconds - (Hours*3600) - (Minutes*60), "00")

In the Format event of the section containing the textbox you want to display this value in, use code like the following:
TimeSpan interval = TimeSpan.FromSeconds(seconds); //Assuming "seconds" comes from a ActiveReport.Fields value or from the Value property of a textbox in the report?
theTextbox.Text = interval.ToString("c");
See the MSDN docs For more information on FromSeconds and the "c" format specifier.

Related

pine script: how do you get the length of a series?

How can I get the number of days for a tickerid? For example, how long is the 'close' series in a simple default script? Or how can I get the first date availble for the current tickerid? If I had that I think that I can compute the number of days available.
This will show the information you requested in a label on the last bar.
If you set the timeframe to Daily, you'll get the number of (trading) days.
//#version=4
study("My Script", overlay=true)
var string firstDate = na
if barstate.isfirst
firstDate := tostring(dayofmonth(time)) + "/" + tostring(month(time)) + "/" + tostring(year(time))
if barstate.islast
label.new(bar_index, high, "First date = " + firstDate + "\nNumber of bars = " + tostring(bar_index+1), yloc=yloc.abovebar)

Macro to calculate the difference in hh:mm:ss

I want to calculate the difference in hh:mm:ss between Now and column E. They both appear in format dd/mm/yyyy hh:mm. With the code I have written below, it only takes into consideration the hh:mm and not the days. So, if they have 2 days difference in wont add to the hours +48. The code is below:
With ws1.Range("N2:N" & lastrow1)
.Formula = "=TIME(HOUR(NOW()),MINUTE(NOW()),SECOND(NOW()))-TIME(HOUR(E2),MINUTE(E2),SECOND(32))"
End With
Just use =(NOW()-E2) and apply a custom format [hh]:mm:ss. The brackets around hh will do the trick.
If you rather need a number of hours, multiply by 24 as #Kerry Jackson suggested.
The logic behing date/time values is that 1 day = 1, so
1 hour = 1/24
1 min = 1/1440 '(that is 24*60 )
etc...
Put this in a module
Public Function DateTimeDiff(d1 As Date, d2 As Date)
Dim diff As Double
diff = ABS(d2 - d1)
DateTimeDiff = Fix(diff) & Format(diff, " hh:mm")
End Function
Then use
=DateTimeDiff( NOW(), E2 )
as the formula in the worksheet.
You might want to add some validation on the dates and return an error message if they are not valid.
Are you looking for a number which is the number of hours, or are you looking for text?
If you want the number of hours, try just subtracting the dates, and multiplying by 24, so the formula would be =(NOW()-E2)*24.
Public Function timeElapsed(ByVal target_cell As Range) As String
Dim hours As Long, minutes As Long, days As Long
If target_cell.Value = 0 Then Exit Function
x = DateDiff("n", target_cell, Now())
days = Format(Application.WorksheetFunction.RoundDown(x / 1440, 0), "00")
hours = Format(Application.WorksheetFunction.RoundDown((x - (days * 1440)) / 60, 0), "00")
minutes = Format(Application.WorksheetFunction.RoundDown((x - ((days * 1440) + (hours * 60))), 0), "00")
timeElapsed = CStr(days) & " " & "days" & " " & CStr(hours) & ":" & CStr(minutes)
End Function
And use as function with the result as below:
So you code becomes:
With ws1
.Range("N2:N" & lastrow1).FormulaR1C1 = "=timeElapsed(RC[-9])"
End With

How do I get the current date in GML?

I need to be able to get the current date, it doesn't really matter what format it is. Is there a function, or perhaps an API i can use?
You can get the current date a number of ways in GML. The easiest of which is probably using the variables current_second, current_minute, current_hour, current_day, current_weekday, current_month, current_year
Here's an example that draws the day, month, and year.
draw_text(32, 32, "Today is " + string(current_day) + "/" + string (current_month) + "/" + string(current_year) +".");
You can change the timezone using date_set_timezone(timezone);
The available timezones are timezone_utc and timezone_local.
Another way to get the date is using date_current_datetime();
myhour = date_get_hour(date_current_datetime());
myday = date_get_day(date_current_datetime());
It exists some ways to do it. If you only need to show the current datetime you can use this:
show_message("Today is " + string(current_day) + "/" + string (current_month) + "/" + string(current_year) + " - " + string(current_hour) + ":" + string(current_minute) + "." + string(current_second) +".");
This will return something like: "Today is 3/6/2017 - 23:40:15."

Convert 24 hour Clock to 12 hour clock and add 6 hours

I have infinite rows with with a single column assigned to define date and time in the following 'General Format' "2016.08.10 06:00:00.066". I am aware that you can't convert every single cell in this column "mm/dd/yyyy hh:mm:ss.000 AM/PM". Therefore I would a single column assigned to "mm/dd/yyyy" and another column assigned to "hh:mm:ss.000 AM/PM". The time is currently 6 hours behind as well so I would like to add 6 hours to it.
I am struggling with this as although the cells are in general or text format the time and date is being displayed as "yyyy.mm.dd hh:mm:ss.000". And can't find a way to split the two in this format
Any help is appreciated
To convert the text to a format that Excel will change to a data/time use this:
=--SUBSTITUTE(SUBSTITUTE(A1,".","/",1),".","/",1)
Then to add 6 hours you would use:
+ TIME(6,0,0)
So to get the date/time is:
=--SUBSTITUTE(SUBSTITUTE(A1,".","/",1),".","/",1) + TIME(6,0,0)
Then simply format the new cell:
mm/dd/yyyy hh:mm:ss.000 AM/PM
You can also split it into the date and time:
Date:
=INT(--SUBSTITUTE(SUBSTITUTE(A1,".","/",1),".","/",1) + TIME(6,0,0))
And format it mm/dd/yyyy
Time:
=MOD(--SUBSTITUTE(SUBSTITUTE(A1,".","/",1),".","/",1) + TIME(6,0,0)),1)
And format it hh:mm:ss.000 AM/PM
use text: =concatenate(text(a1,"MM"),text(a1,"DD"),text(a1,"YYYY") do the same for the other column =concatenate(text(a1,"HH"),text(a1,"MM"),text(a1,"SS")
That's pretty strange that you Excel will round off the milliseconds if you try and use a Date-Time format.
Enum DTValues
ReturnDate
ReturnTime
ReturnDateTime
End Enum
Function getDateTime(yyyymmdd_hh_mm_ss_000 As String, ReturnValue As DTValues) As Single
Dim arr
Dim mSecs As Single
yyyymmdd_hh_mm_ss_000 = Replace(yyyymmdd_hh_mm_ss_000, " ", ".")
yyyymmdd_hh_mm_ss_000 = Replace(yyyymmdd_hh_mm_ss_000, ":", ".")
arr = Split(yyyymmdd_hh_mm_ss_000, ".")
mSecs = arr(6) / 24 / 60 / 60 / 100
Select Case ReturnValue
Case ReturnDate
getDateTime = CSng(DateSerial(arr(0), arr(1), arr(2)))
Case ReturnTime
getDateTime = CSng(TimeSerial(arr(3), arr(4), arr(5))) + mSecs
Case ReturnDateTime
getDateTime = CSng(DateSerial(arr(0), arr(1), arr(2))) + CSng(TimeSerial(arr(3), arr(4), arr(5))) + mSecs
End Select
End Function
Sub ProcessDates()
Const WORKSHEET_NAME = "Sheet1"
Const FIRST_ROW = 2
Const SOURE_COLUMN = 1
Const DATE_COLUMN = 2
Const TIME_COLUMN = 3
Dim Target As Range
Dim arDate, arTime
Dim y As Long
With Worksheets(WORKSHEET_NAME)
Set Target = .Range(.Cells(FIRST_ROW, SOURE_COLUMN), .Cells(Rows.Count, SOURE_COLUMN).End(xlUp))
End With
arDate = Target.Value
arTime = Target.Value
For y = 1 To UBound(arDate)
arDate(y) = getDateTime(arDate(y), ReturnDate)
arTime(y) = getDateTime(arTime(y), ReturnTime)
Next
Target.EntireRow.Columns(DATE_COLUMN).Value = arDate
Target.EntireRow.Columns(TIME_COLUMN).Value = arTime
End Sub

SQL to_date function

im modifying an oracle table using the following line of code:
db.execute("INSERT INTO "+dbOwner+"tbl_name VALUES('" + id+ "','" + studID+ "','"+"TO_DATE('"+ dateStr +"','MM/DD/YY)");
dateStr value: 2014-04-25
var now = new Date(),
dateStr = now.getFullYear() + "-",
monthCode = now.getMonth() + 1;
dateStr += ((monthCode < 10) ? ("0" + monthCode) : ("" + monthCode));
dateStr += "-" + ((now.getDate() < 10) ? ("0" + now.getDate()) : ("" + now.getDate()));
use bind variables. your insert statement will be 'visible'. there would be no problem to add quotes for variables (it will be automatic). and bonus: it is safer and faster.
Also, you can pass date as date, without transformation to/from string.
It will not work only for schema name (dbOwner).
according to what you write in the question
dateStr value is: 2014-04-25
so you should modify the date format in your code to be:
yyyy-MM-DD