Convert integer to full text string durationMilliseconds to Time string & Time string to MillisecondsConvert string to multiline textConvert number into hours : minutesClassifying duration as <1, 1-2, 2-3, or >3 yearsConvert Integer to Date in JavaFilter to convert duration to hours — in controller or model?Convert Iterable tree to stringConvert Lua UINT to hh:mm:ssConvert decimal to stringConvert integer to decimal string

Is it acceptable that I plot a time-series figure with years increasing from right to left?

Why no parachutes in the Orion AA2 abort test?

Can you take the Dodge action while prone?

Is it possible to spoof an IP address to an exact number?

Park the computer

Can a Time Lord survive with just one heart?

Should I increase my 401(k) contributions, or increase my mortgage payments

Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?

Should I warn my boss I might take sick leave?

How to supply water to a coastal desert town with no rain and no freshwater aquifers?

PhD: When to quit and move on?

What are some bad ways to subvert tropes?

Why do Martians have to wear space helmets?

List comprehensions in Mathematica?

How to play a D major chord lower than the open E major chord on guitar?

When is one 'Ready' to make Original Contributions to Mathematics?

Question about targeting a Hexproof creature

Has chattel slavery ever been used as a criminal punishment in the USA since the passage of the Thirteenth Amendment?

Why weren't Gemini capsules given names?

Shipped package arrived - didn't order, possible scam?

How to reclaim personal item I've lent to the office without burning bridges?

Change the default text editor in Terminal

How serious is plagiarism in a master’s thesis?

Is it possible that Curiosity measured its own methane or failed doing the spectrometry?



Convert integer to full text string duration


Milliseconds to Time string & Time string to MillisecondsConvert string to multiline textConvert number into hours : minutesClassifying duration as <1, 1-2, 2-3, or >3 yearsConvert Integer to Date in JavaFilter to convert duration to hours — in controller or model?Convert Iterable tree to stringConvert Lua UINT to hh:mm:ssConvert decimal to stringConvert integer to decimal string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I have a function I have written in VB for use in SSRS 2012. It takes an integer and converts it to full text duration - for example 900 becomes "15 minutes", 9000 becomes "2 hours 30 minutes", and 7201 is "2 hours 1 second".



The code works, and is:



Public Function SecondsFullText(ByVal TotalSeconds As Integer) As String

Dim hours As Integer = new Integer()
Dim minutes As Integer = new Integer()
Dim seconds As Integer = new Integer()

Dim hourString as String = ""
Dim minuteString as String = ""
Dim secondString as String = ""

hours = floor(TotalSeconds / 3600)
minutes = floor((TotalSeconds mod 3600) / 60)
seconds = (TotalSeconds mod 3600) mod 60

If hours = 1 Then
hourString = Cstr(hours) & " hour"
Else If hours > 1 Then
hourString = Cstr(hours) & " hours"
End If

If minutes = 1 Then
minuteString = Cstr(minutes) & " minute"
Else If minutes > 1 Then
minuteString = Cstr(minutes) & " minutes"
End If

If seconds = 1 Then
secondString = Cstr(seconds) & " second"
Else If seconds > 1 Then
secondString = Cstr(seconds) & " seconds"
End If

If hours > 0 and (minutes > 0 or seconds > 0) Then
hourString = hourString & " "
End If

If minutes > 0 and seconds > 0 Then
minuteString = minuteString & " "
End If

return hourString & minuteString & secondString
End Function


My question is: how do I make this better? It seems very clunky, with too many IFs to handle the different possibilities for single/plural, spaces, etc. I feel that this can be better, but I'm not sure how.



This is called in Report Builder 3.0 / SSRS 2012 with this expression: =Code.SecondsFullText(Parameters!Integer.Value)










share|improve this question









New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $begingroup$
    It would be easier to provide a review and answer if the function that called this or displayed the resulting string was included as well.
    $endgroup$
    – pacmaninbw
    7 hours ago










  • $begingroup$
    @pacmaninbw Noted, and updated. There's no real function that calls it as it's for use in a report in SSRS.
    $endgroup$
    – BishNaboB
    7 hours ago

















