Example Code

Converting from Date/Time String back to a Timestamp or Number of Seconds

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This example is used to convert a date and time string back into a timestamp.

 

 

Description

It's common to convert timestamps to strings when writing data to a file, and there are a number of native LabVIEW functions that allow you to do so (Get Date/Time String VI, Format Date/Time String VI). However, if you need to convert a date and time string back into a timestamp, there is no built-in function in LabVIEW to do so! There are good reasons to do this, such as reading data from an old logfile and plotting it onto a LabVIEW Graph. If you want time on the x-axis, you're currently a little out of luck since you can't use Date/Time strings to define the x-axis.

 

Attached is a solution which allows one to take a Date String and Time String (formatted per the outputs of the Get Date/Time String VI), and create a Timestamp. It makes a few extra conversions, and is meant to be modified to fit your application's specific use case.

Convert DateTime String.png

 

Requirements

  • LabVIEW 2012 (or compatible)

 

Steps to Implement or Execute Code

  1. Run the VI and the conversation results will display on Front Panel.

 

Additional Information or References 

VI Snippet

1.png

 

 

**This document has been updated to meet the current required format for the NI Code Exchange.** 

Sanjay C.
Embedded Software Product Manager| National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
PhillipBrooks
Active Participant
Active Participant
on

You can use the following format string with "Format into String" and "Scan from String" to persist an 8601 UTC timestamp.

%^<%Y-%m-%dT%H:%M:%S%3uZ>T


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness

twodogsdad
Member
Member
on

To SanjayC Thanks very much. Could you possibly recompile to LV 8.2 and post that vi as well? Thanks

Also, to PhilipBrooks I tried your string format and it crashed LV twice (ver 8.2)... just FYI Thanks

Cal

Not applicable
on

Calvin: I put up a LV 8 and a LV 8.2 version. I did get a warning when saving back to LV 8 and LV 8.2, so let me know if the 8.2 version is broken in any way. Hopefully you find this function useful.

Phillip: does your solution allow you to go from a string to a timestamp? I'm not totally clear on the alternative you suggest.

PhillipBrooks
Active Participant
Active Participant
on

Calvin: I tested this format string in both directions with LabVIEW 8.6 and 2010; I can't test 8.2, sorry...

BranchSupport-NIC: The format string example I gave works with both "Format into String" and "Scan from String".

There was a discussion on LAVA about persisting timestamp data in a file for the OpenG library and we hit upon the one I provided above.

8601 UTC Date-Time String Example.png


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness

twodogsdad
Member
Member
on

Morning,

Thanks for the 8.2 version it seems to work just fine. No errors.

Philip: I haven't had a chance to investigate your solution in 8.2. I expect it will be some time before I can again but I will let you know here what I find.

Thanks again,

Cal   

Mads
Active Participant
Active Participant
on

I typically have to do this with time strings that have an unknown format. There is a little gem from More Good Ideas that can handle arbritrary time stamps. It's downloadable from their site. Here's the page which describes it, just search it for parse arbritrary:


http://www.mooregoodideas.com/General_VIs/Time/index.html

The downside to this is that if you need to parse a lot of these strings it is not very fast. I've been thinking about creating such a function that would remember the format from the previous parse and use that to speed up the next.

MikeBowen
Member
Member
on

Is there any way that I can get the sub second accuracy?  The string has the accuracy there but when it converts it the function truncates the sub second data.

Mike

Mike
PhillipBrooks
Active Participant
Active Participant
on

I'm not able to provide an example from where I'm at right now, but I think the only thing you should need to do is change the value of the digit(s) before the uZ. the u indicates the fractional part. so a string of

%^<%Y-%m-%dT%H:%M:%S%6uZ>T

should return a timestamp with microsecond accuracy as long as there are 6 digits after the decimal point.

I recall that there is a bug that does not handle the locale specific decimal separator (always reverts to period/dot) but this was fixed in LV2012.


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness

bfrantz
NI Employee (retired)
on

This code doesn't work as expected on systems observing Daylight Saving Time, the Date/Time to Seconds primitive automatically adds an hour to the time in this case.

An approach like the one PhillipBrooks mentioned converts properly:

https://decibel.ni.com/content/docs/DOC-25470

cderrico
Member
Member
on

No one of the vi's works with Labview2015,

I also gave problems adding microseconds to TimeStamp.

The Cluster has no problem.

I have problem in the function "Date/Time to Seconds".

Thank you in advance

Best Regards

Carmine D'Errico

PhillipBrooks
Active Participant
Active Participant
on

The format string function most certainly supports microseconds. If you use the snippet I originally posted, the DISPLAY precision of the timestamp control and indicator on the front panel were only set for 3 digits (milliseconds).

My second post suggested modifying the format string from %3u to %6u. That would properly set "resulting string" to 6 digits, but the timestamps would APPEAR to only have milliseconds. It is important not to confuse the VALUE of a variable with the FORMATTING of a value.

http://www.ni.com/tutorial/7900/en/

Timestamp formatting.png


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness