# Duration in digits

Hello, Dear KNIME users!

I have a question, I am taking difference of two dates as a duration, but I have to represent it in digits (in number) in order to take some of them
Can you suggest me some hints, please
For example, 3H 15M => 3.15(in number)

Here I have taken the difference of start_date and end_date

This could be done using just the String Manipulation node

Please add the following instruction in a String Manipulation node and directly you will get the value in Double format

toDouble( replace( replaceChars( upperCase(\$date&time diff\$), "M ", ββ), βHβ, β.β))

All the best,

Ael

5 Likes

oh, sure))) I was looking for duration to digits node:DDD
I have added substring , cuz it has such format
toDouble(substr(replace(replaceChars(upperCase(\$date&time diff\$), βMβ,"" ), βHβ , β.β) , 2 ))

3 Likes

Cool @Karlygash and thanks for validating the answer !

Cheers

Ael

2 Likes

Hi,
the other option is to set the Duration to Granularity and seconds and divide the results with 3600.

BR

3 Likes

Thanks @morpheus ! Indeed this is a more straight solution that I would recommend in priority.

Best

Ael

1 Like

Hi @morpheus, you are right but I believe what isnβt totally obvious from the subject is that the workflow already had a Duration to Number node that can return the Duration as purely decimal hours.

I believe (but I may be wrong! ) that what is wanted here is the same duration to be converted from 2H 15m to β2.15β [h.mm] rather than 2.25 hours, so if that is the case, then taking your suggestion further, the duration returned in seconds could be turned into that format using String Manipulation node using the following (really fun to write ) formula:

``````join(string(toInt(\$date&time diff\$/3600))
,"."
toInt((toDouble(\$date&time diff\$)/3600 - toInt(\$date&time diff\$/3600))*60)
),2,"0")
)
``````

and of course in that case, it would be returned as a String rather than a numeric, as we wouldnβt really want β2.15β to mean 2.15
(or we could return in minutes and use 60 instead of 3600, if we want to save typing )

It would have been nicer to be able to make use of the mod function in Math Formula node, but then I couldnβt return it as a string, so I decided I was going to have to do it the old-fashioned way!)

After more digging, a lower code method was to get the duration as the original duration (rather than seconds) format and then use an Extract Duration Fields node, extracting Hours and Minutes to two new columns. These can then be fed to a String Manipulation with the expression:

`join(string(\$Hours (#1)\$),".",padLeft(string(\$Minutes\$),2,"0"))`

A pity that there doesnβt appear to be a way to (a) extract both fields to a single column in a specified format, or (b) have it pad minutes with leading zeroes, so that part still requires code, otherwise I would have just thrown it at a column combiner.

3 Likes

Hi @takbb,
your right that the initial reqeust was to return a value of 3.15.
But based on my experience this value is a good candidate for a wrong interpretation. I recommend returning a digit value of 3.25 or a string value of β03:15β but not a mixture of both.

3 Likes

Hi @morpheus, thatβs good advice. Iβm easy on the leading zero for the hours (which of course could be accommodated with another padLeft), but I agree that concatenating a β:β instead of a β.β makes the meaning of the data clearer. I just assumed this was a localisation thing.

2 Likes

Hello @takbb @morpheus @aworker
Thank you
I ve never known that in string manipulation we can use formulas as well

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.