4












$begingroup$


I have a function I have written in VB for use in SSRS 2012. It takes an integer and converts it to full text duration - for example 900 becomes "15 minutes", 9000 becomes "2 hours 30 minutes", and 7201 is "2 hours 1 second".



The code works, and is:



Public Function SecondsFullText(ByVal TotalSeconds As Integer) As String

Dim hours As Integer = new Integer()
Dim minutes As Integer = new Integer()
Dim seconds As Integer = new Integer()

Dim hourString as String = ""
Dim minuteString as String = ""
Dim secondString as String = ""

hours = floor(TotalSeconds / 3600)
minutes = floor((TotalSeconds mod 3600) / 60)
seconds = (TotalSeconds mod 3600) mod 60

If hours = 1 Then
hourString = Cstr(hours) & " hour"
Else If hours > 1 Then
hourString = Cstr(hours) & " hours"
End If

If minutes = 1 Then
minuteString = Cstr(minutes) & " minute"
Else If minutes > 1 Then
minuteString = Cstr(minutes) & " minutes"
End If

If seconds = 1 Then
secondString = Cstr(seconds) & " second"
Else If seconds > 1 Then
secondString = Cstr(seconds) & " seconds"
End If

If hours > 0 and (minutes > 0 or seconds > 0) Then
hourString = hourString & " "
End If

If minutes > 0 and seconds > 0 Then
minuteString = minuteString & " "
End If

return hourString & minuteString & secondString
End Function


My question is: how do I make this better? It seems very clunky, with too many IFs to handle the different possibilities for single/plural, spaces, etc. I feel that this can be better, but I'm not sure how.



This is called in Report Builder 3.0 / SSRS 2012 with this expression: =Code.SecondsFullText(Parameters!Integer.Value)










share|improve this question









New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $begingroup$
    It would be easier to provide a review and answer if the function that called this or displayed the resulting string was included as well.
    $endgroup$
    – pacmaninbw
    7 hours ago










  • $begingroup$
    @pacmaninbw Noted, and updated. There's no real function that calls it as it's for use in a report in SSRS.
    $endgroup$
    – BishNaboB
    7 hours ago













4












4








4





$begingroup$


I have a function I have written in VB for use in SSRS 2012. It takes an integer and converts it to full text duration - for example 900 becomes "15 minutes", 9000 becomes "2 hours 30 minutes", and 7201 is "2 hours 1 second".



The code works, and is:



Public Function SecondsFullText(ByVal TotalSeconds As Integer) As String

Dim hours As Integer = new Integer()
Dim minutes As Integer = new Integer()
Dim seconds As Integer = new Integer()

Dim hourString as String = ""
Dim minuteString as String = ""
Dim secondString as String = ""

hours = floor(TotalSeconds / 3600)
minutes = floor((TotalSeconds mod 3600) / 60)
seconds = (TotalSeconds mod 3600) mod 60

If hours = 1 Then
hourString = Cstr(hours) & " hour"
Else If hours > 1 Then
hourString = Cstr(hours) & " hours"
End If

If minutes = 1 Then
minuteString = Cstr(minutes) & " minute"
Else If minutes > 1 Then
minuteString = Cstr(minutes) & " minutes"
End If

If seconds = 1 Then
secondString = Cstr(seconds) & " second"
Else If seconds > 1 Then
secondString = Cstr(seconds) & " seconds"
End If

If hours > 0 and (minutes > 0 or seconds > 0) Then
hourString = hourString & " "
End If

If minutes > 0 and seconds > 0 Then
minuteString = minuteString & " "
End If

return hourString & minuteString & secondString
End Function


My question is: how do I make this better? It seems very clunky, with too many IFs to handle the different possibilities for single/plural, spaces, etc. I feel that this can be better, but I'm not sure how.



