LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Timestamp Conversion

Solved!
Go to solution

I am trying to convert a non-std timestamp (date and time separated) into a standard timestamp in order to sort the 2D array by date & time.  

The time portion comes through fine, but the date is somewhat wacky - for example 2024/02/15 (February) turns into 12/15/2024  (December).  I've tried with and without leading zeroes. 

 

Q1: Why can't I convert date string to proper Timestamp date?

Q2: How do I "reconstitute" separate date and time into one accurate Timestamp?

 

Please see attachment for details.  LV22

 

Would appreciate any guidance.  Thanks in advance 🙂

0 Kudos
Message 1 of 8
(1,067 Views)
Solution
Accepted by topic author Gaveloni

1. You can use the correct Format Specifier Syntax to parse the entire string.

ZYOng_1-1709171161519.png

 

2. You can change the display format.

ZYOng_0-1709171020240.png

 

 

-------------------------------------------------------
Applications Engineer | TME Systems
Message 2 of 8
(1,025 Views)

Unfortunately, because I've been developing LabVIEW for years and work with a team, I don't "get ahead of my colleages" and do not have the latest version of LabVIEW (unlike most new users of LabVIEW), so I cannot open your 2022 code.  [We are mostly using LabVIEW 2019, but I have a few projects in LabVIEW 2021 ...].

 

However, if you know the format of your string, it is easy/peasy to tease out the time components.  Let's say you know you have a date string of the form "spaces""year"/"month"/"day""spaces" (that is to say, "   2024/2/15   " or "2024/02/15"), you can use Scan from String to get an array of I32, [2024, 2, 15].

Extract Date Info.png

If this isn't the question you were asking (but that I couldn't see), my apologies.

 

Bob Schor 

0 Kudos
Message 3 of 8
(1,022 Views)

Thanks Bob, uploading in version 19

0 Kudos
Message 4 of 8
(991 Views)

Thanks for the upload.

 

What you do with this depends on "What do you want to do with this?".  You start with N x 2 array of strings representing a Date (in YYYY/MM/DD format) and Time (in HH:MM PM format). Pass it into a For Loop to work on each row, which has Date and Time.

 

Date is easy.  Scan from String using format <Decimal number>/<Decimal number>/<Decimal Number>, which (if you use the "Edit Format String" option) turns out to be %d/%d/%d.  The Time is a little harder, since it has both <Decimal number>:<Decimal number> <AM or PM>.  This Format String turns out to be %d:%d %s (be sure you use the <space> entry!), which gives you two numbers (hours and minutes) and an AM/PM string.  So pass all three through a Case Structure and if the String is PM, add 12 (hours) to the first (hour) Array element.  Bring the Date and Time 1D Arrays out from the For Loop and they'll become 2D numeric entries.

 

But what if you want them to look like TimeStamps?  There's a function in the Timing Palette that constructs a Time Stamp from "Date Parts".  It uses a Cluster that NI provides called a "date time rec".  You can bundle the Year, Month, Day of Month, Hour, and Minute into this Cluster, pass it through NI's "Construct TimeStamp" function, and you get (are you ready?) a TimeStamp.

 

Forgive me for doing this to you, but I'm going to post this as a VI Snippet (which you may or may not know about).  It is a "picture of code", which I generally hate, because it means that I, the Big Expert, have to try to copy your messy code to figure out "where's the bug", but in this case, I'm playing The Professor, who wants you to copy this (and use the LabVIEW Help when you get stuck) to learn how to parse Strings, how to use For loops to selectively take apart and build arrays, how to use Scan from String to parse Strings, and how to play with LabVIEW Time representations.

Parsing Date, TimeParsing Date, Time

 

Bob Schor

 

0 Kudos
Message 5 of 8
(972 Views)

@Bob_Schor wrote:

Parsing Date, TimeParsing Date, Time

 

 


No.  Way too complicated.  Look at what ZYOng already posted.  It is exactly what I would have done since the Scan From String can handle weird date/time string formats.  For more details: Format Codes for the Time Format String 


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 8
(965 Views)

I agree with @crossrulz, @ZYOng's method is much simpler (though it can be tricky finding those somewhat-obscure codes for Date/Time records).

 

Still, I'm sure we (ZYOng, crossrulz, and Bob_Schor) can all agree that Scan from String is your friend.

 

Bob Schor

0 Kudos
Message 7 of 8
(929 Views)

@Bob_Schor wrote:

(though it can be tricky finding those somewhat-obscure codes for Date/Time records).


It's not tricky to find.  I have a printout hanging in my cube.  Same for an ASCII table and the Format Specifier Syntax.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 8
(898 Views)