cancel
Showing results for 
Search instead for 
Did you mean: 

Time/Date string to seconds?

patrick54
Member

Time/Date string to seconds?

I have data that contains a time/date string (from Format Date/Time String) I saved to a .txt file. I would now like to be able to take that data and make calculations on it (ex. time between two points). Is it possible to do this?

Thanks,
Patrick
13 REPLIES 13
Photon_Dan
Active Participant

Re: Time/Date string to seconds?

In the Time & Dialog palette there is a function called "Date/Time to Seconds". You feed it a cluster containing the day, month, year, hour, min, sec, etc...

You can get most of the values for the cluster by parsing the date and time string that was formatted by the Format Date/Time String function. I would recommend using a Scan from String function to parse the string and extract the necessary values.

The output of the Date/Time to Seconds function is a U32. Run both strings through and you've got two time points in seconds. Subtract for the difference between the two points.

Daniel L. Press
NI Certified LabVIEW Developer
PrimeTest Corporation
www.primetest.com
Dennis_Knutson
Knight of NI

Re: Time/Date string to seconds?

Just use the function Date/Time to Seconds. It's in the Time & Dialog palette below and to the left of the Format Date/Time String function. You'll need to parse out the string, convert to integers, and wire the results to the input cluster.
Highlighted
GPeople
Member

Re: Time/Date string to seconds?

Message contains an attachment

Given:  date/time string:  11/20/2019 11:59:31 AM

Another way is to convert the date/time string back to a Time Stamp cluster.

Then you can do any math you need very easily.

 

An easy way to get back to Time Stamp format is to use Scan From String function with %T for the format string input.  You will see the output 1  for this function change to a Time Stamp cluster.

 

If you are concatenating a date string with a time string, you must add the date string to the time string (in other words the upper input to the concat function has to be the TIME string, the lower input has to be the DATE string.

Certified LabVIEW Developer
LabVIEW 7.1 - Present
RTSLVU
Trusted Enthusiast

Re: Time/Date string to seconds?

Message contains an image

If you convert a time stamp to a double you can do all the math you want on it and convert it back.

 

seconds2.png

========================
=== Engineer Ambiguously ===
========================
rolfk
Knight of NI

Re: Time/Date string to seconds?

Message contains an image

@RTSLVU wrote:

If you convert a time stamp to a double you can do all the math you want on it and convert it back.

 

seconds2.png


No need for the Timestamp to Double and vv. at all. The arithmetic plus and minus nodes will operate correctly when you try to add or subtract a double (or integer) from a timestamp.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
RTSLVU
Trusted Enthusiast

Re: Time/Date string to seconds?

Message contains an image

@rolfk wrote:

@RTSLVU wrote:

If you convert a time stamp to a double you can do all the math you want on it and convert it back.

 

seconds2.png


No need for the Timestamp to Double and vv. at all. The arithmetic plus and minus nodes will operate correctly when you try to add or subtract a double (or integer) from a timestamp.


Right... But I really detest coercion dots

 

AKA "Loser Dots"  

========================
=== Engineer Ambiguously ===
========================
rolfk
Knight of NI

Re: Time/Date string to seconds?

Message contains an image

Not quite sure where you see coercion dots. This is LabVIEW 2016, but I doubt there is much difference since at least around 8.2. Even then a coercion dot is usually not really anything else than an implicit conversion functions. If you have coercion dots on scalar wires and get rid of them by adding a conversion node, you have basically not gained anything nore lost anything. Of course if you can get rid of coercion dots altogether by changing dataypes consistently throughout a diagram, that is even better. If conversions are unavoidable it is a good idea to do them explicitedly on the scalar wire inside a loop rather than on the array that goes into or out of an autoindexed loop, though.

 

Timestamp Arithmetic.png

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
GerdW
Knight of NI

Re: Time/Date string to seconds?

Hi, 

 

additionally you can enable the unit of the numeric constant and set it to "4h" instead of plain "14400" (seconds)...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Frozen
Active Participant

Re: Time/Date string to seconds?

Message contains an image

Since you have saved the data to a text file, I assume you are then reading that file and want to convert that time stamp back to a LabVIEW data type. Try something like this...

 

image.png

 

The trick is getting the format string to match the format you have save the data as.

After that, as other have pointed out, just simply add or subtract as required.

 

EDIT: Oops, looks like GPeople beat me too it

---------------------------------------------
Former Certified LabVIEW Developer (CLD)