This is called in Report Builder 3.0 / SSRS 2012 with this expression: =Code.SecondsFullText(Parameters!Integer.Value)










share|improve this question









New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




I have a function I have written in VB for use in SSRS 2012. It takes an integer and converts it to full text duration - for example 900 becomes "15 minutes", 9000 becomes "2 hours 30 minutes", and 7201 is "2 hours 1 second".



The code works, and is:



Public Function SecondsFullText(ByVal TotalSeconds As Integer) As String

Dim hours As Integer = new Integer()
Dim minutes As Integer = new Integer()
Dim seconds As Integer = new Integer()

Dim hourString as String = ""
Dim minuteString as String = ""
Dim secondString as String = ""

hours = floor(TotalSeconds / 3600)
minutes = floor((TotalSeconds mod 3600) / 60)
seconds = (TotalSeconds mod 3600) mod 60

If hours = 1 Then
hourString = Cstr(hours) & " hour"
Else If hours > 1 Then
hourString = Cstr(hours) & " hours"
End If

If minutes = 1 Then
minuteString = Cstr(minutes) & " minute"
Else If minutes > 1 Then
minuteString = Cstr(minutes) & " minutes"
End If

If seconds = 1 Then
secondString = Cstr(seconds) & " second"
Else If seconds > 1 Then
secondString = Cstr(seconds) & " seconds"
End If

If hours > 0 and (minutes > 0 or seconds > 0) Then
hourString = hourString & " "
End If

If minutes > 0 and seconds > 0 Then
minuteString = minuteString & " "
End If

return hourString & minuteString & secondString
End Function


My question is: how do I make this better? It seems very clunky, with too many IFs to handle the different possibilities for single/plural, spaces, etc. I feel that this can be better, but I'm not sure how.



This is called in Report Builder 3.0 / SSRS 2012 with this expression: =Code.SecondsFullText(Parameters!Integer.Value)







datetime formatting vb.net






share|improve this question









New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 3 hours ago









200_success

134k21 gold badges170 silver badges440 bronze badges




134k21 gold badges170 silver badges440 bronze badges






New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









BishNaboBBishNaboB

1235 bronze badges




1235 bronze badges




New contributor



BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




BishNaboB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • $begingroup$
    It would be easier to provide a review and answer if the function that called this or displayed the resulting string was included as well.
    $endgroup$
    – pacmaninbw
    7 hours ago










  • $begingroup$
    @pacmaninbw Noted, and updated. There's no real function that calls it as it's for use in a report in SSRS.
    $endgroup$
    – BishNaboB
    7 hours ago
















  • $begingroup$
    It would be easier to provide a review and answer if the function that called this or displayed the resulting string was included as well.
    $endgroup$
    – pacmaninbw
    7 hours ago










  • $begingroup$
    @pacmaninbw Noted, and updated. There's no real function that calls it as it's for use in a report in SSRS.
    $endgroup$
    – BishNaboB
    7 hours ago















$begingroup$
It would be easier to provide a review and answer if the function that called this or displayed the resulting string was included as well.
$endgroup$
– pacmaninbw
7 hours ago




$begingroup$
It would be easier to provide a review and answer if the function that called this or displayed the resulting string was included as well.
$endgroup$
– pacmaninbw
7 hours ago












$begingroup$
@pacmaninbw Noted, and updated. There's no real function that calls it as it's for use in a report in SSRS.
$endgroup$
– BishNaboB
7 hours ago




$begingroup$
@pacmaninbw Noted, and updated. There's no real function that calls it as it's for use in a report in SSRS.
$endgroup$
– BishNaboB
7 hours ago










2 Answers
2






active

oldest

votes


















4












$begingroup$

You should look into the TimeSpan struct, which provides a nice method TimeSpan.FromSeconds().



Having filled a TimeSpan struct by e.g calling the FromSeconds() method, you can just access its properties Seconds, Minutes and Hours.



Instead of concating strings like you do, you should consider to use a StringBuilder.



