convert minutes into hours and minutes with classic asp - function

I would like to convert minutes into hours and minutes with classic asp
Example:
90
would
1 hour and 30 minutes
How would I do this in classic ASP
Thanks!

Like this?
function toHourMin(minutes)
hours = minutes \ 60
minutes = minutes mod 60
toHourMin = hours & iif(hours=1," hour"," hours") & " and " & minutes & iif(minutes=1," minute"," minutes")
end function
function iif(condition, truepart, falsepart)
if condition then
iif = truepart
else
iif = falsepart
end if
end function
Note that this will return output like:
0 hours and 0 minutes
If that isn't desired behaviour, you should state what should happen when you pass in differing (smaller) amounts of time.
Passing in 90 will return what you asked for:
1 hour and 30 minutes

Related

MYsql - Get average time between a start and stop time across many rows

I have the following SQL and PHP that doesnt seem to be giving me the right numbers. (VERY high numbers)
I have a lot of rows with a start time and an end timestamp.
Im looking to get the average time between those two times.
Ie: 2 hours, 3 minutes, 46 seconds.
This is what I have.
SELECT AVG(tmp.dd) AS timetook
FROM
( SELECT TIME_TO_SEC(TIMEDIFF(timeclosed, timeanswered)) AS dd
FROM logs
WHERE timeclosed > DATE_SUB(NOW(), INTERVAL 1 DAY)
) tmp;
Am I going about this the completely wrong way? Anything obviously wrong here?
while($row = $result->fetch_assoc()) {
$timetoclose = $row['timetook'];
$hours = floor($timetoclose / 3600);
$mins = floor($timetoclose / 60 % 60);
$secs = floor($timetoclose % 60);
$timetoclose = sprintf('%02d Hour(s), %02d Minute(s), %02d Second(s)', $hours, $mins, $secs);
}
Cheers
G
SELECT SUM(TIMESTAMPDIFF(minute, start_time, end_time)) / COUNT(*) AS avg_minutes
FROM your_table
try this query.

MS Access Textbox Time control

I am wondering if it is possible to control the time entered into MS Access textbox. Eg, only allow user to enter time in half hourly interval (08:30, 10:00, 13:30) as I do not want to end up with weird timing input.. (i.e 13:37, 16:42). Tried to use calculated control but doesn't seem to be working very well. Any suggestions or advice, please?
Calculated control in my textbox
=IIf((Minute([TxtStartTime1]) Mod 30=0),[TxtStartTime1],"00:00")
Below is a screen capture of my form.
Yes, that is possible, but it takes a little - actually that much, that I wrote an article on how-to:
Entering 24-hour time with input mask and full validation in Microsoft Access
It includes all the code needed to set the inputmask, validation, click and keypress events, as well as handling the form error - too much to post here.
Code is also on GitHub: VBA.TimeEntry
To round the time, you may include this function:
Public Function RoundTime( _
ByVal datDate As Date) _
As Date
Const cintMult As Integer = 24 '1 hour round
' Const cintMult As Integer = 48 '30 minute round
' Const cintMult As Integer = 96 '15 minute round
' Const cintMult As Integer = 144 '10 minute round
' Const cintMult As Integer = 288 '5 minute round
RoundTime = CVDate(Int(datDate * cintMult + 0.5) / cintMult)
End Function
You can round entered time for instance in BeforeUpdate event of your control. For rounding I use this VBA function:
Public Function TimeRoundMinutes(dtUnrounded As Date, Optional intRoundTo As Integer = 30, Optional intOption As Integer) As Date
'rounds time of provided date to specified in intRoundTo number of minutes. intOption:
'1 - down
'2 - up
'0 or anything else - to the nearest part
Dim intMins As Integer
If intRoundTo <= 0 Then
intRoundTo = 1
ElseIf intRoundTo > 60 Then
intRoundTo = 60
End If
intMins = Minute(dtUnrounded)
Select Case intOption
Case 1
intMins = Int(intMins / intRoundTo) * intRoundTo
Case 2
intMins = -Int(-intMins / intRoundTo) * intRoundTo
Case Else
intMins = Round(intMins / intRoundTo) * intRoundTo
End Select
If intMins = 60 Then
TimeRoundMinutes = DateAdd("h", 1, Int(dtUnrounded) + TimeSerial(Hour(dtUnrounded), 0, 0))
Else
TimeRoundMinutes = Int(dtUnrounded) + TimeSerial(Hour(dtUnrounded), intMins, 0)
End If
End Function

calculate the time difference between two dates in HH:MM format using ssrs

