# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Timestamp difference

Hi,

I'm doing a project where I record timestamps for each stage in a process. I'm recording the timestamp into a string txt file (Like below).

What is the best way to calculate the difference between two timestamps (that have already been converted to string).

Example Txt File.

50374211-1 15/07/2020 16:20:34 SS1A Liam Stage 1
50374211-1 15/07/2020 17:30:00 SS2A Liam Stage 2
50374211-1 15/07/2020 17:30:00 SS3A Liam Stage 3

So Ideally a calculation:  15/07/2020 17:30:00 - 15/07/2020 16:20:34 = Difference in seconds.

It's really the reconverting the timestamp from string back to a timestamp is the main problem.

Thanks,

Pád

Message 1 of 8
(572 Views)

## Re: Timestamp difference

What is the best way to calculate the difference between two timestamps (that have already been converted to string).

So Ideally a calculation:  15/07/2020 17:30:00 - 15/07/2020 16:20:34 = Difference in seconds.

Convert those strings back to timestamps, then just subtract them…

It's really the reconverting the timestamp from string back to a timestamp is the main problem.

Use ScanFromString with the correct format string!

I suggest "%<%d/%m/%y %H:%M:%S>T"…

Best regards,
GerdW

using LV2011SP1 + LV2017 (+LV2020 sometimes) on Win10+cRIO
Message 2 of 8
(570 Views)

## Re: Timestamp difference

To convert from string to timestamp, you just need a Scan From String node and the right format string to get back the timestamp in the correct format.

Then a Subtract node can take two timestamps as inputs and immediately returns a double of the seconds of difference between the two.

Redhawk
Test Engineer at Moog Inc.

Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.

Message 3 of 8
(568 Views)

## Re: Timestamp difference

That's going to be ugly because if that's not UTC time, it's likely you'll have to contend with daylight saving issues.  If, let's say, you're burning in a unit over the weekend that you "spring forward", you'll have a "hole" in your data, .  It's even worse with "fall back" because data between 1am and 2am will be interleaved and/or overwritten.  Depending on whether your burn in was specified in hours or dates, you may come up an hour short or long.

Bill

(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 4 of 8
(554 Views)

## Re: Timestamp difference

I just thought I would mention that this is exactly why I always convert my timestamps to a double and save that number.

It's easy to reformat that double to any time/date format in Excel, and you can still do math on them after reformatting to find elapsed time.

About a decade or so ago an Engineer jumped down my throat for not being able to do math on a text timestamp.

-------------------------------------------------------------------
Unfortunately, most readers of this Forum, including some real Experts, have not mastered the skill of being able to read the code that Posters fail to post. If we cannot see "what you did wrong", we are unable to tell you how to fix it. (Bob Schor 28 August 2018)
Message 5 of 8
(488 Views)

## Re: Timestamp difference

In the format string use %Y and not %y. %Y will return 2020 and %y will return 20 (the year without the century.

Ben64

--------------------------------------------------
The best way to say thanks is to give kudos!
Message 6 of 8
(446 Views)
Solution

## Re: Timestamp difference

Cheers all for the help, I've used a bit of everything. For the GMT issue I've just recorded both GMT and UTC time stamps in the txt file and will use UTC for my time lapse calculations.

Here's my final code:

Thanks,

Pád

Message 7 of 8
(378 Views)

## Re: Timestamp difference

you don't need to convert to DBL to subtract two timestamps.

What's the reason of that last Q&R function to calculate "Days"? Isn't "Days" already the IQ output of the "Hours" calculation?

Btw. using AutoCleanup would improve the code readability of the code shown in your image - usually this is a bad sign for your code quality…

Best regards,
GerdW

using LV2011SP1 + LV2017 (+LV2020 sometimes) on Win10+cRIO
Message 8 of 8
(370 Views)