To get rid of the If ... Else If you can just use the singular and only if the value is greater than 1 you add a s to the string/StringBuilder.



Based on the .NET Naming Guidelines method parameters should be named using camelCase casing hence TotalSeconds should be totalSeconds



Implementing the mentioned points will look like so



Public Function SecondsFullText(ByVal totalSeconds As Double) As String

Dim timeSpan As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
Dim stringBuilder As StringBuilder = New StringBuilder()

Dim hours As Integer = timeSpan.Hours
Dim minutes As Integer = timeSpan.Minutes
Dim seconds As Integer = timeSpan.Seconds

stringBuilder.Append(ToText(hours, "hour"))
If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(minutes, "minute"))
If minutes > 0 AndAlso seconds > 0 Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(seconds, "second"))
Return stringBuilder.ToString()

End Function

Private Function ToText(value As Integer, singularName As String) As String

If value = 0 Then Return String.Empty
If value = 1 Then Return String.Format("0 1", value, singularName)
Return String.Format("0 1s", value, singularName)

End Function





share|improve this answer









$endgroup$












  • $begingroup$
    Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
    $endgroup$
    – BishNaboB
    7 hours ago







  • 1




    $begingroup$
    You need to add an import to System. Text
    $endgroup$
    – Heslacher
    7 hours ago










  • $begingroup$
    How does this handle a duration greater than 24 hours?
    $endgroup$
    – BishNaboB
    6 hours ago






  • 1




    $begingroup$
    Then you need to query the TotalHours property
    $endgroup$
    – Heslacher
    6 hours ago










  • $begingroup$
    Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
    $endgroup$
    – BishNaboB
    6 hours ago


















1












$begingroup$

As @Hesclacher has given a fantastic answer, which is not only one that I have upvoted but I would personally mark it as the correct answer if I had the power, I am hesitant to improve upon it. But I would like to offer constructive feedback on your original code.



I have a personal distinction between being a VB Coder and a .NET Developer. I think you can use VB.NET and be what I define to be a .NET Developer - that is you are making traditional .NET calls like any C# dev would use. I define a VB Coder has someone who still thinks and codes with VBA in mind, and such a person relies upon some of the compatibility calls, such as CStr.



According to my own such definitions, a VB Coder would use CStr whereas a .NET Developer would use String.Format (as did Heschalcher) or better yet, Interpolated Strings.



VB Coder:



hourString = Cstr(hours) & " hours"


.NET Developer:



hourString = String.Format("0 hours", hours)


Or



hourString = $"hours hours"


To calculate the number of hours, if you aren't going to use something nice like TimeSpan, I would change this line of code:



hours = floor(TotalSeconds / 3600)


To simply use integer division:



hours = TotalSeconds 3600


Also, VB.NET does support short-circuited conditionals (unlike VBA), so this line of code:



If hours > 0 and (minutes > 0 or seconds > 0) Then


could be changed to:



If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then


This example employs short-circuiting with the AndAlso and OrElse operators.



Again, Heschachler's answer with TimeSpan and StringBuilder is spot-on.






share|improve this answer









$endgroup$












  • $begingroup$
    Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
    $endgroup$
    – BishNaboB
    1 hour ago













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






BishNaboB is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f223421%2fconvert-integer-to-full-text-string-duration%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

You should look into the TimeSpan struct, which provides a nice method TimeSpan.FromSeconds().



Having filled a TimeSpan struct by e.g calling the FromSeconds() method, you can just access its properties Seconds, Minutes and Hours.



Instead of concating strings like you do, you should consider to use a StringBuilder.



To get rid of the If ... Else If you can just use the singular and only if the value is greater than 1 you add a s to the string/StringBuilder.



Based on the .NET Naming Guidelines method parameters should be named using camelCase casing hence TotalSeconds should be totalSeconds



Implementing the mentioned points will look like so



Public Function SecondsFullText(ByVal totalSeconds As Double) As String

Dim timeSpan As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
Dim stringBuilder As StringBuilder = New StringBuilder()