I need to generate the time difference between tow dates in SSRS in HH:MM format. How can i achieve this? My data source is MS CRM FetchXML.
Eg : Date1 : 01/01/2016 04:05 AM
Date2 : 01/03/2016 02:15 PM
Time Differece Should be : 58:10
Regards,
Sandeep
I might be a little too late but was needing to calculate the elapsed time between two times. Hence I stumbled up this link
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/26d8b4fc-6862-42a0-ac7f-0caa93ba2fde/to-find-ellapse-time-in-ssrs-2008?forum=sqlreportingservices
The below expression worked for me. I also found it is simpler to do it in SSRS than SQL.
=Datediff("h",Fields!StartDate.value,Fields!EndDate.value) & ":" & Datediff("n",Fields!StartDate.value,Fields!EndDate.value) mod 60 & ":" & Datediff("s",Fields!StartDate.value,Fields!EndDate.value) mod 60
The result of the above will be HH:MM:SS
Try this:
=DateDiff(
DateInterval.Hour,CDATE("01/01/2016 04:05 AM"), CDATE("03/01/2016 02:15 PM")
) & ":" &
DateDiff(
DateInterval.Minute,CDATE("01/01/2016 04:05 AM"), CDATE("03/01/2016 02:15 PM")
) Mod 60
It will produce 58:10
Replace CDATE() by the field or parameter containing your date time value.
Let me know if this helps.
Write a custom code to calculate time difference between two dates and return a string as expecting HH:MM like
Public Function GetTimeDifference(ByVal dt1 AS DATETIME,ByVal dt2 AS DATETIME) AS String
Dim Mn AS Integer=DateDiff("n",dt1,dt2)
Dim HH AS Integer=Mn /60
Dim MM AS Integer=Mn Mod 60
Return HH.ToString() & ":" & MM.ToString()
End Function
Now call this function as an expression like
=Code.GetTimeDifference(Parameters!startDate.Value,Parameters!endDate.Value)

Finding elapsed time in ms access query factoring in 8am to 5pm work day

Im using this function
Public Function ElapsedTime(Start As Date, Finish As Date) As String
'Calculates elapsed time between 2 date/times and
'parses it out into Hours-Minutes-Seconds in HH:MM:SS format
Dim HoursLapsed, SecondsLeft, MinutesLapsed, SecondsLapsed, TotalSeconds As Long
TotalSeconds = DateDiff("s", Start, Finish)
HoursLapsed = Int(TotalSeconds / 3600)
SecondsLeft = TotalSeconds Mod 3600
MinutesLapsed = Int(SecondsLeft / 60)
SecondsLapsed = SecondsLeft Mod 60
ElapsedTime = Format(HoursLapsed, "00") & ":" & Format(MinutesLapsed, "00") & ":" & Format(SecondsLapsed, "00")
End Function
This is my query
SELECT
ElapsedTime([Date_Faxed],[Date_Found]) AS Duration,
dbo_yard_request.Salesman,
dbo_yard_request.Make,
dbo_yard_request.Model,
dbo_yard_request.ID,
dbo_yard_request.Found2
FROM
dbo_yard_request
WHERE
(((dbo_yard_request.Found2)=True));
It works fine except i need to factor in the end of day at 5pm and start of new day at 8am.
For example
DATE_FAXED = 1/22/2015 4:45:22 PM
DATE_FOUND = 1/23/2015 8:55:13 AM
The elapsed time should be one hour and ten minutes, not 16 hours

VBA: DateDiff Hour Rounded off

I have the following sub in Access 2003 to return the hours elapsed bewteen two datetime fields.
Function DateDifferenceHour(dateStart As Date, dateEnd As Date) As String
'Outputs Hours from two dates
Dim age_hour As Double
age_hour = DateDiff("h", dateStart, dateEnd)
DateDifferenceHour = age_hour
End Function
If I have the following: DateDifferenceHour("07/23/2005","07/23/2005 7:30:00 PM").
It correctly returns 19 hours, but in reality, the time elapsed is 19 hours and 30 minutes.
How can I modify it so it can round off to 20 hours?
Edit:
My original suggestion was "Compute the difference in minutes, divide by 60, and round the quotient to zero decimal places". However, #Jean-François Corbett showed me the limitation of that approach.
? DateDifferenceHour("07/23/2005 7:00:59 PM","07/23/2005 7:30:00 PM")
1
IOW my first attempt rounded a duration of 29 minutes and 1 second up to 1 hour, which is no good. So instead I suggest using the difference in seconds and dividing by 3600.
Function DateDifferenceHour2(dateStart As Date, dateEnd As Date) As String
'Outputs Hours from two dates
DateDifferenceHour2 = _
Format(DateDiff("s", dateStart, dateEnd) / 3600, "0")
End Function
? DateDifferenceHour2("07/23/2005 7:00:59 PM","07/23/2005 7:30:00 PM")
0
There is still the issue of which rounding approach you want.
I chose Format() thinking you would want 2.5 hours rounded up to 3.
The VBA Round() function uses round-to-even, so Round(2.5, 0) would give you 2.
We don't know which you want; you can tell us. Also, dateStart and dateEnd imply dateStart will not be greater than dateEnd. However, if it can be, consider how you want a negative duration "rounded" to the nearest hour. Here are some examples copied from the Immediate Window.
? Round(-2.5, 0)
-2
? Round(-1.5, 0)
-2
? Format(-2.5, "0")
-3
? Format(-1.5, "0")
-2
This works, without any unexpected rounding (aside from the precision of the Date type itself).
Function DateDifferenceHour(dateStart As Date, dateEnd As Date) As String
' Rounds .5's to nearest even integer.
'DateDifferenceHour = CStr( Round( _
' CDbl(dateEnd - dateStart) * 24 ) )
' Rounds .5's up.
DateDifferenceHour = Format( _
CDbl(dateEnd - dateStart) * 24, "0" )
End Function
I put two rounding options so that it rounds to whole numbers; pick your favourite. It's way better programming practice to round explicitly and transparently than having DateDiff implicitly apply its unusual rounding.