Convert time to decimal time - InfoPath Dev
in

InfoPath Dev

Convert time to decimal time

Last post 01-09-2014 08:30 AM by recono1. 14 replies.
Page 1 of 1 (15 items)
Sort Posts: Previous Next
  • 01-14-2010 12:00 PM

    Convert time to decimal time

    I need help with formula or code to convert time entered in one field to decimal time in another field.  For example, 2:33 would get converted to 2.55.  Any help would be appreciated, thanks.

  • 01-14-2010 02:08 PM In reply to

    Re: Convert time to decimal time

    try this code:

    public void CTRL2_5_Clicked(object sender, ClickedEventArgs e)
    {
     XPathNavigator root = MainDataSource.CreateNavigator();
     string Time = root.SelectSingleNode("/my:myFields/my:field1", NamespaceManager).Value;
     string[] Temp = Time.Split(':');
     root.SelectSingleNode("/my:myFields/my:field2", NamespaceManager).SetValue((int.Parse(Temp[0])+(double.Parse(Temp[1])/60)).ToString());
    }

    HTH

  • 01-14-2010 07:34 PM In reply to

    Re: Convert time to decimal time

    Or you can just use this formula:

    concat(substring-before(field, ":"), ".", 100 * substring-after(field, ":") div 60)

    round(100 * substring-before(field, ":") + 100 * (substring-after(field, ":") div 60)) div 100

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-18-2010 11:59 AM In reply to

    Re: Convert time to decimal time

    The formula is working, but for some calculations it is adding 2 decimal points for calculations that go to infinity.  I tried changing the field with the formula from Text to Decimal, but I get validation errors.  Here is an example of what is occurring:

     1:59 gets converted to 1.98.33333333333333

    12:11 gets converted to 12.18.33333333333332 

     

  • 01-18-2010 02:27 PM In reply to

    Re: Convert time to decimal time

    try using the round() function in the formula:

    round(100*substring-after(field, ":") div 60)

     

    or use ToString("#.##") in the C# code:

    (int.Parse(Temp[0]) + (double.Parse(Temp[1]) / 60)).ToString("#.##")

  • 01-20-2010 09:14 AM In reply to

    Re: Convert time to decimal time


    As you can tell working with formulas is not my strong suit...  I tried the following formula and this is the error message I get:

    Formula: 

    concat(substring-before(NCRHours, ":"), ".", round(100 * substring-after(NCRHours, ":") / 60)

    Error Message:

    Expected value type: ,

    Actual value: end-of-string
    concat(substring-before(NCRHours, ":"), ".", round(100 * substring-after(NCRHours, ":") div 60)<--

  • 01-20-2010 09:34 AM In reply to

    Re: Convert time to decimal time

     Looks like you're missing an end-parenthesis.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-20-2010 10:03 AM In reply to

    Re: Convert time to decimal time

    It works!!!  You have no idea how appreciated this is - thanks a billion times!!!!!

  • 01-20-2010 01:21 PM In reply to

    Re: Convert time to decimal time

    I love Notepad2 for things like this (note, link is to the .zip file for the download) -- it has parenthetical matching -- highlights your parentheses when you select them, so that you can see the pairs easily.

    Hilary Stoupa

  • 02-18-2010 11:51 AM In reply to

    • rjanssen
    • Not Ranked
      Male
    • Joined on 02-18-2010
    • The Netherlands
    • Posts 1

    Re: Convert time to decimal time

    Hey! Have the same problem, i want to convert the time into decimal but i cannot get this code to work. I don't understand what i am doing wrong.

    Let's say i have two fields field1 is showing the normal time 2:33 how do i get field2 to convert the time to 2.55?
    I tried the code Jimmy was mentioning but i can not get it to work.

    Any help will be appreciated!

  • 12-12-2012 08:48 AM In reply to

    Re: Convert time to decimal time

    Sorry about reopening a fairly old thread. I'm having an issue with converting times involving 5 mintue intervals. For instance, when I convert 1:05 (h:mm), it is computed as 1.8. The code formula I'm using is: concat(substring-before(IncomingTime, ":"), ".", round(100 * substring-after(IncomingTime, ":") / 60))

    It appears that formula is treating 05 as 50, hence the .8 of an hour. Does anyone have any suggestions on how to treat it as 5 minutes in lieu of 50?

  • 12-12-2012 09:16 AM In reply to

    Re: Convert time to decimal time

    Actually, it's doing this:

    concat(1, ".", round(100 * 05 / 60))

    concat(1, ".", round(8.33333))

    concat(1, ".", 8)

    1.8

    And I must blame my own naïveté for posting that faulty formula 3 years ago.  

    I'd suggest this formula:

    round(100 * substring-before(IncomingTime, ":") + 100 * (substring-after(IncomingTime, ":") div 60)) div 100

    And I'll correct my post above.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-12-2012 10:50 AM In reply to

    Re: Convert time to decimal time

    Thanks for the very quick response. In working with the formula, I needed modify your suggestion to get the rounding and decimal placement correct in the final value.

    (100 * substring-before(IncomingTime, ":") + (substring-after(IncomingTime, ":") / 60) * 100) / 100

    But things are working swimmingly. Thanks Jimmy.

  • 12-12-2012 10:52 AM In reply to

    Re: Convert time to decimal time

    Good catch and thank you for pointing it out.  I'll modify my formula accordingly.
    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-09-2014 08:30 AM In reply to

    Re: Convert time to decimal time

     I am trying to do this in InfoPath 2013 and it is not working I have a field with a time OperationalTime and it ='s 12:30  in the conversion field I have it set to double and the formula is : (100 * substring-before(OperationalTime, ":") + (substring-after(OperationalTime, ":") / 60) * 100) / 100  All I am getting is NaN in the field

     

    I also tried concat(substring-before(OperationalTime, ":"), ".", round(100 * (substring-after(OperationalTime, ":")) / 60)) and I get 12.NaN 

     When I just run substring-after(OperationalTime, ":") it returns 30:00 instead of 30 

Page 1 of 1 (15 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.