Dim hours As Integer = timeSpan.Hours
Dim minutes As Integer = timeSpan.Minutes
Dim seconds As Integer = timeSpan.Seconds

stringBuilder.Append(ToText(hours, "hour"))
If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(minutes, "minute"))
If minutes > 0 AndAlso seconds > 0 Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(seconds, "second"))
Return stringBuilder.ToString()

End Function

Private Function ToText(value As Integer, singularName As String) As String

If value = 0 Then Return String.Empty
If value = 1 Then Return String.Format("0 1", value, singularName)
Return String.Format("0 1s", value, singularName)

End Function





share|improve this answer









$endgroup$












  • $begingroup$
    Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
    $endgroup$
    – BishNaboB
    7 hours ago







  • 1




    $begingroup$
    You need to add an import to System. Text
    $endgroup$
    – Heslacher
    7 hours ago










  • $begingroup$
    How does this handle a duration greater than 24 hours?
    $endgroup$
    – BishNaboB
    6 hours ago






  • 1




    $begingroup$
    Then you need to query the TotalHours property
    $endgroup$
    – Heslacher
    6 hours ago










  • $begingroup$
    Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
    $endgroup$
    – BishNaboB
    6 hours ago















4












$begingroup$

You should look into the TimeSpan struct, which provides a nice method TimeSpan.FromSeconds().



Having filled a TimeSpan struct by e.g calling the FromSeconds() method, you can just access its properties Seconds, Minutes and Hours.



Instead of concating strings like you do, you should consider to use a StringBuilder.



To get rid of the If ... Else If you can just use the singular and only if the value is greater than 1 you add a s to the string/StringBuilder.



Based on the .NET Naming Guidelines method parameters should be named using camelCase casing hence TotalSeconds should be totalSeconds



Implementing the mentioned points will look like so



Public Function SecondsFullText(ByVal totalSeconds As Double) As String

Dim timeSpan As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
Dim stringBuilder As StringBuilder = New StringBuilder()

Dim hours As Integer = timeSpan.Hours
Dim minutes As Integer = timeSpan.Minutes
Dim seconds As Integer = timeSpan.Seconds

stringBuilder.Append(ToText(hours, "hour"))
If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(minutes, "minute"))
If minutes > 0 AndAlso seconds > 0 Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(seconds, "second"))
Return stringBuilder.ToString()

End Function

Private Function ToText(value As Integer, singularName As String) As String

If value = 0 Then Return String.Empty
If value = 1 Then Return String.Format("0 1", value, singularName)
Return String.Format("0 1s", value, singularName)

End Function





share|improve this answer









$endgroup$












  • $begingroup$
    Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
    $endgroup$
    – BishNaboB
    7 hours ago







  • 1




    $begingroup$
    You need to add an import to System. Text
    $endgroup$
    – Heslacher
    7 hours ago










  • $begingroup$
    How does this handle a duration greater than 24 hours?
    $endgroup$
    – BishNaboB
    6 hours ago






  • 1




    $begingroup$
    Then you need to query the TotalHours property
    $endgroup$
    – Heslacher
    6 hours ago










  • $begingroup$
    Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
    $endgroup$
    – BishNaboB
    6 hours ago













4












4








4





$begingroup$

You should look into the TimeSpan struct, which provides a nice method TimeSpan.FromSeconds().



Having filled a TimeSpan struct by e.g calling the FromSeconds() method, you can just access its properties Seconds, Minutes and Hours.



Instead of concating strings like you do, you should consider to use a StringBuilder.



To get rid of the If ... Else If you can just use the singular and only if the value is greater than 1 you add a s to the string/StringBuilder.



Based on the .NET Naming Guidelines method parameters should be named using camelCase casing hence TotalSeconds should be totalSeconds



Implementing the mentioned points will look like so



Public Function SecondsFullText(ByVal totalSeconds As Double) As String

Dim timeSpan As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
Dim stringBuilder As StringBuilder = New StringBuilder()

