LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting a string to a time stamp variable

What is the easiest way to convert a string, say "1:00:00.000 PM 12/1/2005", to a time stamp variable? Thanks.
 
XLESmiley Surprised
0 Kudos
Message 1 of 19
(7,658 Views)

The "tricky" part is to convert from AM/PM to 24 hour clock
 - between 12:00 AM and 12:59 AM, subtract 12 hours : 12:59 AM --> 00:59
 - between 1:00 AM and 12:59 PM, straight conversion :  10:00 AM --> 10:00
 - between 1:00 PM and 11:59 PM, add 12 hours :  10:59 PM --> 22:59

The attached vi does the job.

Chilly Charly    (aka CC)
Message 2 of 19
(7,640 Views)
CC
 
Thanks. Happy new year.
 
 
XLESmiley Happy
0 Kudos
Message 3 of 19
(7,632 Views)
I don't quite like the multiple Time stamp <-> Time record conversions. Attached is a simplified solution that seems to give the same result for non-pathological input. (But please test for yourself, I haven't fully tested it). 🙂
 
(Of course a full solution should be able to automatically distinguish between AM/PM and 24h format and should also be able to deal with 2 digit years. It won't be too difficult to implement all this).
 
 
Message 4 of 19
(7,621 Views)
Hey! Wait a minute...... 😮
 
If you know that the string is properly formatted (=replace the space after M with a newline), you can just write it to the NumText.Text property of the time stamp!
 

Now we even retain the milliseconds!

Message Edited by altenbach on 12-31-2005 11:49 AM

Download All
Message 5 of 19
(7,625 Views)

One problem with this is the timestamp is set by default to specific settings (system date and time). Unless those are identical to your string format, you will be in a problem.

Attached is a VI I wrote once, which I think should be a basic part of the Time and String palettes (or whatever they're called in 😎 - it accepts a format string and converts it to a timestamp. It may not be complete, but it shows the concept. With some modification, it could probably accept AM\PM too.

BTW, Altenbach, I think your method is basically cool. Perhaps using the Format String property would help there.


___________________
Try to take over the world!
0 Kudos
Message 6 of 19
(7,611 Views)


@tst wrote:
One problem with this is the timestamp is set by default to specific settings (system date and time). Unless those are identical to your string format, you will be in a problem.
Yes, the writing to the text property can only be used under very tighly controlled conditions. The smallest mismatch causes the indicator simply not to update, but there won't be any error out of the property node. 😞
0 Kudos
Message 7 of 19
(7,596 Views)
Christian, I am puzzled by the comments attached in your vi, about the valid AM/PM format : I thought that times beginning with 00 were not allowed in the 12 h clock system.Could you develop further ?
Chilly Charly    (aka CC)
0 Kudos
Message 8 of 19
(7,574 Views)


@chilly charly wrote:
Christian, I am puzzled by the comments attached in your vi, about the valid AM/PM format : I thought that times beginning with 00 were not allowed in the 12 h clock system.Could you develop further ?

Yes, they are not allowed so there is no problem. 🙂
 
I was just documenting how the various code versions can deal with "pathological input". While it is not allowed, there is no way to prevent an ignorant user from typing in such a time string. In this case, the program should preferably make a good educated guess what is meant. (or produce an error condition).
 
For example an input of 00:30:45 AM or PM would both translate to 12:30:45 AM in your code, while in the case of PM, it is more likely that 12:30:45 PM was intended by the operator. (Similarly, a time of 14:30:45 AM will be correctly read as 2:30:45 PM in your code while my code would falsely read it as 2:30:45 AM.).
 
So, Yes the full comment on the diagram shoud have read: "Requires correct AM/PM format. (can even deal with some flavors of incorrect AP/PM formats, but ...)" 😄
 
 
Message 9 of 19
(7,567 Views)
Just for completeness, here's a simple modification that retains the fractional seconds. 🙂
 

Message Edited by altenbach on 01-01-2006 12:10 PM

0 Kudos
Message 10 of 19
(7,565 Views)