0

I have an Excel sheet that i'm trying to transcribe to a VB form in order to get equipment's up time from an input number.

Let's say that the calculation results in, ex: 4.391286731 that is the days total.

In Excel I applied:

=INT(col+row) &" Days " & HOUR(col+row) & " h " & MINUTE(col+row) & " m " & SECOND(col+row) & " s".

The return is 4 Days 9h 23m 27s perfect.

Now in VB, after getting the final calculation , ex: the same 4.391286731 how can I convert this total to a date/time format similar to the Excel one above?

Scott Craner
  • 148,073
  • 10
  • 49
  • 81

2 Answers2

2

The data type you're looking for is TimeSpan.

Convert Decimal days to a TimeSpan:

Dim t As TimeSpan = TimeSpan.FromDays(4.391286731)

New TimeSpan from days, minutes, ETC:

dim t as new TimeSpan([Days], Hours, Minutes, Seconds)

Edit: Timespan.ToString() will give you a string representation, but not in your requested format.

string.format("{0:%d} Days {0:%h}h, {0:%m}m, {0:%s}s", timespan.fromdays(4.391286731))

Returns: "4 Days 9h, 23m, 27s"

MSDN Custom TimeSpan Format Strings

Dave 5709
  • 605
  • 3
  • 11
0

Use the Format command.

From the Immediate window with 4.391286731 in the ActiveCell.

?format(int(activecell.Value2), "0 \d\a\y\s ")&format(activecell.Value2, "h\h m\m s\s")
4 days 9h 23m 27s

You cannot mix-and-match a 0 format mask with h, m or s but you can certainly concatenate two Format strings together.