Dim hours As Integer = timeSpan.Hours
Dim minutes As Integer = timeSpan.Minutes
Dim seconds As Integer = timeSpan.Seconds

stringBuilder.Append(ToText(hours, "hour"))
If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(minutes, "minute"))
If minutes > 0 AndAlso seconds > 0 Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(seconds, "second"))
Return stringBuilder.ToString()

End Function

Private Function ToText(value As Integer, singularName As String) As String

If value = 0 Then Return String.Empty
If value = 1 Then Return String.Format("0 1", value, singularName)
Return String.Format("0 1s", value, singularName)

End Function





share|improve this answer









$endgroup$



You should look into the TimeSpan struct, which provides a nice method TimeSpan.FromSeconds().



Having filled a TimeSpan struct by e.g calling the FromSeconds() method, you can just access its properties Seconds, Minutes and Hours.



Instead of concating strings like you do, you should consider to use a StringBuilder.



To get rid of the If ... Else If you can just use the singular and only if the value is greater than 1 you add a s to the string/StringBuilder.



Based on the .NET Naming Guidelines method parameters should be named using camelCase casing hence TotalSeconds should be totalSeconds



Implementing the mentioned points will look like so



Public Function SecondsFullText(ByVal totalSeconds As Double) As String

Dim timeSpan As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
Dim stringBuilder As StringBuilder = New StringBuilder()

Dim hours As Integer = timeSpan.Hours
Dim minutes As Integer = timeSpan.Minutes
Dim seconds As Integer = timeSpan.Seconds

stringBuilder.Append(ToText(hours, "hour"))
If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(minutes, "minute"))
If minutes > 0 AndAlso seconds > 0 Then
stringBuilder.Append(" ")
End If
stringBuilder.Append(ToText(seconds, "second"))
Return stringBuilder.ToString()

End Function

Private Function ToText(value As Integer, singularName As String) As String

If value = 0 Then Return String.Empty
If value = 1 Then Return String.Format("0 1", value, singularName)
Return String.Format("0 1s", value, singularName)

End Function






share|improve this answer












share|improve this answer



share|improve this answer










answered 7 hours ago









HeslacherHeslacher

45.5k4 gold badges67 silver badges162 bronze badges




45.5k4 gold badges67 silver badges162 bronze badges











  • $begingroup$
    Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
    $endgroup$
    – BishNaboB
    7 hours ago







  • 1




    $begingroup$
    You need to add an import to System. Text
    $endgroup$
    – Heslacher
    7 hours ago










  • $begingroup$
    How does this handle a duration greater than 24 hours?
    $endgroup$
    – BishNaboB
    6 hours ago






  • 1




    $begingroup$
    Then you need to query the TotalHours property
    $endgroup$
    – Heslacher
    6 hours ago










  • $begingroup$
    Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
    $endgroup$
    – BishNaboB
    6 hours ago
















  • $begingroup$
    Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
    $endgroup$
    – BishNaboB
    7 hours ago







  • 1




    $begingroup$
    You need to add an import to System. Text
    $endgroup$
    – Heslacher
    7 hours ago










  • $begingroup$
    How does this handle a duration greater than 24 hours?
    $endgroup$
    – BishNaboB
    6 hours ago






  • 1




    $begingroup$
    Then you need to query the TotalHours property
    $endgroup$
    – Heslacher
    6 hours ago










  • $begingroup$
    Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
    $endgroup$
    – BishNaboB
    6 hours ago















$begingroup$
Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
$endgroup$
– BishNaboB
7 hours ago





$begingroup$
Thank you for this. Unfortunately it doesn't look as though StringBuilder is valid in the VB implementation in SSRS/Report Builder 3.0. It returns this error: There is an error on line 46 of custom code: [BC30002] Type 'StringBuilder' is not defined.
$endgroup$
– BishNaboB
7 hours ago





1




1




$begingroup$
You need to add an import to System. Text
$endgroup$
– Heslacher
7 hours ago




