LabVIEW tip: easy relative timestamp calculations

Posted on Friday 8 June 2007

Have you ever wanted to add relative time to a time stamp data type in LabVIEW? You can easily do this by adding a floating point to the time stamp, as shown below.

Add Relative Time.png

The units are in seconds and, as you can see, Time Stamp out is one second greater than Time Stamp (in) as we would expect.

This is pretty cool, but we can do something even cooler! Let’s use the units feature of LabVIEW to specify relative time in units other than seconds. First, make the Unit Label of the relative time constant visible by right-clicking on it and choosing Visible Items>>Unit Label. While the unit label has the cursor, type the letter “d” to set the units as days. Your VI should now look like the Block Diagram shown, below:

Add Relative Time with Units.png

Run the VI and you’ll see that Time Stamp out is one day greater than Time Stamp (in) as we would expect.

Other units of time are

  • “a” (year)
  • “h” (hour)
  • “min” (minute)
  • “s” (second)

Give some of these a try and see how they work :)

If you liked this tip then you should check out, LabVIEW for Everyone.  It’s chalk full of great LabVIEW tips and tricks that I’ve learned over the years.

11 Comments for 'LabVIEW tip: easy relative timestamp calculations'

    June 8, 2007 | 4:14 am

    And then you can set the format of the constant to “relative time” and have a nice HH:MM or HH:MM:SS input… ;)

    Ton Plomp
    June 8, 2007 | 7:43 am

    Who told you that?

    It are the little things that make life easier


    June 8, 2007 | 8:38 am

    Carsten: Yes, setting the unitless floating point to a display mode of HH:MM:SS is another great tip!

    Ton: I think that I learned this by just trying it. I thought to myself, “it would be great if I could just add a number with units of time to a time stamp” and… it worked :)

    June 8, 2007 | 9:25 am

    I’ll have to try this out myself.

    If the unit is years, does leapyears come into play?

    Similar to the note about not doing a “transpose” on a graph because it could be missed by readers, there is a danger of people who do not know this tidbit, could freak-out when they first it.

    Thank you for sharing Jim!


    June 8, 2007 | 10:29 am

    Using 1 year does not seem to work correctly.

    It looks like we have a leap year coming up but that does not explain what I am seeing with the rest of the time.


    June 8, 2007 | 10:37 am

    Another useful time unit are SI variations such as e.g. “ms”. Obviously works equally well.

    Try to add 1000ma and you’ll move up one year. Silly!

    June 8, 2007 | 11:06 am

    That’s one of those, “Why didn’t I think of that!” moments.

    Thanks for the tip Jim.

    June 9, 2007 | 11:27 am

    When did this start?

    I know it does not work in 7.1.

    June 9, 2007 | 11:27 am

    Correction: it does work in 7.1. I just forgot to change the constant value from 0.

    Alvin Moore
    June 11, 2007 | 6:59 am

    Great tip, Jim. One thing I noticed in trying it is that you need to be careful if you use this on a subvi input. I built a sub with an input with the units set to minutes, thinking that if I now use this sub, the input there will be minutes. However, when I dropped this sub on a diagram and created a constant, the constant had units too, and the default was “s”, so instead of adding 5 min I was only adding 5s. I had to manually set the units on the constant to input 5 minutes into this control. I can see how this behavior could be argued either way. It seems to me the constant should be forced to adapt to the control units values, because that is set on the input of the sub, but that is not the case, so it is wise to be careful in use of this ability in subs.

    June 12, 2007 | 10:10 am

    Yes, units are one of those tricky features in LabVIEW that presents some issues for others who will inherit your code and have to decipher your intent. Generally, I don’t use units on subVI inputs. Also, I always add a comment to my code that describes what I did and why (e.g., to call out that 1 d means 1 day and not one second).

Leave a comment



Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.

RSS feed for comments on this post |


Bad Behavior has blocked 775 access attempts in the last 7 days.