473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Function: Time Format

Hello all,

Thanks for reading firstly.

I am looking to format seconds into a format similar to that of an Excel "custom" format [h]:mm:ss

I have crafted the following however I am getting an error:

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function timeFormat(Seconds As Double) as String
  2.  
  3. if (Seconds = nothing) or (Seconds = 0)
  4.      timeFormat = 0
  5.  
  6.  else
  7.      Dim hours as Double = Seconds / 3600
  8.      Dim minutes as Double = (Seconds % 3600) / 60
  9.      Dim seconds as Double = Seconds % 60
  10.      timeFormat = CStr(hours) & ":" & CStr(minutes) & ":" & CStr(seconds)
  11.  
  12. end if
  13.  
  14. End Function
  15.  
Acknowledged that there is probably a real basic error in there, however I cannot get this to work.

I am using this as "custom code" in SQL Reporting Services 2005, Windows XP .NET 2.0

Can anybody push me in the right direction please!?

Thanks in advance?

J
Feb 12 '08 #1
6 1192
OK I do know that I had two variables named "seconds", though I still cannot get this to work:

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function timeFormat(Seconds As Double) as String
  2.  
  3. Dim outHours, outSeconds, outMinutes as Double
  4.  
  5. if (Seconds = nothing) or (Seconds = 0)
  6.      timeFormat = 0
  7.  
  8. else
  9.     outHours = Seconds / 3600
  10.     outMinutes = (Seconds % 3600) / 60
  11.     outSeconds = Seconds % 60
  12.     timeFormat = CStr(outHours) & ":" & CStr(outMinutes) & ":" & CStr outSeconds)
  13.     Return timeFormat
  14.     timeFormat = seconds
  15.  
  16. end if
  17.  
  18. End Function
  19.  
Feb 12 '08 #2
Plater
7,872 Expert 4TB
So you have a value in seconds, and you want to know how that time would be expressed in units of hours: minutes: seconds?

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function timeFormat(Seconds As Double) as String
  2.    Dim outHours, outSeconds, outMinutes as Double
  3.    if (Seconds = nothing) or (Seconds = 0)
  4.       timeFormat = "0"
  5.    else
  6.       outHours = Seconds / 3600
  7.       Seconds=(Seconds % 3600) 'take what is "left over"
  8.       outMinutes = (Seconds /60)
  9.       Seconds=(Seconds % 60) 'take what is "left over"
  10.       outSeconds = Seconds 
  11.       timeFormat = CStr(outHours) & ":" & CStr(outMinutes) & ":" & CStr outSeconds)
  12.    end if
  13.    Return timeFormat
  14. End Function
  15.  
Another possibility would be to use the built-in functions
Expand|Select|Wrap|Line Numbers
  1. Public Shared Function timeFormat(Seconds As Double) as String
  2.    Dim t as TimeSpan
  3.    t = TimeSpan.FromSeconds(Seconds)
  4.    timeformat = t.Hours & ":" & t.Minutes & ":" & t.Seconds
  5.    Return timeformat
  6. End Function
  7.  
Feb 12 '08 #3
Thanks for your reply!

The bottom function works for single cells, however when summing, it does not display correctly. In the detail row I have:

Expand|Select|Wrap|Line Numbers
  1. =Fields!TotalDailySeconds.value
  2.  
...in a hidden row I have
Expand|Select|Wrap|Line Numbers
  1.  =sum(Fields!TotalDailySeconds.value) 
, a textbox which I have called ctlTotalTime.

Then in the row which is visible I have
Expand|Select|Wrap|Line Numbers
  1. =code.timeFormat(ReportItems!ctlTotalTime) 
however, even going through this process means the time is not showing correctly e.g:

8:34:54 + 8:55:53 + 9:33:37 + 21:26:18 => 0:30:42

The actual TotalTime in seconds is 174642, so the minutes and seconds part is right however the hours obviously is not. Does anybody have any idea as to why this might be/ (I have checked the Format property!)

J
Feb 12 '08 #4
Plater
7,872 Expert 4TB
You should be summing the seconds before applying them to the function that returns them as an hour:minute:second format.

I think the default format for TimeSpan.ToString() is hh:MM:ss, but am not sure.
Feb 12 '08 #5
I am suprised no-one shouted at this one, however the function did not work correctly as TimeSpan moves any hours over 24 into the Day property!

The correct function is:

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function timeFormat(Seconds As Double) as String
  2.    Dim t as TimeSpan
  3.    t = TimeSpan.FromSeconds(Seconds)
  4.    timeformat = CStr((t.Days*24) + t.Hours) & ":" & t.Minutes & ":" & t.Seconds
  5.    Return timeformat
  6. End Function
  7.  
...thought I would post this in order that anyone else in a similar spot does not spend a week, in between all the other projects, looking for the solution!
Feb 20 '08 #6
Plater
7,872 Expert 4TB
That was a good catch, didn't even see that when I whipped up the code.
Feb 20 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
5
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than...
3
by: scorpion53061 | last post by:
Could you look at this function and tell me why I am getting an exception concerning date cast on line set apart by stars... If you have better suggestions of how to do this I would be open to...
4
by: laredotornado | last post by:
Hi, I am retrieving results from an SQL query, one of which is a date $arrival = $row; I would like to write a PHP function that formats the date in the following way: If the time part...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
2
by: Alfred | last post by:
Hi, i'am working at a project in an ac2000 enviroment. I need a function to calculate the difference between startt and end-time. I want the reuslt in the followed format " d "days and " hh "...
17
by: pamelafluente | last post by:
Hi I have to insert dates into some Access and SQL Databases. I need to be general as the target computer might be in any country. -------- - For access I wrote the follow: Function...
9
by: baibaichen | last post by:
hi our internal log apis support varargs, but do not support <<, it means that we have to write funciton tracing like this: printf("%s(%d,%f)", functionName,iCount,dTime); this need us...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.