$begingroup$
You need to add an import to System. Text
$endgroup$
– Heslacher
7 hours ago












$begingroup$
How does this handle a duration greater than 24 hours?
$endgroup$
– BishNaboB
6 hours ago




$begingroup$
How does this handle a duration greater than 24 hours?
$endgroup$
– BishNaboB
6 hours ago




1




1




$begingroup$
Then you need to query the TotalHours property
$endgroup$
– Heslacher
6 hours ago




$begingroup$
Then you need to query the TotalHours property
$endgroup$
– Heslacher
6 hours ago












$begingroup$
Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
$endgroup$
– BishNaboB
6 hours ago




$begingroup$
Thank you. I had to change Dim stringBuilder As StringBuilder = New StringBuilder() to Dim stringBuilder As New System.Text.StringBuilder() to make it work in SSRS.
$endgroup$
– BishNaboB
6 hours ago













1












$begingroup$

As @Hesclacher has given a fantastic answer, which is not only one that I have upvoted but I would personally mark it as the correct answer if I had the power, I am hesitant to improve upon it. But I would like to offer constructive feedback on your original code.



I have a personal distinction between being a VB Coder and a .NET Developer. I think you can use VB.NET and be what I define to be a .NET Developer - that is you are making traditional .NET calls like any C# dev would use. I define a VB Coder has someone who still thinks and codes with VBA in mind, and such a person relies upon some of the compatibility calls, such as CStr.



According to my own such definitions, a VB Coder would use CStr whereas a .NET Developer would use String.Format (as did Heschalcher) or better yet, Interpolated Strings.



VB Coder:



hourString = Cstr(hours) & " hours"


.NET Developer:



hourString = String.Format("0 hours", hours)


Or



hourString = $"hours hours"


To calculate the number of hours, if you aren't going to use something nice like TimeSpan, I would change this line of code:



hours = floor(TotalSeconds / 3600)


To simply use integer division:



hours = TotalSeconds 3600


Also, VB.NET does support short-circuited conditionals (unlike VBA), so this line of code:



If hours > 0 and (minutes > 0 or seconds > 0) Then


could be changed to:



If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then


This example employs short-circuiting with the AndAlso and OrElse operators.



Again, Heschachler's answer with TimeSpan and StringBuilder is spot-on.






share|improve this answer









$endgroup$












  • $begingroup$
    Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
    $endgroup$
    – BishNaboB
    1 hour ago















1












$begingroup$

As @Hesclacher has given a fantastic answer, which is not only one that I have upvoted but I would personally mark it as the correct answer if I had the power, I am hesitant to improve upon it. But I would like to offer constructive feedback on your original code.



I have a personal distinction between being a VB Coder and a .NET Developer. I think you can use VB.NET and be what I define to be a .NET Developer - that is you are making traditional .NET calls like any C# dev would use. I define a VB Coder has someone who still thinks and codes with VBA in mind, and such a person relies upon some of the compatibility calls, such as CStr.



According to my own such definitions, a VB Coder would use CStr whereas a .NET Developer would use String.Format (as did Heschalcher) or better yet, Interpolated Strings.



VB Coder:



hourString = Cstr(hours) & " hours"


.NET Developer:



hourString = String.Format("0 hours", hours)


Or



hourString = $"hours hours"


To calculate the number of hours, if you aren't going to use something nice like TimeSpan, I would change this line of code:



hours = floor(TotalSeconds / 3600)


To simply use integer division:



hours = TotalSeconds 3600


Also, VB.NET does support short-circuited conditionals (unlike VBA), so this line of code:



If hours > 0 and (minutes > 0 or seconds > 0) Then


could be changed to:



If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then


This example employs short-circuiting with the AndAlso and OrElse operators.



Again, Heschachler's answer with TimeSpan and StringBuilder is spot-on.






share|improve this answer









$endgroup$












  • $begingroup$
    Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
    $endgroup$
    – BishNaboB
    1 hour ago













1












1








1





$begingroup$

