Friday, March 9, 2012

Displaying difference between two dates in the HH:mm:ss format

I have a need to display the difference between two dates, a start date and a end date in the format HH:mm where the hours could be greater than 60.

For example:

Start Date - 30/01/2007 09:00:01
End Date - 01/02/2007 20:40:04

When i use the following code (=Fields!dateend.Value - Fields!DateStart.Value) i get 2.11:40:03 which i can easily understand, but the customer wants it as above!

I would like to be able to get it to be 59:40:03.

Any help would be much appreciated.I've worked it out like this:

First use DateDiff on the two fields using a hidden text box, then use the following code.

=IIF(Len(CStr(Fix(ReportItems!GrossTimeMins.Value/60)))= 1,
"0" + CStr(Fix(ReportItems!GrossTimeMins.Value/60)),
CStr(Fix(ReportItems!GrossTimeMins.Value/60)))
+ ":"
+ IIF(Len(CStr(Fix(ReportItems!GrossTimeMins.Value mod 60)))= 1,
"0" + CStr(Fix(ReportItems!GrossTimeMins.Value mod 60)),
CStr(Fix(ReportItems!GrossTimeMins.Value mod 60)))

If anyone thinks of a better way of doing it quite happy for you to comment.

No comments:

Post a Comment