As @Hesclacher has given a fantastic answer, which is not only one that I have upvoted but I would personally mark it as the correct answer if I had the power, I am hesitant to improve upon it. But I would like to offer constructive feedback on your original code.



I have a personal distinction between being a VB Coder and a .NET Developer. I think you can use VB.NET and be what I define to be a .NET Developer - that is you are making traditional .NET calls like any C# dev would use. I define a VB Coder has someone who still thinks and codes with VBA in mind, and such a person relies upon some of the compatibility calls, such as CStr.



According to my own such definitions, a VB Coder would use CStr whereas a .NET Developer would use String.Format (as did Heschalcher) or better yet, Interpolated Strings.



VB Coder:



hourString = Cstr(hours) & " hours"


.NET Developer:



hourString = String.Format("0 hours", hours)


Or



hourString = $"hours hours"


To calculate the number of hours, if you aren't going to use something nice like TimeSpan, I would change this line of code:



hours = floor(TotalSeconds / 3600)


To simply use integer division:



hours = TotalSeconds 3600


Also, VB.NET does support short-circuited conditionals (unlike VBA), so this line of code:



If hours > 0 and (minutes > 0 or seconds > 0) Then


could be changed to:



If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then


This example employs short-circuiting with the AndAlso and OrElse operators.



Again, Heschachler's answer with TimeSpan and StringBuilder is spot-on.






share|improve this answer









$endgroup$



As @Hesclacher has given a fantastic answer, which is not only one that I have upvoted but I would personally mark it as the correct answer if I had the power, I am hesitant to improve upon it. But I would like to offer constructive feedback on your original code.



I have a personal distinction between being a VB Coder and a .NET Developer. I think you can use VB.NET and be what I define to be a .NET Developer - that is you are making traditional .NET calls like any C# dev would use. I define a VB Coder has someone who still thinks and codes with VBA in mind, and such a person relies upon some of the compatibility calls, such as CStr.



According to my own such definitions, a VB Coder would use CStr whereas a .NET Developer would use String.Format (as did Heschalcher) or better yet, Interpolated Strings.



VB Coder:



hourString = Cstr(hours) & " hours"


.NET Developer:



hourString = String.Format("0 hours", hours)


Or



hourString = $"hours hours"


To calculate the number of hours, if you aren't going to use something nice like TimeSpan, I would change this line of code:



hours = floor(TotalSeconds / 3600)


To simply use integer division:



hours = TotalSeconds 3600


Also, VB.NET does support short-circuited conditionals (unlike VBA), so this line of code:



If hours > 0 and (minutes > 0 or seconds > 0) Then


could be changed to:



If hours > 0 AndAlso (minutes > 0 OrElse seconds > 0) Then


This example employs short-circuiting with the AndAlso and OrElse operators.



Again, Heschachler's answer with TimeSpan and StringBuilder is spot-on.







share|improve this answer












share|improve this answer



share|improve this answer










answered 3 hours ago









Rick DavinRick Davin

3,3428 silver badges19 bronze badges




3,3428 silver badges19 bronze badges











  • $begingroup$
    Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
    $endgroup$
    – BishNaboB
    1 hour ago
















  • $begingroup$
    Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
    $endgroup$
    – BishNaboB
    1 hour ago















$begingroup$
Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
$endgroup$
– BishNaboB
1 hour ago




$begingroup$
Hi Rick. Thank you for the distinction between coder types, it's an interesting observation. I suppose the cstr() comes from the fact this is code added to a report in SSRS, which uses those functions. Short circuiting is something I use in SSRS, and forgot to add in to the code example. Integer division is not something I'm overly familiar with so gives me something to read up on.
$endgroup$
– BishNaboB
1 hour ago










BishNaboB is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















BishNaboB is a new contributor. Be nice, and check out our Code of Conduct.












BishNaboB is a new contributor. Be nice, and check out our Code of Conduct.











BishNaboB is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f223421%2fconvert-integer-to-full-text-string-duration%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480